【问题标题】:reading multiple excel sheets in java在java中读取多个excel表
【发布时间】:2015-03-20 00:43:53
【问题描述】:

我正在尝试将 Excel 文档的多个工作表的值添加到 ArrayList。不使用 HSSFSheet。这似乎更简单。小心不要忘记将字符串类型更改为数字。

public void setinputfile(String inputfile) {
    this.inputfile = inputfile;}

public void read() throws IOException {
    File inputWorkbook = new File(inputfile);

    try {
        Workbook w = Workbook.getWorkbook(inputWorkbook);
        int Num = w.getNumberOfSheets();
        for (int i = 0; i < Num; i++) {
            Sheet sheet = w.getSheet(i);
            Sheet s = w.getSheet(i);
            int row = s.getRows();
            int col = s.getColumns();
            for (int i1 = 0; i1 < row; i1++) {
                inner = new ArrayList<Double>();
                for (int j = 0; j < col; j++) {
                    Cell c = s.getCell(j, i1);
                    CellType type = c.getType();
                    System.out.println(c.getContents());
                    String text = c.getContents(); // example String
                    double value = Double.parseDouble(text);
                    inner.add(value);
                }
                Kargo_Amount.add(inner);
            }

        }
    } catch (BiffException e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) throws Exception {
    Facility_Location test = new Facility_Location();
    test.setinputfile("C:\\Users\\soheyl\\Desktop\\Kargo miktarii.xls");
    test.read();
}

【问题讨论】:

    标签: java spreadsheet import-from-excel


    【解决方案1】:

    你调用 getSheet(1) 而不是 getSheet(i)

    【讨论】:

      【解决方案2】:

      以下代码正在读取所有 xls 文件,您必须将 jxl.jar 文件添加到此:

      import java.io.File;
      
      import jxl.Cell;
      import jxl.Sheet;
      import jxl.Workbook;
      
      public class ReadExcel {
      
          public static void main(String[] args) throws Exception {
              File f = new File("N:\\Data\\Book1.xls");
      
              try {
                  Workbook wb=Workbook.getWorkbook(f);
                  int Num = wb.getNumberOfSheets();
      
                  for (int i = 0; i < Num; i++) {
                      Sheet sheet = wb.getSheet(i); 
                      Sheet s = wb.getSheet(i);
      
                      int row = s.getRows();
                      int col = s.getColumns();
      
                      for(int i1=0; i1<row;i1++) {
                          for(int j=0;j<col;j++) {
                              Cell c =s.getCell(j, i1);
                              System.out.println(c.getContents());
                              //String text = c.getContents();
                          }
                      }
                  }
              } catch (Exception e) {
                  e.getMessage();
              }
      
              System.out.println("completed");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-14
        • 1970-01-01
        • 2021-09-24
        • 2018-08-14
        相关资源
        最近更新 更多