【问题标题】:How to read data from excel using apache poi and store the data into String[][] array?如何使用 apache poi 从 excel 中读取数据并将数据存储到 String[][] 数组中?
【发布时间】:2017-04-20 00:17:31
【问题描述】:

我想使用 apache poi 从 excel 中读取数据并将该数据存储到二维字符串数组中。使用下面的代码,我将显示数据,但我想存储数据。

public static void main(String[] args) throws Exception {

    File f = new File("C:/Users/SYKAMREDDY/Desktop/testData.xls");
    FileInputStream fis = new FileInputStream(f);

    HSSFWorkbook wb = new HSSFWorkbook(fis);
    HSSFSheet sh = wb.getSheet("Data");
    int rc=sh.getLastRowNum()-sh.getFirstRowNum();

    for (int i = 1; i < rc; i++) {
        Row r = sh.getRow(i);
        for (int j = 1; j < r.getLastCellNum(); j++) {
            String s = r.getCell(j).getStringCellValue();
            System.out.print(s+" ");
        }
        System.out.println();
    }
}

【问题讨论】:

  • 您面临的问题是什么?如果是,数据是否被成功读取,您希望达到什么目的?

标签: java excel


【解决方案1】:

尝试使用字节数组

简化示例:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
    workbook.write(bos);
} finally {
    bos.close();
}
byte[] bytes = bos.toByteArray();

另外,看看How can I convert POI HSSFWorkbook to bytes?

如果你想使用字符串,简单做

String s = new String(bytes);

【讨论】:

  • 据我了解问题标题,提问者希望将单元格数据存储在string[][] 数组中。您建议将工作簿保存在 byte[] 数组中。所以这不是问题的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多