Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用ApachePOI处理excel文件中的空单元格_Java_Apache Poi - Fatal编程技术网

Java 如何使用ApachePOI处理excel文件中的空单元格

Java 如何使用ApachePOI处理excel文件中的空单元格,java,apache-poi,Java,Apache Poi,我目前正在研究将数据从一个excel工作表复制到另一个工作簿的概念,如果存在空白单元格,则应将其复制到输出文件中。下面是输入文件的屏幕截图: 这是我执行复制功能的代码 import org.apache.poi.*; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apac

我目前正在研究将数据从一个excel工作表复制到另一个工作簿的概念,如果存在空白单元格,则应将其复制到输出文件中。下面是输入文件的屏幕截图:

这是我执行复制功能的代码

import org.apache.poi.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.examples.CreateCell;

import java.io.*;
import java.util.*;
public class openwb_test {


    public static void main(String[] args) throws Exception {

        File inputFile=new File("input.xlsx");
        FileInputStream fis=new FileInputStream(inputFile);
        XSSFWorkbook inputWorkbook=new XSSFWorkbook(fis);
        int inputSheetCount=inputWorkbook.getNumberOfSheets();
        System.out.println("Input sheetCount: "+inputSheetCount);


        File outputFile=new File("output.xlsx");
        FileOutputStream fos=new FileOutputStream(outputFile);


        XSSFWorkbook outputWorkbook=new XSSFWorkbook();


        for(int i=0;i<inputSheetCount;i++) 
        { 
            XSSFSheet inputSheet=inputWorkbook.getSheetAt(i); 
            String inputSheetName=inputWorkbook.getSheetName(i); 
            XSSFSheet outputSheet=outputWorkbook.createSheet(inputSheetName); 


            copySheet(inputSheet,outputSheet); 
        }


        outputWorkbook.write(fos); 

        fos.close(); 

        outputWorkbook.close();
    }

    public static void copySheet(XSSFSheet inputSheet,XSSFSheet outputSheet) 
    { 
        int rowCount=inputSheet.getLastRowNum(); 
        System.out.println(rowCount+" rows in inputsheet "+inputSheet.getSheetName()); 

        int currentRowIndex=0; if(rowCount>0)
        {
            Iterator<Row> rowIterator=inputSheet.iterator();
            //XSSFRow row=(XSSFRow) rowIterator.next();
            while(rowIterator.hasNext())
            {
                int currentCellIndex=0;
                Iterator<Cell> cellIterator=((XSSFRow) rowIterator.next()).cellIterator();
                while(cellIterator.hasNext())
                {

                    String cellData=cellIterator.next().toString();


                    if(currentCellIndex==0 )
                        outputSheet.createRow(currentRowIndex).createCell(currentCellIndex).setCellValue(cellData);
                    else
                        outputSheet.getRow(currentRowIndex).createCell(currentCellIndex).setCellValue(cellData);



                    currentCellIndex++; 
                } 

                currentRowIndex++;
                System.out.println(currentRowIndex);
            }
            System.out.println((currentRowIndex-1)+" rows added to outputsheet "+outputSheet.getSheetName());
            System.out.println();
        }
    }




}
import org.apache.poi.*;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellType;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.xssf.usermodel.XSSFCell;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.apache.poi.xssf.usermodel.examples.CreateCell;
导入java.io.*;
导入java.util.*;
公共类openwb_测试{
公共静态void main(字符串[]args)引发异常{
File inputFile=新文件(“input.xlsx”);
FileInputStream fis=新的FileInputStream(inputFile);
XSSF工作簿输入工作簿=新XSSF工作簿(fis);
int inputSheetCount=inputWorkbook.getNumberOfSheets();
System.out.println(“输入纸张计数:+inputSheetCount”);
File outputFile=新文件(“output.xlsx”);
FileOutputStream fos=新的FileOutputStream(outputFile);
XSSFWorkbook outputWorkbook=新XSSFWorkbook();
对于(int i=0;i0)
{
迭代器rowIterator=inputSheet.Iterator();
//XSSFRow行=(XSSFRow)行迭代器.next();
while(roweiterator.hasNext())
{
int currentCellIndex=0;
迭代器cellIterator=((XSSFRow)rowIterator.next()).cellIterator();
while(cellIterator.hasNext())
{
字符串cellData=cellIterator.next().toString();
如果(currentCellIndex==0)
outputSheet.createRow(currentRowIndex).createCell(currentCellIndex).setCellValue(cellData);
其他的
outputSheet.getRow(currentRowIndex).createCell(currentCellIndex).setCellValue(cellData);
currentCellIndex++;
} 
currentRowIndex++;
System.out.println(currentRowIndex);
}
System.out.println((currentRowIndex-1)+“添加到outputsheet的行”+outputsheet.getSheetName());
System.out.println();
}
}
}

但是如果我运行上面的代码,结果将被复制,但是无论空白单元格存在哪里,代码都会被修剪掉。 例如,下面是输出剪报:


<> P>是否有人建议我如何处理这个场景中的空白单元格,因为我的期望是输入文件数据应该被复制,因为它包括空白单元格到输出文件

。GETCCEL方法有一个第二个参数,它指定一个处理空单元格的策略。 在良好的ol'for循环中使用它:

    for (int i=0; i<row.getLastCellNum(); i++) {
        Cell cell = row.getCell(i, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
        if (cell == null) {
            // ...
        } else {
            // ...
        }
    }

for(int i=0;ii如果我是你,我将首先将数据存储到arraylist中,然后将其输出到输出文件。通过这种方式,你可以更正确地处理空单元格。你仍然会遇到跳过空行的问题,例如,在使用sheet.rowIterator()时。为了避免这种情况,请使用行号遍历行并测试null。