java实现导出excel

package com.github.wxiaoqi.security.auth.common.util.excel.tow;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

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.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

import javax.servlet.http.HttpServletResponse;


public class ExportExcel{
    /**
     * 导出Excel
     * @param dataList  数据
     * @param response 返回
     * @param tableName 表名
     * @param tableHead 表头
     * @param tableInfo 表信息
     * @param titles 列标题
     *
     */
    public void getValue(List<List<String>> dataList, HttpServletResponse response, String tableName, String tableHead, String tableInfo, String[] titles){
        try{
            //1.创建簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //2.创建表
            HSSFSheet sheet = workbook.createSheet(tableName);
            //1.1创建合并单元格对象
            //起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,titles.length-1);
            //起始行,结束行,起始列,结束列
            CellRangeAddress callRangeAddress1 = new CellRangeAddress(1,1,0,titles.length-1);
//
            //部项目经理部
            HSSFCellStyle headStyle = createCellStyle(workbook,(short)20,true,true,false);
            //派工单
            HSSFCellStyle erStyle = createCellStyle(workbook,(short)15,false,true,false);
            //内容样式
            HSSFCellStyle cellStyle = createCellStyle(workbook,(short)10,false,true,false);

            //设置默认列宽
            sheet.setDefaultColumnWidth(15);
            //2.1加载合并单元格对象
            sheet.addMergedRegion(callRangeAddress);
            sheet.addMergedRegion(callRangeAddress1);
            //3.创建行
            //3.1创建头标题行;并且设置头标题
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell = row.createCell(0);
            //style为带边框的样式 上面有定义
            cell.setCellStyle(headStyle);
            cell.setCellValue(tableHead);

            HSSFRow rower = sheet.createRow(1);
            HSSFCell celler = rower.createCell(0);
            //style为带边框的样式 上面有定义
            celler.setCellStyle(erStyle);
            celler.setCellValue(tableInfo);

            createCellsStyle(workbook,sheet,callRangeAddress,true);
            createCellsStyle(workbook,sheet,callRangeAddress1,true);

            //3.2创建列标题;并且设置列标题
            HSSFRow row2 = sheet.createRow(2);
            //""为占位字符串
            for(int i=0;i<titles.length;i++)
            {
                HSSFCell cell2 = row2.createCell(i);
                //加载单元格样式
                cell2.setCellStyle(cellStyle);
                cell2.setCellValue(titles[i]);
            }
            //4.操作单元格;将用户列表写入excel
            if(dataList != null)
            {
                int j=3;
                for (List<String> list:dataList){
                    int i=0;
                    HSSFRow row3 = sheet.createRow(j++);
                    for(String str:list){
                        HSSFCell cell0 = row3.createCell(i++);
                        cell0.setCellStyle(cellStyle);
                        cell0.setCellValue(str);
                    }
                }
            }
            //5.输出
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            String fileName=tableName+".xls";
            try {
                response.addHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("UTF-8"),"ISO8859-1"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                fileName= UUID.randomUUID().toString()+".xls";
                response.addHeader("Content-Disposition", "attachment; filename="+fileName);
            }
            workbook.write(response.getOutputStream());
            workbook.close();
            //out.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

     /**
     * 单元格样式
     * @param workbook  工作簿
     * @param fontsize 字体大小
     * @param flag 是否加粗
     * @param flag1 是否水平居中
     * @param flag2 是否靠右
     * @return
     */
    private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook,short fontsize,boolean flag,boolean flag1,boolean flag2) {
        // TODO Auto-generated method stub
        HSSFCellStyle style = workbook.createCellStyle();

        //下边框
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //左边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        //右边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //上边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);

        //是否水平居中
        if(flag1){
            //水平居中
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        }
        if(flag2){
            //靠右
            style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        }
        //垂直居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        //创建字体
        HSSFFont font = workbook.createFont();
        //是否加粗字体
        if(flag){
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        }
        font.setFontHeightInPoints(fontsize);
        //加载字体
        style.setFont(font);
        return style;
    }

    /**
     *合并后单元格样式
     * @param workbook
     * @param sheet
     * @return 
     */
    private static void createCellsStyle(HSSFWorkbook workbook,HSSFSheet sheet ,CellRangeAddress callRangeAddress, boolean flag) {

        if (flag){
            // 下边框
            RegionUtil.setBorderBottom(1, callRangeAddress, sheet, workbook);
            // 左边框
            RegionUtil.setBorderLeft(1, callRangeAddress, sheet, workbook);
            // 右边框
            RegionUtil.setBorderRight(1, callRangeAddress, sheet, workbook);
            // 上边框
            RegionUtil.setBorderTop(1, callRangeAddress, sheet, workbook);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值