我如何读取excel文件并将值设置为null

时间:2016-02-10 14:42:50

标签: java poi-hssf hssf

我想读取一个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();
        }

1 个答案:

答案 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();