cn.fql.template.poi.PoiTest.java Source code

Java tutorial

Introduction

Here is the source code for cn.fql.template.poi.PoiTest.java

Source

/*
 * File: $RCSfile$
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */
package cn.fql.template.poi;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.util.Region;

import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;

/**
 * The class <code>cn.fql.template.poi.PoiTest</code>
 *
 * @author User, WN ASP SSD
 * @version $Revision$
 */
public class PoiTest {
    private static HSSFWorkbook templateWbk;
    private static HSSFCellStyle percentageStyle;

    public static void main(String[] args) {
        writeInvoice("D://do.xls", "D://2.xls");
    }

    private static void writeInvoice(String inputFile, String outputFile) {
        FileOutputStream oos = null;
        FileInputStream fis = null;
        try {
            oos = new FileOutputStream(outputFile);
            fis = new FileInputStream(inputFile);
            templateWbk = new HSSFWorkbook(fis);
            writeEffort(2, "report");
            writeEffort(3, "report");
            writeEffort(4, "report");
            writeEffort(5, "report");
            templateWbk.write(oos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void writeEffort(int index, String sheetName) {
        HSSFSheet templateSheet = templateWbk.getSheet(sheetName);
        String lastCellValue = null;
        for (int i = 1118; i < 1232; i++) {
            HSSFRow row = templateSheet.getRow(i);
            if (row == null) {
                if (templateSheet.getRow(i + 1) == null) {
                    break;
                }
            } else {
                HSSFCell cell = row.getCell((short) index);
                if (lastCellValue == null) {
                    lastCellValue = cell.getRichStringCellValue().getString();
                } else {
                    String newCellValue = cell.getRichStringCellValue().getString();
                    if (cell != null) {
                        if (lastCellValue.equals(newCellValue)) {
                            Region region = new Region();
                            region.setRowTo(i);
                            region.setRowFrom(i - 1);
                            region.setColumnFrom((short) index);
                            region.setColumnTo((short) index);
                            templateSheet.addMergedRegion(region);
                        } else {
                            lastCellValue = newCellValue;
                        }
                    }
                }
            }
        }
    }
}
/**
 * History:
 *
 * $Log$
 */