如何使用apache poi编写.xlsx文件

发布于 2024-11-03 11:10:50 字数 1720 浏览 0 评论 0原文

请看这是我要写入 .xlsx 文件的编码,它正在工作,但写入后我无法打开文件。它说文件已损坏。请给出解决方案

OPCPackage fileSystems = OPCPackage.open(file.getAbsolutePath(),PackageAccess.READ);

XSSFWorkbook workBook = new XSSFWorkbook(fileSystems);
XSSFSheet sheet = workBook.getSheetAt(0);
Iterator rows = sheet.rowIterator();

while (rows.hasNext ())
{
XSSFRow row = (XSSFRow) rows.next ();

System.out.println ("Row No.: " + row.getRowNum ());

Iterator cells = row.cellIterator();

while (cells.hasNext ())
{
XSSFCell cell = (XSSFCell) cells.next();

String value = "OldValue";
if(value.equals(cell.getStringCellValue()))
{
switch (cell.getCellType())
{ 
case Cell.CELL_TYPE_NUMERIC: 
double cellNumericValue =  cell.getNumericCellValue(); 
cell.setCellValue(cellNumericValue); 
break; 
case Cell.CELL_TYPE_STRING: 
String cellStringValue = cell.getStringCellValue(); 
cell.setCellValue("NewValue"); 
break; 
case Cell.CELL_TYPE_FORMULA: 
String cellFormulaValue = cell.getCellFormula(); 
cell.setCellValue(cellFormulaValue); 
break; 
case Cell.CELL_TYPE_BLANK: 
cell.setCellValue("");
break; 
case Cell.CELL_TYPE_BOOLEAN: 
boolean cellBooleanValue = cell.getBooleanCellValue();
cellBooleanValue=false; 
cell.setCellValue(cellBooleanValue); 
break; 
case Cell.CELL_TYPE_ERROR: 
byte error = cell.getErrorCellValue();
cell.setCellValue(error);
break; 
default: 
break; 
} 
}
}
}
XSSFWorkbook newWorkBook = new XSSFWorkbook();
FileOutputStream outPutStream = null;
try {
outPutStream = new FileOutputStream(file);
newWorkBook.write(outPutStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outPutStream != null) {
try {
outPutStream.flush();
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

please see this is my coding about to write .xlsx file it is working but i can't open file after wrote. it says file is corrupted. please give solution

OPCPackage fileSystems = OPCPackage.open(file.getAbsolutePath(),PackageAccess.READ);

XSSFWorkbook workBook = new XSSFWorkbook(fileSystems);
XSSFSheet sheet = workBook.getSheetAt(0);
Iterator rows = sheet.rowIterator();

while (rows.hasNext ())
{
XSSFRow row = (XSSFRow) rows.next ();

System.out.println ("Row No.: " + row.getRowNum ());

Iterator cells = row.cellIterator();

while (cells.hasNext ())
{
XSSFCell cell = (XSSFCell) cells.next();

String value = "OldValue";
if(value.equals(cell.getStringCellValue()))
{
switch (cell.getCellType())
{ 
case Cell.CELL_TYPE_NUMERIC: 
double cellNumericValue =  cell.getNumericCellValue(); 
cell.setCellValue(cellNumericValue); 
break; 
case Cell.CELL_TYPE_STRING: 
String cellStringValue = cell.getStringCellValue(); 
cell.setCellValue("NewValue"); 
break; 
case Cell.CELL_TYPE_FORMULA: 
String cellFormulaValue = cell.getCellFormula(); 
cell.setCellValue(cellFormulaValue); 
break; 
case Cell.CELL_TYPE_BLANK: 
cell.setCellValue("");
break; 
case Cell.CELL_TYPE_BOOLEAN: 
boolean cellBooleanValue = cell.getBooleanCellValue();
cellBooleanValue=false; 
cell.setCellValue(cellBooleanValue); 
break; 
case Cell.CELL_TYPE_ERROR: 
byte error = cell.getErrorCellValue();
cell.setCellValue(error);
break; 
default: 
break; 
} 
}
}
}
XSSFWorkbook newWorkBook = new XSSFWorkbook();
FileOutputStream outPutStream = null;
try {
outPutStream = new FileOutputStream(file);
newWorkBook.write(outPutStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outPutStream != null) {
try {
outPutStream.flush();
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下伊人醉 2024-11-10 11:10:50

您似乎正在创建一个工作簿,填充它,然后创建一个新的空工作簿并保存!尝试删除该行

  XSSFWorkbook newWorkBook = new XSSFWorkbook();

然后将写入更改为从 workBook 而不是 newWorkBook 写入,应该没问题。

You appear to be creating a workbook, populating it, then creating a new empty one and saving that! Try removing the line

  XSSFWorkbook newWorkBook = new XSSFWorkbook();

And then change the write to be a write from workBook rather than newWorkBook and you should be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文