gpt4 book ai didi

java - 无法在 Excel 工作表中使用 POI 为空值插入数据

转载 作者:行者123 更新时间:2023-11-29 08:04:42 25 4
gpt4 key购买 nike

我试图在 excel (xlsx) 工作表中找到一个空白值,并使用我的程序和 Apache POI 库替换为一些字符串,如“ENTER VALUE”,如下所示

我能够读取和识别 Excel 工作表中的空白/空值,但无法向这些单元格中插入任何值

public static void main(String args[]) throws IOException, InvalidFormatException
{



FileInputStream inputFile = new FileInputStream("//Users//suk//Documents/tes//testexcel.xlsx");

//now initializing the Workbook with this inputFie


// Create workbook using WorkbookFactory util method

Workbook wb = WorkbookFactory.create(inputFile);

// creating helper for writing cells

CreationHelper createHelper = wb.getCreationHelper();

// setting the workbook to handle null

wb.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);


Sheet sheet = wb.getSheetAt(0);


for (Row row : sheet) {
int lastCellNo =row.getLastCellNum();
int firstCellNo=row.getFirstCellNum();

int rowNo =row.getRowNum();
System.out.println(" row number = "+rowNo);
System.out.println(" last cell no = "+lastCellNo);


for(int i=firstCellNo;i<lastCellNo;i++){
System.out.println("************");

Cell cell = row.getCell(i);
int colIndex =cell.getColumnIndex();
if(cell==null)
{
System.out.println(" The Cell:"+colIndex+" for row "+row.getRowNum()+" is NULL");
}




System.out.println(" column index = "+colIndex);


int cellType = cell.getCellType();
System.out.println(" cell type ="+cellType);

// writing a switch case statement

switch(cellType)
{

case 0:
{
double numValue = cell.getNumericCellValue();
//System.out.println(numValue);
break;
}
case 1:
{
String cellString = cell.getStringCellValue();
System.out.println("----String value = "+cellString+"---String length ="+cellString.length());

// here checking null consition

/* if(cellString == null || cellString.equalsIgnoreCase("") || cellString.length()<=0 || cellString.equalsIgnoreCase(" "))
{
// here creating the cell value after last cell


} */
break;
}
case 4:
{
boolean bolValue =cell.getBooleanCellValue();
//System.out.println(bolValue);
break;
}

// for case of blank cell

case 3:
{

//int lastCellPlus =lastCellNo+1;
//System.out.println("last+1 column ="+lastCellPlus);
//row.createCell(lastCellPlus).setCellValue(" NULL VALUE..PLEASE ENTER VALUE ");

System.out.println(" cell details = "+cell.getColumnIndex()+" row details ="+row.getRowNum());
cell.setCellValue(createHelper.createRichTextString("ENTER VALUE"));
System.out.println(" Sucessfully written error");

break;
}


default:
System.out.println(" unknown format");
break;

}// switch

} // row and cell iterator


}
}

下面是一段代码,我通过它识别它的空白/空值并尝试插入字符串但它没有写入该单元格

case 3:
{

//int lastCellPlus =lastCellNo+1;
//System.out.println("last+1 column ="+lastCellPlus);
//row.createCell(lastCellPlus).setCellValue(" NULL VALUE..PLEASE ENTER VALUE ");

System.out.println(" cell details = "+cell.getColumnIndex()+" row details ="+row.getRowNum());
cell.setCellValue(createHelper.createRichTextString("ENTER VALUE"));
System.out.println(" Sucessfully written error");

break;
}

最佳答案

if(row.getCell(0))==null || getCellValue(row.getCell(0)).trim().isEmpty()){
cell.setCellValue("ENTER VALUE");

private String getCellValue(Cell cell) {
if (cell == null) {
return null;
}
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
return cell.getNumericCellValue() + "";
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
return cell.getBooleanCellValue() + "";
}else if(cell.getCellType() == Cell.CELL_TYPE_BLANK){
return cell.getStringCellValue();
}else if(cell.getCellType() == Cell.CELL_TYPE_ERROR){
return cell.getErrorCellValue() + "";
}
else {
return null;
}
}

关于java - 无法在 Excel 工作表中使用 POI 为空值插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12051777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com