Java 如何使用poi读取excel文件中的空单元格,以及如何将此空单元格添加到数组列表中? public void readExcel(字符串文件名) 尝试 { FileInputStream myInput=新的FileInputStream(文件名); POIFSFileSystem myFileSystem=新的POIFSFileSystem(myInput); HSSFWorkbook myWorkBook=新的HSSFWorkbook(myFileSystem); HSSFSheet mySheet=myWorkBook.getSheetAt(0); 迭代器rowIter=mySheet.rowitter(); while(rowIter.hasNext()) { HSSFRow myRow=(HSSFRow)rowIter.next(); 迭代器cellIter=myRow.cellIterator(); ArrayList cellStoreVector=新的ArrayList(); 字符串头\u name=null; while(cellIter.hasNext()) { HSSFCell myCell=(HSSFCell)cellIter.next(); //如果它是excel文件中的空单元格,则不会将其添加到 //数组列表 cellStoreVector.add(myCell); } cellVectorHolder.add(cellStoreVector); } }捕获(例外e) { e、 printStackTrace(); } saveToDatabase(cellVectorHolder); } 公共void saveToDatabase(ArrayList数组) { 对于(int i=0;i
您是否修改或覆盖了Java 如何使用poi读取excel文件中的空单元格,以及如何将此空单元格添加到数组列表中? public void readExcel(字符串文件名) 尝试 { FileInputStream myInput=新的FileInputStream(文件名); POIFSFileSystem myFileSystem=新的POIFSFileSystem(myInput); HSSFWorkbook myWorkBook=新的HSSFWorkbook(myFileSystem); HSSFSheet mySheet=myWorkBook.getSheetAt(0); 迭代器rowIter=mySheet.rowitter(); while(rowIter.hasNext()) { HSSFRow myRow=(HSSFRow)rowIter.next(); 迭代器cellIter=myRow.cellIterator(); ArrayList cellStoreVector=新的ArrayList(); 字符串头\u name=null; while(cellIter.hasNext()) { HSSFCell myCell=(HSSFCell)cellIter.next(); //如果它是excel文件中的空单元格,则不会将其添加到 //数组列表 cellStoreVector.add(myCell); } cellVectorHolder.add(cellStoreVector); } }捕获(例外e) { e、 printStackTrace(); } saveToDatabase(cellVectorHolder); } 公共void saveToDatabase(ArrayList数组) { 对于(int i=0;i,java,apache-poi,Java,Apache Poi,您是否修改或覆盖了HSSFCell?如果没有修改或覆盖,应该可以正常工作。来自HSSFRow#cellIteratorJavaDoc: 请注意,第4个元素可能不是cell 4,因为迭代器不会返回未定义(null)的单元格。对返回的单元格调用getCellNum()以了解它们是哪个单元格 这意味着您必须存储当前和最后一个单元格编号,如果差值大于1,则中间有空白/未定义的单元格,例如int-numBlankCells=(curCellNum-lastCellNum)-1; 另一种方法可能是: if(
HSSFCell
?如果没有修改或覆盖,应该可以正常工作。来自HSSFRow#cellIterator
JavaDoc:
请注意,第4个元素可能不是cell 4,因为迭代器不会返回未定义(null)的单元格。对返回的单元格调用getCellNum()以了解它们是哪个单元格
这意味着您必须存储当前和最后一个单元格编号,如果差值大于1,则中间有空白/未定义的单元格,例如int-numBlankCells=(curCellNum-lastCellNum)-1;
另一种方法可能是:
if(myCell.cellType == NPOI.SS.UserModel.CellType.BLANK)
short minColIndex=row.getFirstCellNum();
short maxColIndex=row.getLastCellNum();
对于(短colIndex=minColIndex;colIndex
List cellDataList=new ArrayList();
int lineNumber=0;
while(roweiterator.hasNext())
{
HSSFRow HSSFRow=(HSSFRow)行迭代器.next();
//System.out.println(“在If之前”);
lineNumber++;
如果(行号==1){continue;}
//System.out.println(“out-side if”);
迭代器迭代器=hssfRow.cellIterator();
List celltemplast=newarraylist();
int current=0,next=1;
while(iterator.hasNext())
{
HSSFCell HSSFCell=(HSSFCell)迭代器.next();
当前=hssfCell.getColumnIndex();
如果(当前请格式化您的代码,将标题改为有用的内容,并将问题的描述从标题改为文本。这样做,您将不会得到任何帮助,因为它完全不可读。我已经编辑了您的格式,但请您下次自己做。否则,人们根本无法帮助您。我不确定是否理解-您ave a void test()返回cellVectorHolder对象。saveToDatabase在哪里调用?如果设置缺少单元格策略,代码可能会更清晰-这样,当单元格不存在时,它会自动创建为空白单元格。
if(myCell.cellType == NPOI.SS.UserModel.CellType.BLANK)
short minColIndex = row.getFirstCellNum();
short maxColIndex = row.getLastCellNum();
for(short colIndex = minColIndex; colIndex < maxColIndex; colIndexx++) {
HSSFCell cell = row.getCell(colIndex);
if(cell == null) {
//blank/undefined cell
}
else {
//defined cell which still could be of type HSSFCell.CELL_TYPE_BLANK or contain an empty string
}
}
List cellDataList = new ArrayList();
int lineNumber = 0;
while (rowIterator.hasNext())
{
HSSFRow hssfRow = (HSSFRow) rowIterator.next();
//System.out.println("Befor If");
lineNumber++;
if(lineNumber==1){continue;}
//System.out.println("Out side if ");
Iterator iterator = hssfRow.cellIterator();
List cellTempList = new ArrayList();
int current = 0, next = 1;
while (iterator.hasNext())
{
HSSFCell hssfCell = (HSSFCell) iterator.next();
current = hssfCell.getColumnIndex();
if(current<next)
{
System.out.println("Condition Satisfied");
}
else
{
int loop = current-next;
System.out.println("inside else Loop value : "+(loop));
for(int k=0;k<loop+1;k++)
{
System.out.println("Adding nulls");
cellTempList.add(null);
next = next + 1;
}
}
cellTempList.add(hssfCell);
next = next + 1;
System.out.println("At End next value is : "+next);
}
cellDataList.add(cellTempList);
}