如何使用apache poi编写.xlsx文件
请看这是我要写入 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正在创建一个工作簿,填充它,然后创建一个新的空工作簿并保存!尝试删除该行
然后将写入更改为从 workBook 而不是 newWorkBook 写入,应该没问题。
You appear to be creating a workbook, populating it, then creating a new empty one and saving that! Try removing the line
And then change the write to be a write from workBook rather than newWorkBook and you should be fine.