Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.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
如何使用ApachePOI在Java中读取Excel的合并单元格?_Java_Apache_Apache Poi_Cell_Import From Excel - Fatal编程技术网

如何使用ApachePOI在Java中读取Excel的合并单元格?

如何使用ApachePOI在Java中读取Excel的合并单元格?,java,apache,apache-poi,cell,import-from-excel,Java,Apache,Apache Poi,Cell,Import From Excel,我有一个.xlsx格式的Excel文件。我通过合并单元格以形成不同的列来存储数据。我正在通过Java web应用程序读取Excel文件,并将其数据保存到数据库(MySQL)。但是当我从合并的单元格中读取时,我会得到空值以及存储在列和标题中的内容。我正在使用ApachePOI。我的代码是: public static void excelToDBLogIN() { FileInputStream file = null; Boolean flag = true; Arra

我有一个.xlsx格式的Excel文件。我通过合并单元格以形成不同的列来存储数据。我正在通过Java web应用程序读取Excel文件,并将其数据保存到数据库(MySQL)。但是当我从合并的单元格中读取时,我会得到空值以及存储在列和标题中的内容。我正在使用ApachePOI。我的代码是:

public static void excelToDBLogIN() {

    FileInputStream file = null;
    Boolean flag = true;
    ArrayList<String> rows = new ArrayList<String>();
    try {


        // here uploadFolder contains the path to the Login 3.xlsx file

        file = new FileInputStream(new File(uploadFolder + "Login 3.xlsx"));

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);

        //Iterate through each rows one by one
        Iterator<Row> rowIterator = sheet.iterator();


        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            String tuple = "";
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();

                //Check the cell type and format accordingly
                switch (cell.getCellType()) {

                        case Cell.CELL_TYPE_NUMERIC:                            

                        //int value = new BigDecimal(cell.getNumericCellValue()).setScale(0, RoundingMode.HALF_UP).intValue();
                        //tuple = tuple + String.valueOf(value) + "+";

                        DataFormatter objDefaultFormat = new DataFormatter();    

                        String str = objDefaultFormat.formatCellValue(cell);

                        tuple = tuple + str + "+";

                        break;

                    case Cell.CELL_TYPE_STRING:

                        tuple = tuple + cell.getStringCellValue() + "+";

                        break;

                    case Cell.CELL_TYPE_BLANK:                                                        

                        tuple = tuple + "" + "+";

                        break;


                }

            }

            rows.add(tuple);
            flag = true;

        }

    }    


    } catch (Exception e) {

        e.printStackTrace();

    } finally {

        if (file != null) {

            try {
                file.close();
                file = null;
            } catch (Exception e) {

                System.out.println("File closing operation failed");
                e.printStackTrace();
            }
        }                                 

    }

    }

}
publicstaticvoid excelToDBLogIN(){
FileInputStream文件=null;
布尔标志=真;
ArrayList行=新的ArrayList();
试一试{
//此处uploadFolder包含登录3.xlsx文件的路径
file=newfileinputstream(新文件(uploadFolder+“Login 3.xlsx”);
//创建包含对.xlsx文件引用的工作簿实例
XSSF工作簿=新XSSF工作簿(文件);
//从工作簿中获取第一张/所需的工作表
XSSFSheet sheet=workbook.getSheetAt(0);
//逐个遍历每一行
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
//对于每一行,遍历所有列
迭代器cellIterator=row.cellIterator();
字符串元组=”;
while(cellIterator.hasNext()){
Cell=cellIterator.next();
//检查相应的单元格类型和格式
开关(cell.getCellType()){
case Cell.Cell\u类型\u数值:
//int value=new BigDecimal(cell.getNumericCellValue()).setScale(0,舍入模式.HALF_UP).intValue();
//tuple=tuple+String.valueOf(value)+“+”;
DataFormatter objDefaultFormat=新的DataFormatter();
String str=objDefaultFormat.formatCellValue(单元格);
tuple=tuple+str++;
打破
case Cell.Cell\u类型\u字符串:
tuple=tuple+cell.getStringCellValue()+“+”;
打破
case Cell.Cell\u类型\u空白:
元组=元组+“+”+”;
打破
}
}
添加(元组);
flag=true;
}
}    
}捕获(例外e){
e、 printStackTrace();
}最后{
如果(文件!=null){
试一试{
file.close();
file=null;
}捕获(例外e){
System.out.println(“文件关闭操作失败”);
e、 printStackTrace();
}
}                                 
}
}
}

我在网上搜索了答案,但没有找到任何相关信息。

下面的代码片段可能会有所帮助

while (rowIterator.hasNext()) {
        Row row = rowIterator.next();

        //For each row, iterate through all the columns
        Iterator<Cell> cellIterator = row.cellIterator();

        outer:
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();

            //will iterate over the Merged cells
            for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
                CellRangeAddress region = sheet.getMergedRegion(i); //Region of merged cells

                int colIndex = region.getFirstColumn(); //number of columns merged
                int rowNum = region.getFirstRow();      //number of rows merged
                //check first cell of the region
                if (rowNum == cell.getRowIndex() && colIndex == cell.getColumnIndex()) {
                    System.out.println(sheet.getRow(rowNum).getCell(colIndex).getStringCellValue());
                    continue outer;
                }
            }
            //the data in merge cells is always present on the first cell. All other cells(in merged region) are considered blank
            if (cell.getCellType() == Cell.CELL_TYPE_BLANK || cell == null) {
                continue;
            }
            System.out.println(cell.getStringCellValue());
        }
    }
while(roweiterator.hasNext()){
行=行迭代器。下一步();
//对于每一行,遍历所有列
迭代器cellIterator=row.cellIterator();
外部:
while(cellIterator.hasNext()){
Cell=cellIterator.next();
//将迭代合并的单元格
对于(int i=0;i
合并单元格不正确,不允许使用。避开它们。Excel通常将合并区域的内容存储在该区域的左上角单元格中。所有其他单元格都返回0。请尝试此操作,我知道,但excel的格式是由大学各系制作的。我们的项目只是从他们那里获取信息并更新数据库。我个人应该避免这些。@teylyn合并的单元格不好有什么特别的原因吗?@EMM是的。他们把事情搞砸了。合并A3到D3。现在尝试选择C1到C5。或者使用循环将某些内容写入C1到C5。明白为什么不好了吗?谢谢,但我决定在excel中使用未合并的单元格。其他人(包括这里的人)建议不要使用合并单元格excel文件将数据输入应用程序。@Deepika如果文件中没有合并列,代码会工作吗?即使没有合并列,代码也工作正常。但是单元迭代器只获取那些具有非空值的单元。我的要求是迭代每一列,不管它是null还是非null。