我正在使用Apache POI
将数据导入excel file
到database
.(新手到 APACHE POI)
在其中我允许用户从 excel 表中选择列并将这些列映射到数据库列。映射列后,当我尝试从那时插入记录Excel
时Database
:
- 如果其中包含
NO blank
值的列被映射,则将适当的数据插入数据库 - 如果列映射了其中的
BLANK
值,则如果 aExcel Cell
具有空白值,column
则分配该值的前一个值。
源代码:
FileInputStream file = new FileInputStream(new File("C:/Temp.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file); //Get the workbook instance for XLS file
HSSFSheet sheet = workbook.getSheetAt(0); //Get first sheet from the workbook
Iterator<Row> rowIterator = sheet.iterator(); //Iterate through each rows from first sheet
while (rowIterator.hasNext())
{
HSSFRow hssfRow = (HSSFRow) rowIterator.next();
Iterator<Cell> iterator = hssfRow.cellIterator();
int current = 0, next = 1;
while (iterator.hasNext())
{
HSSFCell hssfCell = (HSSFCell) iterator.next();
current = hssfCell.getColumnIndex();
for(int i=0;i<arrIndex.length;i++) //arrayIndex is array of Excel cell Indexes selected by the user
{
if(arrIndex[i] == hssfCell.getColumnIndex())
{
if(current<next)
{
//System.out.println("Condition Satisfied");
}
else
{
System.out.println( "pstmt.setString("+next+",null);");
pstmt.setString(next,null);
next = next + 1;
}
System.out.println( "pstmt.setString("+next+","+((Object)hssfCell).toString()+");");
pstmt.setString(next,((Object)hssfCell).toString());
next = next + 1;
}
}
}
pstmt.addBatch();
}
我已经在 SO 上寻找类似的问题,但仍然无法解决问题。所以任何帮助将不胜感激。
提前致谢..