2016-11-25 457 views
0

我想在执行我的selenium脚本之前使用java清除Excel表格(.xlsx文件)中3列的内容。我正在使用XSSFWorkbook(Apache poi)读取和写入excel表格的数据。excel表格中有5列。我想清除3列内容,剩下的列内容应该像以前一样。如何使用java清除excel表格中的列的内容

请帮助我如何实现这一目标?

回答

0
FileInputStream objFileInputStream = new FileInputStream(new File(
        Xlsx_File_path.xlsx)); 
XSSFWorkbook objWorkbook = new XSSFWorkbook(objFileInputStream); 
XSSFSheet objSheet = objWorkbook.getSheetAt(0); 
Cell cell = null; 
int row=[start of row] 
int cell=[your cell] 


//iterate the below code as you want 
//code 
    cell = objSheet.getRow(rows).getCell(cell); 
    cell.setCellValue("") 
//code 

objFileInputStream.close(); 
FileOutputStream output_file = new FileOutputStream(new File(
        finalXlsx.xlsx)); 
    objWorkbook.write(output_file); 
      output_file.close(); 
+0

感谢@ Kishan..I认为这将需要更多的时间,如果我将清除3列数百data.Because的在我的情况1column有200+ data.Is有任何内置methos在POI到? –

+0

OOXML格式将内容存储在行中,因此对列的任何操作往往都很昂贵,因为您必须迭代所有行。内置的方法不会更有效率。同时决定是否要将文本设置为空字符串,或者您想要完全删除单元格。 – IceArdor