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

How I can read excel file and set value to null

我想读取一个 excel 文件,如果有任何数据,我想将其设置为 null 然后开始写入。这就是我将 excel sheet 数据设置为空的原因。我能够成功写入但在写入之前我想将值设置为空。

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();
        }

一个简单的解决方案是删除并创建 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();