用HSSFSheet创建复杂的excel表格

用HSSFSheet可以很方便的创建如下复杂的excel表格:

商户名称月份订单状态订单数量现金总额(元)投诉件数
NIKE2010-11未支付00.005
支付中00.00
待发货30.00
已发货00.00
已签收00.00
已支付00.00
已完结00.00
已取消00.00
已退费00.00
未核实00.00
全部30.00
红双喜2010-11未支付00.0013
支付中25,495.00
待发货00.00
已发货23,297.00
已签收00.00
已支付00.00
已完结00.00
已取消00.00
已退费00.00
未核实00.00
全部48,792.00
总和1426,278.5118

得导入poi.jar包,代码如下:

[html]  view plain copy
  1. public void export() throws ServiceException, IOException{  
  2.        NumberFormat nf = NumberFormat.getInstance();  
  3.        nf.setMaximumFractionDigits(2);  
  4.        nf.setMinimumFractionDigits(2);  
  5.        HSSFWorkbook wb = new HSSFWorkbook();  
  6.        HSSFSheet sheet = wb.createSheet();  
  7.        this.initSheet(sheet);//初始化sheet,设置列数和每列宽度  
  8.        HSSFCellStyle centerStyle = wb.createCellStyle();//设置为水平居中  
  9.        centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  10.        centerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  11.        HSSFCellStyle rightStyle = wb.createCellStyle();//水平靠右  
  12.        rightStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);  
  13.        rightStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  14.        this.initHeader(sheet, centerStyle);//初始化头部为水平居中  
  15.        List list = service.listReport(orderReportVo);//拿出数据  
  16.        int orderCount = 0;  
  17.        int INTEGRAL = 0;  
  18.        double cash = 0;  
  19.        int complaintCount = 0;  
  20.        //整理数据  
  21.        Map<String, Map<Integer, OrderReportVo>> map = new TreeMap<String, Map<Integer,OrderReportVo>>();  
  22.        for(int i = 0; i < list.size(); i++){  
  23.            OrderReportVo vo = (OrderReportVo) list.get(i);  
  24.            orderCount += vo.getOrderCount();  
  25.            cash += vo.getCashAmount();  
  26.            complaintCount += vo.getComplaintCount();  
  27.            String key = vo.getMerchantCode() + vo.getMonth();  
  28.            Map<Integer, OrderReportVo> tempMap = map.get(key);  
  29.            if(tempMap == null){  
  30.                tempMap = new HashMap<Integer, OrderReportVo>();  
  31.                OrderReportVo tempVo = new OrderReportVo();  
  32.                tempVo.setMerchantName(vo.getMerchantName());  
  33.                tempVo.setCashAmount(0.0);  
  34.                tempVo.setOrderCount(0);  
  35.                tempVo.setMonth(vo.getMonth());  
  36.                tempVo.setComplaintCount(0);  
  37.   
  38.                for (int j = 0; j < status.length; j++) {  
  39.                    tempMap.put(status[j], tempVo);  
  40.                }  
  41.                map.put(key, tempMap);  
  42.            }  
  43.            tempMap.put(vo.getOrderStatus(), vo);  
  44.        }  
  45.        if(!AssertUtil.isEmpty(map)){  
  46.            int rowNumPerMerchant = 11;//每个商家一个月的统计记录占11个表格,10个订单状态+1个全部(统计)  
  47.            int rowNum = 0;  
  48.            int merchantCount = 0;//记录数  
  49.            //按要求创建各单元格  
  50.            for(Map<Integer, OrderReportVo> report : map.values()){  
  51.                merchantCount++;  
  52.                HSSFRow row = sheet.createRow(++rowNum);  
  53.                OrderReportVo vo0 = report.get(0);  
  54.                int OrderCountPerMerchant = vo0.getOrderCount();  
  55.                int ComplaintCountPerMerchant = vo0.getComplaintCount();  
  56.                double CashAmountPerMerchant = vo0.getCashAmount();  
  57.                  
  58.                this.createCell(row, 0, vo0.getMerchantName() , centerStyle);  
  59.                this.createCell(row, 1, vo0.getMonth(), centerStyle);  
  60.                sheet.addMergedRegion(new Region(rowNum,(short)0,rowNum + 10,(short)0));  
  61.                sheet.addMergedRegion(new Region(rowNum,(short)1,rowNum + 10,(short)1));  
  62.                this.createCell(row, 2, orderStatusToString(0), centerStyle);  
  63.                this.createCell(row, 3, vo0.getOrderCount(), rightStyle);  
  64.                this.createCell(row, 4, nf.format(vo0.getCashAmount()), rightStyle);  
  65.                  
  66.                //循环按订单状态设置单元格,(i从1开始)  
  67.                for (int i = 1; i < status.length; i++) {  
  68.                    OrderReportVo vo = report.get(status[i]);  
  69.                    OrderCountPerMerchant += vo.getOrderCount();  
  70.                    ComplaintCountPerMerchant += vo.getComplaintCount();  
  71.                    CashAmountPerMerchant += vo.getCashAmount();  
  72.                          
  73.                    HSSFRow row2 = sheet.createRow(++rowNum);  
  74.                    this.createCell(row2, 2, orderStatusToString(status[i]), centerStyle);//订单状态  
  75.                    this.createCell(row2, 3, vo.getOrderCount(), rightStyle);//订单数量  
  76.                    //this.createCell(row2, 4, vo2.getIntegralAmount(), rightStyle);  
  77.                    this.createCell(row2, 4, nf.format(vo.getCashAmount()), rightStyle);//现金总额  
  78.                }  
  79.                HSSFRow allrow = sheet.createRow(++rowNum);  
  80.                this.createCell(allrow, 2, "全部", centerStyle);  
  81.                this.createCell(allrow, 3, OrderCountPerMerchant, rightStyle);  
  82.                //this.createCell(row, 4, INTEGRAL, rightStyle);  
  83.                this.createCell(allrow, 4, nf.format(CashAmountPerMerchant), rightStyle);  
  84.                int rowIndex = (merchantCount-1)*rowNumPerMerchant;//计算投诉件数单元格的行数位置  
  85.                HSSFRow row0 = sheet.createRow(rowIndex+1);  
  86.                this.createCell(row0, 5, ComplaintCountPerMerchant, centerStyle);//投诉件数  
  87.                sheet.addMergedRegion(new Region(rowIndex+1,(short)5,rowIndex+11,(short)5));  
  88.                  
  89.            }  
  90.            //底部总结单元格  
  91.            HSSFRow row = sheet.createRow(++rowNum);  
  92.            this.createCell(row, 2, "总和", centerStyle);  
  93.            this.createCell(row, 3, orderCount, rightStyle);  
  94.            //this.createCell(row, 4, INTEGRAL, rightStyle);  
  95.            this.createCell(row, 4, nf.format(cash), rightStyle);  
  96.            this.createCell(row, 5, complaintCount, centerStyle);//投诉件数  
  97.              
  98.        }  
  99.        response.setContentType("application/ms-excel;charset=UTF-8");  
  100.        response.setHeader("Content-disposition", "attachment; filename=" + new String("订单统计报表".getBytes("GBK"), "ISO-8859-1") + ".xls");  
  101.        OutputStream out = response.getOutputStream();  
  102.        wb.write(out);  
  103.        out.flush();  
  104.        out.close();  
  105.          
  106.    }  

其中

[html]  view plain copy
  1. //10种订单状态    private final int[] status = new int[]{0,1,2,3,4,5,6,-1,-2,-3};  

初始化sheet

[html]  view plain copy
  1. //初始化sheet,设置列数和每列宽度  
  2. private void initSheet(HSSFSheet sheet){  
  3.     sheet.setColumnWidth((short)0, (short) (35.7 * 150));  
  4.     sheet.setColumnWidth((short)1, (short)(35.7 * 100));  
  5.     sheet.setColumnWidth((short)2, (short)(35.7 * 150));  
  6.     sheet.setColumnWidth((short)3, (short)(35.7 * 60));  
  7.     sheet.setColumnWidth((short)4, (short)(35.7 * 120));  
  8.     sheet.setColumnWidth((short)5, (short)(35.7 * 120));  
  9. }  

初始化sheet样式

[html]  view plain copy
  1. /**  
  2.     * 初始化sheet样式  
  3.     * @param sheet  
  4.     * @param style  
  5.     */  
  6.    private void initHeader(HSSFSheet sheet,HSSFCellStyle style){  
  7.        HSSFRow row1 = sheet.createRow((short) 0);  
  8.        createCell(row1, 0, "商户名称", style);  
  9.        createCell(row1, 1, "月份", style);  
  10.        createCell(row1, 2, "订单状态", style);  
  11.        createCell(row1, 3, "订单数量", style);  
  12.        createCell(row1, 4, "现金总额(元)", style);  
  13.        createCell(row1, 5, "投诉件数", style);  
  14.    }  


 

创建单元格

[html]  view plain copy
  1. /**  
  2.     * 创建单元格  
  3.     * @param row 行  
  4.     * @param column 列位置  
  5.     * @param value 值  
  6.     * @param style 样式  
  7.     */  
  8.    private void createCell(HSSFRow row,int column,Object value,HSSFCellStyle style){  
  9.        HSSFCell cell = row.createCell((short)column);  
  10.        cell.setEncoding((short) 1);  
  11.        cell.setCellValue(String.valueOf(value));  
  12.        cell.setCellStyle(style);  
  13.    }   
[html]  view plain copy
  1. private  String orderStatusToString(int code){  
  2.         switch (code) {  
  3.         case 0:  
  4.             return "未支付";  
  5.         case 1:  
  6.             return "支付中";  
  7.         case 2:  
  8.             return "待发货";  
  9.         case 3:  
  10.             return "已发货";  
  11.         case 4:  
  12.             return "已签收";  
  13.         case 5:  
  14.             return "已支付";  
  15.         case 6:  
  16.             return "已完结";  
  17.         case -1:  
  18.             return "已取消";  
  19.         case -2:  
  20.             return "已退费";  
  21.         case -3:  
  22.             return "未核实";  
  23.         default:  
  24.             return null;  
  25.         }  
  26.           
  27.     }  
  28. 可参考hr系统excel导入导出功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值