从一个excel表中读取JXL API并将其写入java中的另一个excel表仅针对有限的行和列进行

时间:2017-07-08 22:43:39

标签: java jxl

我已经搜索了在此论坛中提出的任何类似查询,但没有得到任何查询。所以,我怀疑这个。

目标: - >尝试从一个Excel工作表中读取数据并创建新的Excel工作表,并使用ONLY JXL API将该数据粘贴到其中。我已经使用.xls文件来阅读。

问题: - >我能够做到这一点,但它是以某种方式,只为3列和3行做。如果我在另外的单元格中提供另一个数据,它会给出 错误: -

"**Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
at myTimesPackage.ReadNwriteJXL.main(ReadNwriteJXL.java:42)**"

[Code image][1]

    package myTimesPackage;

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ReadNwriteJXL {

    public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException {

        File f = new File ("D:\\Rajiv\\Selenium\\4jxl.xls");
        Workbook wb = Workbook.getWorkbook(f);
        Sheet s=wb.getSheet(0);

        File f1 = new File ("D:\\Rajiv\\Selenium\\w1.xls");
        WritableWorkbook wb1 = Workbook.createWorkbook(f1); 
        WritableSheet ws1 = wb1.createSheet("Sheet1", 0);

        int b = s.getColumns();
        System.out.println("The No. of Columns in the Sheet are = " + b);
        int a = s.getRows();
        System.out.println("The No. of Rows in the sheet are = " +a);

        String p [][]= new String[s.getColumns()][s.getRows()];

        for (int i=0; i<b; i++){

            for (int j=0; j<a; j++){

                p[i][j]=s.getCell(j, i).getContents();

                Cell c = s.getCell(i, j);
                System.out.println(c.getContents());

                Label l = new Label(j,i,p[i][j]);
                ws1.addCell(l);

            }
        }

        wb1.write();
        wb1.close();
        System.out.println("File Written");
    }
}
  

输入.xls文件是: - &gt; 3x3矩阵

raj 3   5
12  9   22
22  24  25

请指导我做什么错误。感谢

0 个答案:

没有答案