gpt4 book ai didi

java - 如何使用apache poi从excel读取数据并将数据存储到String[][]数组中?

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:49 25 4
gpt4 key购买 nike

我想使用 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();
}
}

最佳答案

尝试使用byteArray

简化示例:

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

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

如果你想使用 string ,很简单

String s = new String(bytes);

关于java - 如何使用apache poi从excel读取数据并将数据存储到String[][]数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43500607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com