Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

java - Find merged cell in Excel, split the cells and write those in new spreadsheet?

I have been given a Assignment that I need to Split the data of a Spreadsheet and Write it into the new Spreadsheet. The Conditions are, Given Spreadsheet may have multiple numbers of Merged Cells and I need to find those Merged cells and write those Data in a New SpreadSheet. ie, the data or cells between one merged cell till to another Merged cell must be written in another Spreadsheet.

My Code of Effort is given below,

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"+myCell);
                            }System.out.println("Reading Cell value"+myCell);
                        }
                    }
                }
            }
        }

    }  

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

 }}

With this Code, I have Achieved cloning the spreadsheet into another new Sheet. Here, I am failing to find the merged Cell, getMergedCellRegionAt() helps me and returns merged cell region like A:4 D:12 like that. how do I proceed with this. Kindly Help me, your small effort is appreciated. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the Javadocs for HSSFSheet, getMergedCellRegionAt() was deprecated in 2008 because the Region it returns was also deprecated, in favor of CellRangeAddress. It suggests that you should use getMergedRegion(int) instead, which returns a CellRangeAddress.

The merged region data is not stored directly with the cells themselves, but with the Sheet object. So you do not need to loop through rows and cells looking for whether they are part of a merged region; you just need to loop through the list of merged regions on the sheet, then add the merged region to your new sheet with 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);
}

These methods on HSSFSheet are in the Sheet interface, so they will work with any Excel workbook that Apache POI supports, .xls (HSSF) or .xlsx (XSSF).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...