HSSFWorkbook导出excel

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;    

       HSSFWorkbook  excel=new HSSFWorkbook();// 创建一个Excel文件   
        HSSFSheet sheet=excel.createSheet();// 创建一个Excel的Sheet
        //设置列宽 第一个参数代表列id(从0开始),第2个参数代表宽度值  参考 :"2012-08-10"的宽度为2500
        sheet.setColumnWidth(0, 6000);
        sheet.setColumnWidth(1, 3000);
        sheet.setColumnWidth(2, 3000);
        sheet.setColumnWidth(3, 3000);
        sheet.setColumnWidth(4, 3000);
        sheet.setColumnWidth(5, 3000);
        sheet.setColumnWidth(6, 3000);
        sheet.setColumnWidth(7, 3000);
        sheet.setColumnWidth(8, 3000);
         // 设置字体   
        HSSFFont font1=excel.createFont();
        font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
        font1.setFontHeightInPoints((short)16);//16号字
        
        HSSFCellStyle cellstyle1=excel.createCellStyle();// Sheet样式   
        cellstyle1.setWrapText(true);//设置自动换行
        cellstyle1.setFont(font1);
        cellstyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        cellstyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        
        HSSFRow row=sheet.createRow(0); // 创建第一行   
        HSSFCell cell=row.createCell(0);// 创建第一列
        //头部第一行
        cell.setCellValue(date+" 消费数据");//第一行名称,我的是用时间+说明
        cell.setCellStyle(cellstyle1);
        
        CellRangeAddress range = new CellRangeAddress(0, 0, 0, 8);   
        sheet.addMergedRegion(range);
        
        //设置字体   
        HSSFFont font=excel.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
        
        HSSFCellStyle cellstyle=excel.createCellStyle();// Sheet样式   
        cellstyle.setWrapText(true);//设置自动换行
        cellstyle.setFont(font);//字体
        cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        //第二行
        row=sheet.createRow(1);
        cell=row.createCell(0);
        cell.setCellValue("刷卡机组");
        cell.setCellStyle(cellstyle);
        /**  
         *   合并单元格  
         *    第一个参数:第一个单元格的行数(从0开始)  
         *    第二个参数:第二个单元格的行数(从0开始)  
         *    第三个参数:第一个单元格的列数(从0开始)  
         *    第四个参数:第二个单元格的列数(从0开始)  
        */  
        range = new CellRangeAddress(1, 2, 0, 0);   
        sheet.addMergedRegion(range);
        
        range = new CellRangeAddress(1, 1, 1,2);   
        sheet.addMergedRegion(range);
        cell=row.createCell(1);
        cell.setCellValue("5:30~8:00");
        cell.setCellStyle(cellstyle);
        range = new CellRangeAddress(1, 1, 3,4);   
        sheet.addMergedRegion(range);
        cell=row.createCell(3);
        cell.setCellValue("11:30~12:30");
        cell.setCellStyle(cellstyle);
        range = new CellRangeAddress(1, 1, 5,6);   
        sheet.addMergedRegion(range);
        cell=row.createCell(5);
        cell.setCellValue("16:30~18:00");
        cell.setCellStyle(cellstyle);
        range = new CellRangeAddress(1, 1, 7,8);   
        sheet.addMergedRegion(range);
        cell=row.createCell(7);
        cell.setCellValue("21:00~22:00");
        cell.setCellStyle(cellstyle);
        //第二行
        row=sheet.createRow(2);
        cell=row.createCell(1);
        cell.setCellValue("刷卡人数");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(2);
        cell.setCellValue("刷卡总额");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(3);
        cell.setCellValue("刷卡人数");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(4);
        cell.setCellValue("刷卡总额");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(5);
        cell.setCellValue("刷卡人数");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(6);
        cell.setCellValue("刷卡总额");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(7);
        cell.setCellValue("刷卡人数");
        cell.setCellStyle(cellstyle);
        cell=row.createCell(8);
        cell.setCellValue("刷卡总额");
        cell.setCellStyle(cellstyle);
        int i=2;

       //循环读取数据
        while(xfls.next()){
            i++;
            row=sheet.createRow(i);//我的从第三行开始循环数据
            for(int j=2;j<=count;j++){
                cell=row.createCell(j-2);
                cell.setCellValue(String.valueOf(xfls.getObject(j)));
            }
        }
        
        String fileName = "消费数据";//表格名称
        try {
            fileName = new String(fileName.getBytes("GB2312"), "ISO_8859_1");
        } catch (UnsupportedEncodingException e1) {
            // TODO 自动生成的 catch 块
            e1.printStackTrace();
        }
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");
        ServletOutputStream out;
        try {
            out = response.getOutputStream();
            excel.write(out);
            out.close();
            out.flush();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

 

导出的Excel效果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值