将xlsx文件逐行解析为String [] [英] Parse xlsx file row by row into String[]
问题描述
def Reader = new CSVReader(new FileReader("/Users/me/myfile.csv"));
while ((row = Reader.readNext()) != null) {
我可以使用xlsx文件的相同格式来执行上述操作吗?我已经研究过一些像POI这样的选项,但我不知道如何做到上面的内容。我看到的例子似乎需要额外的代码来检索行中的值。在我目前的情况下,与opencsv,我只是得到一个行,这是一个字符串[],然后我可以像这样使用value = row [index]
How can I do the above with the same format for an xlsx file. I've looked into some options like POI, but i'm not sure how to do exactly what I have above. The examples I saw seemed to require additional code for retrieving the value from a row. In my current case, with opencsv, i just get a row which is a String[] which I can then use like this value = row[index]
推荐答案
您必须使用POI或另一个库来读取excel的文件。
如果您使用POI,实现代码的最佳方式如下所示:
You will have to use POI or another library to read excel's files. If you use POI the best way to implement your code is as follow:
FileInputStream file = new FileInputStream(new File("/Users/me/myfile.xsls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt("name of sheet");
for( Row row : sheet.iterator()) {
//reading new row
Cell cell = row.getCell(<your index here>)
//then you can read cell with cell.getStringCellValue()
}
希望它有帮助
这篇关于将xlsx文件逐行解析为String []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!