我有一个现有的excel工作表,我正在尝试阅读它并将其放在我的列表中。但是,这里有合并的单元格,所以我想先取消合并,然后再将其存储在列表中。我尝试了removeMergedRegion()方法,但没有成功。当我在完成该过程后打开Excel工作表时,我希望将单元格合并。并且如果可能,在取消合并之后,应该删除空白单元格。
public void newone(){
//calling my existing excel file
File file = new File("C:\\Users\\daisy\\Downloads\\What.xlsx");
FileInputStream fis;
try {
fis = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(fis);
// Iterate through all merged region in the sheet
Sheet sheet = workbook.getSheet("Sheet1");
for(int i=0; i < sheet.getNumMergedRegions(); i++)
{
// Delete the region
sheet.removeMergedRegion(i);
System.out.println("Unmerged successfully");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:0)
我认为问题是,您所做的更改仅在内存中完成,没有保存。您需要保存修改后的图纸。为了证明我是从另一个问题Save Excel document Apache POI
借来的代码try {
FileOutputStream out = new FileOutputStream("C:\\Users\\...\\New.xlsx");
workbook.write(out);
out.close();
} catch (FileNotFoundException ex) {
// handle the exception here
}
离题-我强烈建议您不要吞下catch(e) { e.printStackTrace(); }
的异常。有很多文章解释了为什么这是一种不好的做法。