com.mto.excel.model.MergeWorkbook.java Source code

Java tutorial

Introduction

Here is the source code for com.mto.excel.model.MergeWorkbook.java

Source

/*
 * Copyright (C) 2012 eXo Platform SAS.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.mto.excel.model;

import org.apache.poi.ss.usermodel.Cell;
import static org.apache.poi.ss.usermodel.Cell.*;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author <a href="hoang281283@gmail.com">Minh Hoang TO</a>
 * @date 1/17/12
 */
public class MergeWorkbook {
    private Workbook workbook;

    private Sheet sheet;

    private CreationHelper helper;

    private int rowOffset;

    public MergeWorkbook(Workbook workbook, int rowOffset) {
        this.workbook = workbook;
        this.sheet = workbook.createSheet("Merge");
        this.helper = workbook.getCreationHelper();

        this.rowOffset = rowOffset;
    }

    public void mergeUnit(MergeUnit unit, MergeContext context) {
        Sheet firstSheet = unit.getFirstSheet();
        boolean addRow = false;
        int cellOffset = 0;

        boolean startRowFound = false;
        boolean endRowFound = false;

        for (Row row : firstSheet) {
            if (addRow) {
                if (new RowMatcher(context.endRowPattern, row).find() > 0) {
                    endRowFound = true;
                    break;
                } else {
                    addRow(row, cellOffset);
                }
            } else {
                int i = new RowMatcher(context.startRowPattern, row).find();
                if (i >= 0) {
                    addRow = true;
                    startRowFound = true;
                    cellOffset = i;
                }
            }
        }

        if (startRowFound && endRowFound) {
            //TODO: Use log here
            System.out.println("INFO: Merge successfully content in file: " + unit.getFileName());
        } else {
            System.out.println("ERROR: Could not process correctly the file: " + unit.getFileName()
                    + " , please check it manually");
        }
    }

    private void addRow(Row row, int cellOffset) {
        Row newRow = sheet.createRow(rowOffset);
        rowOffset++;
        for (int i = cellOffset; i < row.getLastCellNum(); i++) {
            Cell c = row.getCell(i);
            if (c == null) {
                continue;
            }

            Cell newCell = newRow.createCell(i, c.getCellType());

            switch (c.getCellType()) {
            case CELL_TYPE_STRING:
                newCell.setCellValue(helper.createRichTextString(c.getStringCellValue()));
                break;
            case CELL_TYPE_BOOLEAN:
                newCell.setCellValue(c.getBooleanCellValue());
                break;
            case CELL_TYPE_NUMERIC:
                newCell.setCellValue(c.getNumericCellValue());
                break;
            default:
                return;
            }
        }
    }

    public void persist(OutputStream out) {
        try {
            workbook.write(out);
        } catch (IOException ioex) {
        } finally {
            try {
                out.close();
            } catch (IOException ioex) {
            }
        }
    }
}