gpt4 book ai didi

apache-poi - Excel 发现无法读取的内容 -POI XSSFWorkbook setcellvalue

转载 作者:行者123 更新时间:2023-12-01 06:36:45 24 4
gpt4 key购买 nike

我正在尝试为 xlsx 文件设置单元格值,程序工作正常,没有错误,但是在打开 xlsx 文件时,它抛出一个错误,说 Excel 发现无法读取的内容

public boolean setCellData(String sheetName,String colName,int rowNum, String data){
try{
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(path);

if(rowNum<=0)
return false;

int index = workbook.getSheetIndex(sheetName);
int colNum=-1;
if(index==-1)
return false;


sheet = workbook.getSheetAt(index);


row=sheet.getRow(0);
for(int i=0;i<row.getLastCellNum();i++){
//System.out.println(row.getCell(i).getStringCellValue().trim());
if(row.getCell(i).getStringCellValue().trim().equals(colName))
colNum=i;
}
if(colNum==-1)
return false;

sheet.autoSizeColumn(colNum);
row = sheet.getRow(rowNum-1);
if (row == null)
row = sheet.createRow(rowNum-1);

cell = row.getCell(colNum);
if (cell == null)
cell = row.createCell(colNum);

// cell style
//CellStyle cs = workbook.createCellStyle();
//cs.setWrapText(true);
//cell.setCellStyle(cs);

cell.setCellValue(data);

fileOut = new FileOutputStream(path);

workbook.write(fileOut);
fileOut.close();

}
catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}

最佳答案

您已初始化 FileInputStream,但未使用它。请更换

fis = new FileInputStream(path); 
workbook = new XSSFWorkbook(path);


fis = new FileInputStream(path); 
workbook = new XSSFWorkbook(fis);

这对我有用,没有显示任何错误。

关于apache-poi - Excel 发现无法读取的内容 -POI XSSFWorkbook setcellvalue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111564/

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