使用Java永久删除Excel空白列

我的Excel表格如下所示:

Column Index: [0] [1] [2] [3] Column Name : Name Password Address XYZ 111 Delhi ABC 222 Chennai 

这里的列在索引[2]的单元格types是BLANK.I我想删除CELL_TYPE_BLANK的列。

我可以findCELL_TYPE_BLANK的列和它们的索引号,但是我没有find从Excel工作表中永久删除这些列的方法。如何编写符合我的要求的代码?

删除CELL_TYPE_BLANK列后,我的输出Excel表格应如下所示。

 Column Index: [0] [1] [2] Column Name : Name Password Address XYZ 111 Delhi ABC 222 Chennai import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.usermodel.Workbook; public class DeleteColumn { public static void main(String[] args) throws InvalidFormatException, FileNotFoundException, IOException { Workbook wb = WorkbookFactory.create(new FileInputStream("Delete_Columns.xls")); Sheet sheet = wb.getSheetAt(0); Cell cell; Column column; Row row= sheet.getRow(0); int numXolumns = row.getLastCellNum(); for(int col=0; col< numXolumns; col++) { cell = row.getCell(col); int type = cell.getCellType(); if (type == HSSFCell.CELL_TYPE_BLANK) { System.out.println("[" + cell.getRowIndex() + ", "+ cell.getColumnIndex() + "] = BLANK CELL"+ cell.toString()); int Delete_column_index=cell.getColumnIndex(); } } FileOutputStream fileOut = new FileOutputStream("Output_Excel.xls"); wb.write(fileOut); fileOut.close(); } } 

我实施删除行的代码是

 public static void removeRow() { try { int getLastCell=row.getLastCellNum()-1; int lastIndex = sheet.getLastRowNum(); for (int i=0; i<=lastIndex; i++) { row=sheet.getRow(i); if(row.getCell(getLastCell)!=null && ((row.getCell(getLastCell).toString().equalsIgnoreCase("Not valid for this App Type".trim()))|| (row.getCell(getLastCell).toString().equalsIgnoreCase("Not Applicable".trim())))) { sheet.shiftRows(i+1, lastIndex, -1); i--; } } } catch(NullPointerException e) { e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } } 

如何删除列?

您无法一次删除一列。 您需要删除每一行上的一个单元格。