I am using apache poi to read form excel file, the code also works without any problem but the code reads data from middle and it does not read from first row of the file.
我正在使用apache poi来读取表单excel文件,代码也可以正常工作,但代码从中间读取数据,而不是从文件的第一行读取。
the code is :
代码是:
package org.sample;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
public class Main {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
//
// An excel file name. You can create a file name with a full
// path information.
//
String fileName = "D:/workspace/ReadExlsx/xlsx/SampleData.xls";
// Create an ArrayList to store the data read from excel sheet.
//
List sheetData = new ArrayList();
FileInputStream fis = null;
try {
//
// Create a FileInputStream that will be use to read the
// excel file.
//
fis = new FileInputStream(fileName);
//
// Create an excel workbook from the file system.
//
HSSFWorkbook workbook = new HSSFWorkbook(fis);
//
// Get the first sheet on the workbook.
//
HSSFSheet sheet = workbook.getSheetAt(0);
//
// When we have a sheet object in hand we can iterator on
// each sheet's rows and on each row's cells. We store the
// data read on an ArrayList so that we can printed the
// content of the excel to the console.
//
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
List data = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
data.add(cell);
}
sheetData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}
showExelData(sheetData);
}
private static void showExelData(List sheetData) {
//
// Iterates the data and print it out to the console.
//
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
//for (int j = 0; j < list.size(); j++) {
HSSFCell employeeid = (HSSFCell) list.get(0);
HSSFCell department = (HSSFCell) list.get(3);
HSSFCell date = (HSSFCell) list.get(5);
System.out.print(employeeid.getRichStringCellValue().getString()+" , ");
System.out.print(department.getRichStringCellValue().getString()+" , ");
System.out.print(date.getRichStringCellValue().getString());
//if (j < list.size() - 1) {
// System.out.print(", ");
//}
// }
System.out.println("");
}
}
}
Please help to solve this.
请帮忙解决这个问题。
2 个解决方案
#1
4
If you have data int I rows and J columns you should try code like this:
如果您有int I行和J列的数据,您应该尝试这样的代码:
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
for (int j = 0; j < list.size(); j++) {
HSSFCell employeeid = (HSSFCell) list.get(j);
System.out.print(employeeid.getRichStringCellValue().getString());
if (j < list.size() - 1) {
System.out.print(", ");
}
}
System.out.println("");
}
for me it worked perfectly. My version of poi is poi-3.8-20120306.
对我来说它完美无缺。我的poi版本是poi-3.8-20120306。
#2
1
// Get the workbook instance for XLS file
//获取XLS文件的工作簿实例
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get first sheet from the workbook
//从工作簿中获取第一张表
XSSFSheet sheet = workbook.getSheetAt(0);
ArrayList<String> columndata = null;
Iterator<Row> rowIterator = sheet.iterator();
columndata = new ArrayList<>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (row.getRowNum() > 0) { // To filter column headings
if (cell.getColumnIndex() == 0) {// To match column
// index
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
columndata.add(cell.getNumericCellValue() + "");
break;
case Cell.CELL_TYPE_STRING:
columndata.add(cell.getStringCellValue());
break;
}
}
}
}
}
#1
4
If you have data int I rows and J columns you should try code like this:
如果您有int I行和J列的数据,您应该尝试这样的代码:
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
for (int j = 0; j < list.size(); j++) {
HSSFCell employeeid = (HSSFCell) list.get(j);
System.out.print(employeeid.getRichStringCellValue().getString());
if (j < list.size() - 1) {
System.out.print(", ");
}
}
System.out.println("");
}
for me it worked perfectly. My version of poi is poi-3.8-20120306.
对我来说它完美无缺。我的poi版本是poi-3.8-20120306。
#2
1
// Get the workbook instance for XLS file
//获取XLS文件的工作簿实例
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get first sheet from the workbook
//从工作簿中获取第一张表
XSSFSheet sheet = workbook.getSheetAt(0);
ArrayList<String> columndata = null;
Iterator<Row> rowIterator = sheet.iterator();
columndata = new ArrayList<>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (row.getRowNum() > 0) { // To filter column headings
if (cell.getColumnIndex() == 0) {// To match column
// index
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
columndata.add(cell.getNumericCellValue() + "");
break;
case Cell.CELL_TYPE_STRING:
columndata.add(cell.getStringCellValue());
break;
}
}
}
}
}