创建Excel的工具类 ExcelUtil

package com.joyveb.genlotwbos.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hssf.util.HSSFCellUtil;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;

import com.lottery.genlotwbos.domain.StatByDrawNumber;
import com.lottery.genlotwbos.domain.GxStatByDrawNumber;

/**
 * 描述:Excel写操作帮助类
 *
 * @author ALEX
 * @since 2010-11-24
 * @version 1.0v
 */
public class ExcelUtil {
    /**
     * 功能:将HSSFWorkbook写入Excel文件
     *
     * @param wb
     *            HSSFWorkbook
     * @param absPath
     *            写入文件的相对路径
     * @param wbName
     *            文件名
     */
    public static void writeWorkbook(HSSFWorkbook wb, String fileName) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(fileName);
            wb.write(fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 功能:创建HSSFSheet工作簿
     *
     * @param wb
     *            HSSFWorkbook
     * @param sheetName
     *            String
     * @return HSSFSheet
     */
    public static HSSFSheet createSheet(HSSFWorkbook wb, String sheetName) {
        HSSFSheet sheet = wb.createSheet(sheetName);
        sheet.setDefaultColumnWidth(12);
        sheet.setGridsPrinted(false);
        sheet.setDisplayGridlines(false);
        return sheet;
    }

    /**
     * 功能:创建HSSFRow
     *
     * @param sheet
     *            HSSFSheet
     * @param rowNum
     *            int
     * @param height
     *            int
     * @return HSSFRow
     */
    public static HSSFRow createRow(HSSFSheet sheet, int rowNum, int height) {
        HSSFRow row = sheet.createRow(rowNum);
        row.setHeight((short) height);
        return row;
    }

    /**
     * 功能:创建CellStyle样式
     *
     * @param wb
     *            HSSFWorkbook
     * @param backgroundColor
     *            背景色
     * @param foregroundColor
     *            前置色
     * @param font
     *            字体
     * @return CellStyle
     */
    public static CellStyle createCellStyle(HSSFWorkbook wb,
            short backgroundColor, short foregroundColor, short halign,
            Font font) {
        CellStyle cs = wb.createCellStyle();
        cs.setAlignment(halign);
        cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        cs.setFillBackgroundColor(backgroundColor);
        cs.setFillForegroundColor(foregroundColor);
        cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cs.setFont(font);
        return cs;
    }

    /**
     * 功能:创建带边框的CellStyle样式
     *
     * @param wb
     *            HSSFWorkbook
     * @param backgroundColor
     *            背景色
     * @param foregroundColor
     *            前置色
     * @param font
     *            字体
     * @return CellStyle
     */
    public static CellStyle createBorderCellStyle(HSSFWorkbook wb,
            short backgroundColor, short foregroundColor, short halign,
            Font font) {
        CellStyle cs = wb.createCellStyle();
        cs.setAlignment(halign);
        cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        cs.setFillBackgroundColor(backgroundColor);
        cs.setFillForegroundColor(foregroundColor);
        cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cs.setFont(font);
        cs.setBorderLeft(CellStyle.BORDER_DASHED);
        cs.setBorderRight(CellStyle.BORDER_DASHED);
        cs.setBorderTop(CellStyle.BORDER_DASHED);
        cs.setBorderBottom(CellStyle.BORDER_DASHED);
        return cs;
    }

    /**
     * 功能:创建CELL
     *
     * @param row
     *            HSSFRow
     * @param cellNum
     *            int
     * @param style
     *            HSSFStyle
     * @return HSSFCell
     */
    public static HSSFCell createCell(HSSFRow row, int cellNum, CellStyle style) {
        HSSFCell cell = row.createCell(cellNum);
        cell.setCellStyle(style);
        return cell;
    }

    /**
     * 功能:合并单元格
     *
     * @param sheet
     *            HSSFSheet
     * @param firstRow
     *            int
     * @param lastRow
     *            int
     * @param firstColumn
     *            int
     * @param lastColumn
     *            int
     * @return int 合并区域号码
     */
    public static int mergeCell(HSSFSheet sheet, int firstRow, int lastRow,
            int firstColumn, int lastColumn) {
        return sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow,
                firstColumn, lastColumn));
    }

    /**
     * 功能:创建字体
     *
     * @param wb
     *            HSSFWorkbook
     * @param boldweight
     *            short
     * @param color
     *            short
     * @return Font
     */
    public static Font createFont(HSSFWorkbook wb, short boldweight,
            short color, short size) {
        Font font = wb.createFont();
        font.setBoldweight(boldweight);
        font.setColor(color);
        font.setFontHeightInPoints(size);
        return font;
    }

    /**
     * 设置合并单元格的边框样式
     *
     * @param sheet
     *            HSSFSheet
     * @param ca
     *            CellRangAddress
     * @param style
     *            CellStyle
     */
    public static void setRegionStyle(HSSFSheet sheet, CellRangeAddress ca,
            CellStyle style) {
        for (int i = ca.getFirstRow(); i <= ca.getLastRow(); i++) {
            HSSFRow row = HSSFCellUtil.getRow(i, sheet);
            for (int j = ca.getFirstColumn(); j <= ca.getLastColumn(); j++) {
                HSSFCell cell = HSSFCellUtil.getCell(row, j);
                cell.setCellStyle(style);
            }
        }
    }

    public static String RMBMoney(Long value) {
        String strValue = value + "";
        char[] arrayValue = strValue.toCharArray();
        for (int i = arrayValue.length - 1; i >= 0; i--) {

        }
        return "¥";
    }

    /**
     * 设置合并单元格的边框样式
     *
     * @param sheet
     *            HSSFSheet
     * @param ca
     *            CellRangAddress
     * @param style
     *            CellStyle
     * @throws IOException
     */
    public static void createExcel(String gameName,List<StatByDrawNumber> statByDrawNumbers,
            String filePath, String startDate, String endDate)
            throws IOException {/*
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");
        // 根据指定的文件创建输出流
        String fileName = "按期统计" + df1.format(new Date()) + ".xls";
        FileOutputStream out = new FileOutputStream(filePath+ fileName);
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();

        HSSFRow title = sheet.createRow(0);
        title.setHeightInPoints(20);
        HSSFCell cell = title.createCell(0);
        HSSFFont cnFont = wb.createFont();
        cnFont.setFontHeightInPoints((short) 20);
        cnFont.setBoldweight((short) 30);
        cnFont.setFontName("隶书");
        HSSFCellStyle cnStyle = wb.createCellStyle();
        cnStyle.setFont(cnFont);
        cell.setCellStyle(cnStyle);
        HSSFRichTextString richText = new HSSFRichTextString(startDate + "日至"
                + endDate + "日广西("+gameName+")期销售明细报表");
        cell.setCellValue(richText);

        HSSFRow header = sheet.createRow(1);

        HSSFFont rowHeaderFont = wb.createFont();
        cnFont.setFontHeightInPoints((short) 15);
        cnFont.setBoldweight((short) 20);
        cnFont.setFontName("隶书");
        HSSFCellStyle rowHeaderStyle = wb.createCellStyle();
        rowHeaderStyle.setFont(rowHeaderFont);
        HSSFCell cell0 = createCell(header, 0, rowHeaderStyle);
        cell0.setCellValue("游戏名称");
        HSSFCell cell1 = createCell(header, 1, rowHeaderStyle);
        cell1.setCellValue("游戏代码");
        HSSFCell cell2 = createCell(header, 2, rowHeaderStyle);
        cell2.setCellValue("游戏期号");
        HSSFCell cell3 = createCell(header, 3, rowHeaderStyle);
        cell3.setCellValue("开奖日期");
        HSSFCell cell4 = createCell(header, 4, rowHeaderStyle);
        cell4.setCellValue("销售总金额(元)");
        HSSFCell cell5 = createCell(header, 5, rowHeaderStyle);
        cell5.setCellValue("小奖中奖金额(元)");
        HSSFCell cell6 = createCell(header, 6, rowHeaderStyle);
        cell6.setCellValue("应收金额(元)");
        Long totalBetMoney = 0L;
        Long smallPrizeMoney = 0L;
        Long yue = 0L;
        for (int i = 0; i < statByDrawNumbers.size(); i++) {
            NewStatByDrawNumber statByDrawNumber = statByDrawNumbers.get(i);
            totalBetMoney += statByDrawNumber.getTotalBetMoney();
            smallPrizeMoney += statByDrawNumber.getSmallPrizeMoney();
            yue += statByDrawNumber.getTotalBetMoney()
                    - statByDrawNumber.getSmallPrizeMoney();

            HSSFRow row = sheet.createRow(i + 2);
            HSSFFont font = wb.createFont();
            cnFont.setFontHeightInPoints((short) 10);
            cnFont.setBoldweight((short) 10);
            cnFont.setFontName("隶书");
            HSSFCellStyle style = wb.createCellStyle();
            rowHeaderStyle.setFont(font);
            createCell(row, 0, rowHeaderStyle)
                    .setCellValue(
                            DarwNumberUtils.lotteryName.get(statByDrawNumber
                                    .getLtype()));
            createCell(row, 1, rowHeaderStyle).setCellValue(
                    statByDrawNumber.getLtype());
            createCell(row, 2, style).setCellValue(
                    statByDrawNumber.getDrawnumber());
            createCell(row, 3, style).setCellValue(
                    df.format(statByDrawNumber.getDrawtime()));
            createCell(row, 4, style).setCellValue(
                    statByDrawNumber.getTotalBetMoney());
            createCell(row, 5, style).setCellValue(
                    statByDrawNumber.getSmallPrizeMoney());
            createCell(row, 6, style).setCellValue(
                    statByDrawNumber.getTotalBetMoney()
                            - statByDrawNumber.getSmallPrizeMoney());
        }

        HSSFRow row = sheet.createRow(statByDrawNumbers.size() + 2);
        HSSFFont font = wb.createFont();
        cnFont.setFontHeightInPoints((short) 15);
        cnFont.setBoldweight((short) 20);
        cnFont.setFontName("隶书");
        HSSFCellStyle style = wb.createCellStyle();
        rowHeaderStyle.setFont(font);
        createCell(row, 0, rowHeaderStyle).setCellValue("");
        createCell(row, 1, rowHeaderStyle).setCellValue("");
        createCell(row, 2, style).setCellValue("合计(元)");
        createCell(row, 3, style).setCellValue("");
        createCell(row, 4, style).setCellValue(totalBetMoney);
        createCell(row, 5, style).setCellValue(smallPrizeMoney);
        createCell(row, 6, style).setCellValue(yue);

        sheet.setColumnWidth(0, 5000);
        sheet.setColumnWidth(1, 5000);
        sheet.setColumnWidth(2, 5000);
        sheet.setColumnWidth(3, 5000);
        sheet.setColumnWidth(4, 5000);
        sheet.setColumnWidth(5, 5000);
        sheet.setColumnWidth(6, 5000);

        // 3.output
        sheet.setDisplayGridlines(false);
        sheet.setPrintGridlines(false);
        HSSFPrintSetup printSetup = sheet.getPrintSetup();
        // A4纸
        printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
        wb.write(out);
        out.close();
    */}

    public static void createGXExcel(String gameName,List<GxStatByDrawNumber> statByDrawNumbers,
            String filePath, String startDate, String endDate)
            throws IOException {/*
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        DateFormat df1 = new SimpleDateFormat("yyyyMMddhhmmss");
        // 根据指定的文件创建输出流
        String fileName = "广西按期统计" + df1.format(new Date()) + ".xls";
        FileOutputStream out = new FileOutputStream(filePath+fileName);
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();

        HSSFRow title = sheet.createRow(0);
        title.setHeightInPoints(20);
        HSSFCell cell = title.createCell(0);
        HSSFFont cnFont = wb.createFont();
        cnFont.setFontHeightInPoints((short) 20);
        cnFont.setBoldweight((short) 30);
        cnFont.setFontName("隶书");
        HSSFCellStyle cnStyle = wb.createCellStyle();
        cnStyle.setFont(cnFont);
        cell.setCellStyle(cnStyle);
        HSSFRichTextString richText = new HSSFRichTextString(startDate + "日至"
                + endDate + "日广西("+gameName+")期销售明细报表");
        cell.setCellValue(richText);

        HSSFRow header = sheet.createRow(1);

        HSSFFont rowHeaderFont = wb.createFont();
        cnFont.setFontHeightInPoints((short) 15);
        cnFont.setBoldweight((short) 20);
        cnFont.setFontName("隶书");
        HSSFCellStyle rowHeaderStyle = wb.createCellStyle();
        rowHeaderStyle.setFont(rowHeaderFont);
        HSSFCell cell0 = createCell(header, 0, rowHeaderStyle);
        cell0.setCellValue("游戏名称");
        HSSFCell cell1 = createCell(header, 1, rowHeaderStyle);
        cell1.setCellValue("游戏代码");
        HSSFCell cell2 = createCell(header, 2, rowHeaderStyle);
        cell2.setCellValue("游戏期号");
        HSSFCell cell3 = createCell(header, 3, rowHeaderStyle);
        cell3.setCellValue("开奖日期");
        HSSFCell cell4 = createCell(header, 4, rowHeaderStyle);
        cell4.setCellValue("销售总金额(元)");
        HSSFCell cell5 = createCell(header, 5, rowHeaderStyle);
        cell5.setCellValue("小奖中奖金额(元)");
        HSSFCell cell6 = createCell(header, 6, rowHeaderStyle);
        cell6.setCellValue("应付金额(元)");
        Long totalBetMoney = 0L;
        Long smallPrizeMoney = 0L;
        Long yue = 0L;
        for (int i = 0; i < statByDrawNumbers.size(); i++) {
            GxStatByDrawNumber statByDrawNumber = statByDrawNumbers.get(i);
            totalBetMoney += statByDrawNumber.getTotalBetMoney();
            smallPrizeMoney += statByDrawNumber.getSmallPrizeMoney();
            yue += statByDrawNumber.getTotalBetMoney()
                    - statByDrawNumber.getSmallPrizeMoney();

            HSSFRow row = sheet.createRow(i + 2);
            HSSFFont font = wb.createFont();
            cnFont.setFontHeightInPoints((short) 10);
            cnFont.setBoldweight((short) 10);
            cnFont.setFontName("隶书");
            HSSFCellStyle style = wb.createCellStyle();
            rowHeaderStyle.setFont(font);
            createCell(row, 0, rowHeaderStyle)
                    .setCellValue(
                            DarwNumberUtils.lotteryName.get(statByDrawNumber
                                    .getLtype()));
            createCell(row, 1, rowHeaderStyle).setCellValue(
                    statByDrawNumber.getLtype());
            createCell(row, 2, style).setCellValue(
                    statByDrawNumber.getDrawnumber());
            createCell(row, 3, style).setCellValue(
                    df.format(statByDrawNumber.getDrawtime()));
            createCell(row, 4, style).setCellValue(
                    statByDrawNumber.getTotalBetMoney());
            createCell(row, 5, style).setCellValue(
                    statByDrawNumber.getSmallPrizeMoney());
            createCell(row, 6, style).setCellValue(
                    statByDrawNumber.getTotalBetMoney()
                            - statByDrawNumber.getSmallPrizeMoney());
        }

        HSSFRow row = sheet.createRow(statByDrawNumbers.size() + 2);
        HSSFFont font = wb.createFont();
        cnFont.setFontHeightInPoints((short) 15);
        cnFont.setBoldweight((short) 20);
        cnFont.setFontName("隶书");
        HSSFCellStyle style = wb.createCellStyle();
        rowHeaderStyle.setFont(font);
        createCell(row, 0, rowHeaderStyle).setCellValue("");
        createCell(row, 1, rowHeaderStyle).setCellValue("");
        createCell(row, 2, style).setCellValue("合计(元)");
        createCell(row, 3, style).setCellValue("");
        createCell(row, 4, style).setCellValue(totalBetMoney);
        createCell(row, 5, style).setCellValue(smallPrizeMoney);
        createCell(row, 6, style).setCellValue(yue);

        sheet.setColumnWidth(0, 5000);
        sheet.setColumnWidth(1, 5000);
        sheet.setColumnWidth(2, 5000);
        sheet.setColumnWidth(3, 5000);
        sheet.setColumnWidth(4, 5000);
        sheet.setColumnWidth(5, 5000);
        sheet.setColumnWidth(6, 5000);

        // 3.output
        sheet.setDisplayGridlines(false);
        sheet.setPrintGridlines(false);
        HSSFPrintSetup printSetup = sheet.getPrintSetup();
        // A4纸
        printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
        wb.write(out);
        out.close();
    */}

    public static void main(String... args) throws IOException {/*
        List<GxStatByDrawNumber> statByDrawNumbers = new ArrayList<GxStatByDrawNumber>();
        GxStatByDrawNumber s1 = new GxStatByDrawNumber();
        s1.setLtype("QGSLTO");
        s1.setDrawnumber("2011001");
        s1.setDrawtime(new Date());
        s1.setTotalBetMoney(10000L);
        s1.setSmallPrizeMoney(1000L);

        GxStatByDrawNumber s2 = new GxStatByDrawNumber();
        s2.setLtype("QGSLTO");
        s2.setDrawnumber("2011001");
        s2.setDrawtime(new Date());
        s2.setTotalBetMoney(10000L);
        s2.setSmallPrizeMoney(1000L);

        GxStatByDrawNumber s3 = new GxStatByDrawNumber();
        s3.setLtype("QGSLTO");
        s3.setDrawnumber("2011001");
        s3.setDrawtime(new Date());
        s3.setTotalBetMoney(10000L);
        s3.setSmallPrizeMoney(1000L);

        GxStatByDrawNumber s4 = new GxStatByDrawNumber();
        s4.setLtype("QGSLTO");
        s4.setDrawnumber("2011001");
        s4.setDrawtime(new Date());
        s4.setTotalBetMoney(10000L);
        s4.setSmallPrizeMoney(1000L);

        GxStatByDrawNumber s5 = new GxStatByDrawNumber();
        s5.setLtype("QGSLTO");
        s5.setDrawnumber("2011001");
        s5.setDrawtime(new Date());
        s5.setTotalBetMoney(10000L);
        s5.setSmallPrizeMoney(1000L);

        statByDrawNumbers.add(s1);
        statByDrawNumbers.add(s2);
        statByDrawNumbers.add(s3);
        statByDrawNumbers.add(s4);
        statByDrawNumbers.add(s5);
       

    */}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值