Excel-生成工具类

package com.xxx.statistic.file;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import com.hundsun.statistic.util.FileUtils;


@SuppressWarnings("deprecation")
public class ExcelWriter {
    
    public static void write(String floder ,String fileName,String title,String[] fields,String[] fieldNames,List<Map<String,Object>> records) throws Exception{
        if (records.size()>0){
            FileOutputStream fileOut = null;
            String filePath = "c:/statistic/excel/"+floder+"/"+fileName+"_" 
                +new SimpleDateFormat("yyMMdd_HHmmss").format(new Date()) +".xls";
            try {
                FileUtils.createFoldersByPath(filePath.substring(0,filePath.lastIndexOf("/")));
                HSSFWorkbook wb = new HSSFWorkbook();
                HSSFSheet sheet = wb.createSheet(title);
                
                //报表标题行   0行
                HSSFRow rowHead = sheet.createRow(0);
                rowHead.setHeightInPoints((short) 36);// 设置宽度
                HSSFFont f = sheet.getWorkbook().createFont();
                CellStyle style = sheet.getWorkbook().createCellStyle();
                f.setFontHeightInPoints((short) 18);// 字号
                f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
                f.setFontName("华文楷体");
                style.setFont(f);
                style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
                style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
                sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, fields.length-1));// 合并第一行单元格
                HSSFCell cellHead = rowHead.createCell(0);
                cellHead.setCellValue(title);
                cellHead.setCellStyle(style);
                //表头   1行
                HSSFRow rowTableHead = sheet.createRow(1);
                for(int i=0;i<fields.length;i++){
                    sheet.setColumnWidth(i, 5000);
                    HSSFCell cell = rowTableHead.createCell(i);
                    cell.setCellValue(fieldNames[i]);
                    CellStyle styleTableHead = sheet.getWorkbook().createCellStyle();
                    styleTableHead.setFillForegroundColor(IndexedColors.AQUA.getIndex());
                    styleTableHead.setFillPattern(CellStyle.SOLID_FOREGROUND);
                    styleTableHead.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
                    styleTableHead.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
                    styleTableHead.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
                    styleTableHead.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
                    styleTableHead.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
                    styleTableHead.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
                    cell.setCellStyle(styleTableHead);
                }
                //表体 2行开始
                for(int i=0;i<records.size();i++){
                    HSSFRow rowTableBody = sheet.createRow(i+2);
                    for(int j=0;j<fields.length;j++){
                        HSSFCell cell = rowTableBody.createCell(j);
                        cell.setCellValue(records.get(i).get("oracle".equals(floder)?fields[j].toUpperCase():fields[j]).toString());
                    }
                }
                fileOut = new FileOutputStream(filePath);
                wb.write(fileOut);
            } catch (Exception e) {
                throw new Exception(e);
            }finally{
                if (fileOut != null)
                    try {fileOut.close();} catch (IOException e) {}
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值