如何使用java和apache poi删除excel中的空单元格

如何使用java和apache poi删除excel中的空单元格,java,excel,apache-poi,Java,Excel,Apache Poi,使用ApachePOI,我从一个Excel文件中提取值并写入另一个Excel文件 但是,我在输出Excel文件中得到了空单元格。我如何: 在末尾检测哪些行为空 然后删除这些行 我添加了这部分代码以删除行。我能够检测到空的行,但无法删除它 Please tell how to implement in the above code I added. 我添加了这个 public class Nlp_health { private static boolean isRowEmpty; pub

使用ApachePOI,我从一个Excel文件中提取值并写入另一个Excel文件

但是,我在输出Excel文件中得到了空单元格。我如何:

  • 在末尾检测哪些行为空
  • 然后删除这些行
我添加了这部分代码以删除行。我能够检测到空的行,但无法删除它

Please tell how to implement in the above code I added.
我添加了这个

public class Nlp_health {
private static boolean isRowEmpty;

public static void main(String[] args) throws IOException, ParseException, RowsExceededException, WriteException
{
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet1 = wb.createSheet("Sheet1");
    XSSFSheet sheet2 = wb.createSheet("Sheet2");
    XSSFSheet sheet3 = wb.createSheet("Sheet3");
    XSSFRow row_1 = sheet1.createRow((char)0);
    XSSFRow row_2 = sheet2.createRow((char)0);
    XSSFRow row_3 = sheet3.createRow((char)0);
    row_1.createCell(0).setCellValue("Column1");
    row_1.createCell(1).setCellValue("Column2");

    row_2.createCell(0).setCellValue("Column1");
    row_2.createCell(1).setCellValue("Column2");

    row_3.createCell(0).setCellValue("Column1");
    row_3.createCell(1).setCellValue("Column2");

    FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Master\\Desktop\\Nlp_health3.xlsx");
    wb.write(fileOut);
    fileOut.close();

Xls_Reader datatable = new Xls_Reader("C:\\Users\\Master\\Desktop\\SMB NLP Projects to Update 20140722.xlsx");
Xls_Reader datatable1 = new Xls_Reader("C:\\Users\\Master\\Desktop\\Nlp_health.xlsx");
int rows_count = datatable.getRowCount("Projects View");
System.out.println("Total number of rows = " + rows_count);
System.out.println();
for(int i=4; i<rows_count;i++)
{
    String milestone = datatable.getCellData("Projects View", "SMB Project Health", i);
    String project = datatable.getCellData("Projects View", "Name", i);
{
    //System.out.println(value);
    if (milestone.equals("Yellow")){
        System.out.println("1st step = " + project);

          datatable1.setCellData("Sheet1", "Column1", i, project);
          datatable1.setCellData("Sheet1", "Column2", i, "Yellow");
          //System.out.println(project);
    }
    if(milestone.equals("Red")){
        System.out.println("2nd step = " + project);
        datatable1.setCellData("Sheet1", "Column2", i, "Red");
        datatable1.setCellData("Sheet1", "Column1", i, project);

    }

    }
}

int rows_count_new = datatable1.getRowCount("Sheet1");
System.out.println("Number of rows are" + rows_count_new );
for (int j = 0; j <= rows_count_new; j++){
    String empty_cells = datatable1.getCellData("Sheet1", "Column1", j);
    System.out.println("Empty step1 = " + empty_cells);
     if(sheet1.getRow(j)==null){
            sheet1.shiftRows(j + 1, sheet1.getLastRowNum(), -1);
            j--;
        continue;
        }
        for(int k =0; k<sheet1.getRow(j).getLastCellNum();k++){
            if(sheet1.getRow(j).getCell(k).toString().trim().equals("")){
                isRowEmpty=true;
            }else {
                isRowEmpty=false;
                break;
            }
        }
        if(isRowEmpty==true){
            sheet1.shiftRows(j + 1, sheet1.getLastRowNum(), -1);
            j--;
        }


}





}
公共类Nlp\U健康{
私有静态布尔值为空;
publicstaticvoidmain(字符串[]args)抛出IOException、ParseException、rowseceededexception、WriteException
{
XSSF工作簿wb=新XSSF工作簿();
XSSFSheet sheet1=wb.createSheet(“sheet1”);
XSSFSheet sheet2=wb.createSheet(“sheet2”);
XSSFSheet sheet3=wb.createSheet(“sheet3”);
XSSFRow row_1=sheet1.createRow((char)0);
XSSFRow row_2=sheet2.createRow((char)0);
XSSFRow row_3=sheet3.createRow((char)0);
行1.createCell(0.setCellValue(“Column1”);
行1.createCell(1.setCellValue(“Column2”);
行2.createCell(0.setCellValue(“Column1”);
行2.createCell(1.setCellValue(“Column2”);
第3行。createCell(0)。setCellValue(“Column1”);
第3行。createCell(1)。setCellValue(“第2列”);
FileOutputStream fileOut=新的FileOutputStream(“C:\\Users\\Master\\Desktop\\Nlp\u health3.xlsx”);
wb.写入(文件输出);
fileOut.close();
Xls_Reader datatable=新的Xls_Reader(“C:\\Users\\Master\\Desktop\\SMB NLP项目更新20140722.xlsx”);
Xls_阅读器datatable1=新的Xls_阅读器(“C:\\Users\\Master\\Desktop\\Nlp_health.xlsx”);
int rows_count=datatable.getRowCount(“项目视图”);
System.out.println(“总行数=”+行数);
System.out.println();

对于(int i=4;iPOI Javadoc清晰地文档,它将从工作表中删除整行


还有
XSSFRow#removeCell(cell)
删除单个单元格。

POI Javadoc清楚地记录了文档,这将从工作表中删除整行


还有
XSSFRow#removeCell(cell)
删除单个单元格。

假设:根据您的问题,您似乎担心的是空白行,而不是一行中的空白单元格并非所有单元格都为空

可以通过两种方式找到空行

  • 首先,该行位于其他行之间,但从未初始化或创建。在这种情况下,Sheet.getRow(i)将为空。
  • 第二,行已创建,其单元格可能会被使用,也可能不会被使用,但现在它的所有单元格都为空。在这种情况下,Sheet.getRow(i)将不会为null(您可以使用Sheet.getRow(i.getLastCellNum()检查它将始终显示与其他行相同的计数。)
  • 一般情况下会出现第二个条件。可能在您的情况下,这应该是原因。为此,您需要添加附加条件以检查所有单元格是否为空

        for(int i = 0; i < sheet.getLastRowNum(); i++){
            if(sheet.getRow(i)==null){
               // condition of blank row so do whatever you want to do in this case
               // for example I have done nothing just removed that row.
                sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
                i--;
            continue;
            }
            for(int j =0; j<sheet.getRow(i).getLastCellNum();j++){
                if(sheet.getRow(i).getCell(j).toString().trim().equals("")){
                    isRowEmpty=true;
                }else {
                    isRowEmpty=false;
                    break;
                }
            }
            if(isRowEmpty==true){
             // condition of blank row so do whatever you want to do in this case
             // for example I have done nothing just removed that row.
                sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
                i--;
            }
        }
    
    for(int i=0;i对于(int j=0;j假设:根据您的问题,您似乎担心的是空白行,而不是一行中的空白单元格并非所有单元格都为空白

    可以通过两种方式找到空行

  • 首先,该行位于其他行之间,但从未初始化或创建。在这种情况下,Sheet.getRow(i)将为空。
  • 第二,行已创建,其单元格可能会被使用,也可能不会被使用,但现在它的所有单元格都为空。在这种情况下,Sheet.getRow(i)将不会为null(您可以使用Sheet.getRow(i.getLastCellNum()检查它将始终显示与其他行相同的计数。)
  • 一般情况下会出现第二个条件。可能在您的情况下,这应该是原因。为此,您需要添加附加条件以检查所有单元格是否为空

        for(int i = 0; i < sheet.getLastRowNum(); i++){
            if(sheet.getRow(i)==null){
               // condition of blank row so do whatever you want to do in this case
               // for example I have done nothing just removed that row.
                sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
                i--;
            continue;
            }
            for(int j =0; j<sheet.getRow(i).getLastCellNum();j++){
                if(sheet.getRow(i).getCell(j).toString().trim().equals("")){
                    isRowEmpty=true;
                }else {
                    isRowEmpty=false;
                    break;
                }
            }
            if(isRowEmpty==true){
             // condition of blank row so do whatever you want to do in this case
             // for example I have done nothing just removed that row.
                sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
                i--;
            }
        }
    
    for(int i=0;i对于(int j=0;jAre您是在问一般如何删除行?还是如何检测哪些行是空的,以便您可以删除它们?是的,一旦我在第二个excel文件中获得输出,首先我需要在末尾检测哪些行是空的,然后我需要删除这些行或单元格。然后在您的问题中这样说,并省去所有额外的内容ous详细信息。这一次是为你做的。你是在问一般如何删除行?还是如何检测哪些行是空的,这样你就可以删除它们?是的,一旦我在第二个excel文件中得到输出,首先我需要在最后检测哪些行是空的,然后我需要删除这些行或单元格。然后在你的问题中这样说,然后省去所有无关的细节。这次是为你做的。你能举个例子吗,不能实现这个。你能举个例子吗,不能实现这个。得到这个错误:线程“main”中出现异常java.lang.IllegalArgumentException:firstMovedIndex,lastMovedIndex无序行计数是基于零索引的…可能您指出了错误的索引号。请检查您的索引号。获取此错误:线程“main”中出现异常java.lang.IllegalArgumentException:firstMovedIndex,lastMovedIndex无序行计数是基于零索引的…可能是您指出了错误的索引号。请检查您的索引号。