Parse xlsx file row by row into String[]

谁都会走 提交于 2019-12-25 03:14:09

问题


def Reader = new CSVReader(new FileReader("/Users/me/myfile.csv"));
while ((row = Reader.readNext()) != null) {   

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]


回答1:


Xslx is a binary format so you can't read it as it were plain text and splitting on something.

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()
}

Hope it helps



来源:https://stackoverflow.com/questions/17053802/parse-xlsx-file-row-by-row-into-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!