如何从Java中使用Apache POI的Excel合并单元格看?

编程入门 行业动态 更新时间:2024-10-15 10:12:28
本文介绍了如何从Java中使用Apache POI的Excel合并单元格看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对的.xlsx格式的Excel文件。我已经合并单元格,形成各列存储的数据。我通过一个Java Web应用程序读取Excel文件和数据保存到一个数据库(MySQL的)。但是,当我从合并单元格读我得到空值什么都存储在列的还有标头。我使用Apache POI。我的code是:

I have a Excel file in .xlsx format. I have stored data by merging cells to form various columns. I am reading the Excel file via a Java web application and saving its data to a database (MySQL). But when I read from merged cells I get null values along with what are stored in the columns as well as the headers. I am using Apache POI. My code is:

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(); } } } } }

我搜索了在Web答案,但没有找到任何有关。

I searched for answers in the web but did not find anything relevant.

推荐答案

下面的代码片段可能帮助code。

Following code of snippet might help.

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()); } }

更多推荐

如何从Java中使用Apache POI的Excel合并单元格看?

本文发布于:2023-11-30 08:57:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649436.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   Apache   Java   Excel   POI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!