java - 使用 Java 永久删除 Excel 空白列

标签 java excel

我的 Excel 工作表如下所示:

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

此处索引[2] 的列的单元格类型为 BLANK。我想删除 CELL_TYPE_BLANK 的列。

我可以找到 CELL_TYPE_BLANK 的列及其索引号,但我找不到从 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();
    }
}

如何删除列?

最佳答案

您不能一次删除一列。您需要删除每一行的一个单元格。

关于java - 使用 Java 永久删除 Excel 空白列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18262448/

相关文章:

java - 判断应用程序是否安装在android上

java - systemd 找不到 Java

vba - Excel-VBA : Inputting range in userform call

excel - 如何使 Excel MIN 函数忽略集合中的零?

excel - TEXT 公式中的日期格式与 Excel 中使用的语言无关

java - 枚举类型中静态 block 的执行顺序 w.r.t 到构造函数

java - 当我在 Netbeans 中按 Enter 时如何自然地中断 {}

excel - Excel 公式,查找最大值并检查多个条件

excel - 如何将 Excel 工作表中的文本和图表复制到 Outlook 正文?

java - 测量圈复杂度