我想读取一个excel文件,如果有任何数据,那么我想将其设置为null然后开始写入它。这就是我将excel表数据设置为null的方法。我能够成功写入但在写入之前我想设置值null。
FileOutputStream out = null;
try{
FileInputStream file = new FileInputStream(new File("/test.xls"));
HSSFWorkbook oldFile = new HSSFWorkbook(file);
HSSFSheet sheet = oldFile.getSheetAt(0);
file.close();
oldFile.write(null);
outFile.close();
} catch(Exception e){
e.printStackTrace();
}
答案 0 :(得分:2)
一个简单的解决方案是删除并创建sheet
:
File destFile = new File("/test.xls");
FileInputStream fileStream = new FileInputStream(destFile);
POIFSFileSystem fsPoi = new POIFSFileSystem(fileStream);
HSSFWorkbook workbook = new HSSFWorkbook(fsPoi);
int index = 0;
HSSFSheet sheet = workbook.getSheet("Setup");
if(sheet != null) {
index = workbook.getSheetIndex(sheet);
workbook.removeSheetAt(index);
}
workbook.createSheet("Setup");
FileOutputStream output = new FileOutputStream(destFile);
workbook.write(output);
output.close();