【问题标题】:Find merged cell in Excel, split the cells and write those in new spreadsheet?在 Excel 中查找合并的单元格,拆分单元格并将其写入新的电子表格?
【发布时间】:2013-08-30 09:27:39
【问题描述】:

我被分配了一个作业,我需要拆分电子表格的数据并将其写入新的电子表格。条件是,给定电子表格可能有多个合并单元格,我需要找到这些合并单元格并将这些数据写入新电子表格。 即,一个合并单元格与另一个合并单元格之间的数据或单元格必须写入另一个电子表格。

我的努力守则如下,

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class CopyTest {

public static void main(String[] args) throws IOException {
    CopyTest excel = new CopyTest();
    excel.process("D:\\B3.xls");
 }

public void process(String fileName) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
    HSSFWorkbook workbook = new HSSFWorkbook(bis);
    HSSFWorkbook myWorkBook = new HSSFWorkbook();
    HSSFSheet sheet = null;
    HSSFRow row = null;
    HSSFCell cell = null;
    HSSFSheet mySheet = null;
    HSSFRow myRow = null;
    HSSFCell myCell = null;
    int sheets = workbook.getNumberOfSheets();
    int fCell = 0;
    int lCell = 0;
    int fRow = 0;
    int lRow = 0;

    for (int iSheet = 0; iSheet < sheets; iSheet++) {
        sheet = workbook.getSheetAt(iSheet);
        if (sheet != null) {
            mySheet = myWorkBook.createSheet(sheet.getSheetName());

            fRow = sheet.getFirstRowNum();
            System.out.println("First Row at"+fRow);
            lRow = sheet.getLastRowNum();
            for (int iRow = fRow; iRow <= lRow; iRow++) {
                row = sheet.getRow(iRow);
                myRow = mySheet.createRow(iRow);



                if (row != null) {
                    fCell = row.getFirstCellNum();
                    lCell = row.getLastCellNum();
                    for (int iCell = fCell; iCell < lCell; iCell++) {
                        //if (mySheet.getMergedRegionAt(index)!=null)
                       System.out.println("Finding next merged Cells");
                       cell = row.getCell(iCell);
                        myCell = myRow.createCell(iCell);
                        if (cell != null) {
                            myCell.setCellType(cell.getCellType());

                            switch (cell.getCellType()) {
                            case HSSFCell.CELL_TYPE_BLANK:
                                myCell.setCellValue("");
                                break;

                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                myCell.setCellValue(cell.getBooleanCellValue());
                                break;

                            case HSSFCell.CELL_TYPE_ERROR:
                                myCell.setCellErrorValue(cell.getErrorCellValue());
                                break;

                            case HSSFCell.CELL_TYPE_FORMULA:
                                myCell.setCellFormula(cell.getCellFormula());
                                break;

                            case HSSFCell.CELL_TYPE_NUMERIC:
                                myCell.setCellValue(cell.getNumericCellValue());
                                break;

                            case HSSFCell.CELL_TYPE_STRING:
                                myCell.setCellValue(cell.getStringCellValue());
                                break;
                            default:
                                myCell.setCellFormula(cell.getCellFormula());
                               // System.out.println("Reading Cell value\t"+myCell);
                            }System.out.println("Reading Cell value\t"+myCell);
                        }
                    }
                }
            }
        }

    }  

    bis.close();
    BufferedOutputStream bos = new BufferedOutputStream(
            new FileOutputStream("D:\\Result Excel1.xls", true));
    myWorkBook.write(bos);
    bos.close();

 }}

使用此代码,我已将电子表格克隆到另一个新工作表中。在这里,我找不到合并的单元格,getMergedCellRegionAt() 帮助我并返回像 A:4 D:12 这样的合并单元格区域。我该怎么做。请帮助我,感谢您的微小努力。提前致谢。

【问题讨论】:

    标签: java excel apache-poi


    【解决方案1】:

    合并的单元格在第一个单元格中有它们的值。

    以下方法返回区域的值,提供合并区域中第一个单元格的行和列

    String getMergedRegionStringValue(HSSFSheet sheet, int firstRow, int firstColumn){
       for(int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress region = sheet.getMergedRegion(i);
    
            int colIndex = region.getFirstColumn();
            int rowNum = region.getFirstRow();
            //check first cell of the region
            if(rowNum == firstRow && colIndex == firstColumn){  
                return sheet.getRow(rowNum).getCell(colIndex).getStringCellValue();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      rgettmans 的回答是完全正确的。

      Java 8

      我只是想为 Java 8 添加一个带有流的解决方案:

      Sheet oldSheet, newSheet;
      IntStream.range(0, oldSheet.getNumMergedRegions())
                 .mapToObj(oldSheet::getMergedRegion)
                 .forEach(newSheet::addMergedRegion);
      

      【讨论】:

        【解决方案3】:

        根据Javadocs for HSSFSheet,getMergedCellRegionAt() 在 2008 年被弃用,因为它返回的 Region 也被弃用,取而代之的是 CellRangeAddress。它建议您改用getMergedRegion(int),它会返回CellRangeAddress。

        合并的区域数据不直接与单元格本身一起存储,而是与Sheet 对象一起存储。所以你不需要遍历行和单元格来寻找它们是否是合并区域的一部分;您只需要遍历工作表上的合并区域列表,然后使用 addMergedRegion(CellRangeAddress) 将合并区域添加到新工作表。

        for (int i = 0; i < sheet.getNumMergedRegions(); i++)
        {
            CellRangeAddress mergedRegion = sheet.getMergedRegion(i);
            // Just add it to the sheet on the new workbook.
            mySheet.addMergedRegion(mergedRegion);
        }
        

        HSSFSheet 上的这些方法位于 Sheet 接口中,因此它们适用于 Apache POI 支持的任何 Excel 工作簿、.xls (HSSF) 或 .xlsx (XSSF)。

        【讨论】:

        • 正确答案。只是为了使答案完整:也有可能使用getMergedRegions(),它返回CellRangeAddress的列表。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-24
        • 1970-01-01
        • 2015-10-12
        • 1970-01-01
        相关资源
        最近更新 更多