码迷,mamicode.com
首页 > 其他好文 > 详细

一个ExcelUtils

时间:2015-03-19 11:33:59      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:

工作需要下载excel文件,使用poi做了一个ExcelUtils,是一个用来excel生成工具,功能比较基础,但也够用了,代码如下

package com.hxt.common.internal.utils;

import java.util.Collection;

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;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.WorkbookUtil;
import org.springframework.util.StringUtils;

import com.hxt.common.internal.exception.BlankArgumentException;

public class ExcelUtils {

    /**
     * 创建workbook对象
     * 
     * @param workbookName
     *            工作薄名称
     * @param headers
     *            表头
     * @param datas
     *            表数据
     * @return Workbook 用headers和datas填充后的表格文件
     * @throws BlankArgumentException
     */

    public static Workbook createExcelFile(String workbookName, String[] headers, Collection<String[]> datas) throws BlankArgumentException {

        if (StringUtils.isEmpty(workbookName)) {
            throw new BlankArgumentException("outFilename is blank");
        }
        Workbook wb = new HSSFWorkbook();
        HSSFSheet sheet = (HSSFSheet) wb.createSheet(WorkbookUtil.createSafeSheetName(workbookName));
        createHeaderRow(sheet, headers);
        fillDatas(sheet, datas);
        return wb;
    }

    public static void createExcelFile(Workbook wb, String settleBatchNumber, String[] settleHeaders, String[] transHeaders, int startIndex,
            String[] settleData,
            Collection<String[]> transDatas) {
        HSSFSheet sheet = (HSSFSheet) wb.createSheet(WorkbookUtil.createSafeSheetName(settleBatchNumber));
        createHeaderRow(sheet, settleHeaders, startIndex);
        fillDatas(sheet, settleData, startIndex);
        createHeaderRow(sheet, transHeaders, startIndex + 3);
        fillDatas(sheet, transDatas, startIndex + 3);
    }

    public static Workbook createExcelFile(String workbookName, String[] headersSettle, String[] headersTrans,
            Collection<String[]> datasSettle, Collection<String[]> datasTrans) throws BlankArgumentException {

        if (StringUtils.isEmpty(workbookName)) {
            throw new BlankArgumentException("outFilename is blank");
        }
        Workbook wb = new HSSFWorkbook();
        HSSFSheet sheetSettle = (HSSFSheet) wb.createSheet(WorkbookUtil.createSafeSheetName(workbookName));
        createHeaderRow(sheetSettle, headersSettle);
        fillDatas(sheetSettle, datasSettle);
        createHeaderRow(sheetSettle, headersTrans, datasSettle.size() + 3);
        fillDatas(sheetSettle, datasTrans, datasSettle.size() + 3);
        return wb;
    }

    private static void createHeaderRow(Sheet sheet, String[] headers) {

        if (headers == null) {
            return;
        }
        HSSFRow row = (HSSFRow) sheet.createRow(0);
        for (int colIdx = 0; colIdx < headers.length; colIdx++) {
            fillCellData(row, colIdx, headers[colIdx]);
        }
    }

    private static void createHeaderRow(Sheet sheet, String[] headers, int index) {

        if (headers == null) {
            return;
        }
        HSSFRow row = (HSSFRow) sheet.createRow(index);
        for (int colIdx = 0; colIdx < headers.length; colIdx++) {
            fillCellData(row, colIdx, headers[colIdx]);
        }
    }

    private static void fillDatas(HSSFSheet sheet, Collection<String[]> datas) {

        if (datas == null || datas.size() == 0) {
            return;
        }
        int rowIdx = 0;
        for (String[] dataArr : datas) {
            rowIdx++;
            HSSFRow hssfRow = sheet.createRow(rowIdx);
            for (int colIdx = 0; colIdx < dataArr.length; colIdx++) {
                fillCellData(hssfRow, colIdx, dataArr[colIdx]);
            }
        }

    }

    private static void fillDatas(HSSFSheet sheet, Collection<String[]> datas, int rowIdx) {

        if (datas == null || datas.size() == 0) {
            return;
        }
        for (String[] dataArr : datas) {
            rowIdx++;
            HSSFRow hssfRow = sheet.createRow(rowIdx);
            for (int colIdx = 0; colIdx < dataArr.length; colIdx++) {
                fillCellData(hssfRow, colIdx, dataArr[colIdx]);
            }
        }

    }

    private static void fillDatas(HSSFSheet sheet, String[] datas, int rowIdx) {

        if (datas == null || datas.length == 0) {
            return;
        }
        rowIdx++;
        HSSFRow hssfRow = sheet.createRow(rowIdx);
        for (int colIdx = 0; colIdx < datas.length; colIdx++) {
            fillCellData(hssfRow, colIdx, datas[colIdx]);
        }

    }

    private static void fillCellData(HSSFRow hssfRow, int colIdx, String data) {

        HSSFCell hssfCell = hssfRow.createCell(colIdx);
        hssfCell.setCellValue(data);
    }

}


一个ExcelUtils

标签:

原文地址:http://blog.csdn.net/ro_wsy/article/details/44454431

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!