导出报表的代码

   // 导出报表(商铺交易统计)

   @RequestMapping("/downloadOrder/{page}")

   public voiddownLoadOrder(@PathVariable intpage,

        HttpServletRequestrequest,OrderBean orderBean, HttpServletResponse response)

        throwsException {

     if(page <= 0) {

        page = 1;

     }

 

     PageUntilspageBean = tborderService.countTborderByPage(orderBean,page,

          Const.PAGE_ROWS);

     @SuppressWarnings("unchecked")

     List<OrderBean>list = pageBean.getList(); // 得到数据库数据

 

     // 第一步,创建一个webbook,对应一个Excel文件

     Workbook wb = newHSSFWorkbook();

     // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet

     HSSFSheet sheet =(HSSFSheet) wb.createSheet("sheet1");

     // 第三步,在sheet中添加表头第0,

     HSSFRow row =sheet.createRow((int) 0);

     // 第四步,创建单元格,并设置值表头设置表头居中

     HSSFCellStyle style =(HSSFCellStyle) wb.createCellStyle();

     style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

     // 生成一个字体

     HSSFFont font =(HSSFFont) wb.createFont();

     font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色

     font.setFontHeightInPoints((short)12);

     //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗

     style.setFont(font);

     // 创建格式

     CellStyle cellStyle =wb.createCellStyle();

     cellStyle.setBorderBottom(CellStyle.BORDER_THIN);

     cellStyle.setBorderLeft(CellStyle.BORDER_THIN);

     cellStyle.setBorderRight(CellStyle.BORDER_THIN);

     cellStyle.setBorderTop(CellStyle.BORDER_THIN);

     cellStyle.setFont(font);

     HSSFCell cell =row.createCell(0);

     cell.setCellValue("订单号");

     cell.setCellStyle(style);

     sheet.setColumnWidth(0,4000);// 设置列的宽度

     cell =row.createCell(1);

     cell.setCellValue("用户名");

     cell.setCellStyle(style);

     sheet.setColumnWidth(1,4000);

     cell =row.createCell(2);

     cell.setCellValue("订单状态");

     cell.setCellStyle(style);

     sheet.setColumnWidth(2,4000);

     cell =row.createCell(3);

     cell.setCellValue("下单时间");

     cell.setCellStyle(style);

     sheet.setColumnWidth(3,4000);

     cell =row.createCell(4);

     cell.setCellValue("商品数量");

     cell.setCellStyle(style);

     sheet.setColumnWidth(4,4000);

 

     for (int i =0; i < list.size(); i++) {

        HSSFRow newrow =sheet.createRow((int) i + 1);

        HSSFCell newcell =newrow.createCell(0);

        newcell.setCellValue(list.get(i).getOcode());

        newcell.setCellStyle(style);

        sheet.setColumnWidth(0,4000);// 设置列的宽度

        newcell =newrow.createCell(1);

        newcell.setCellValue(list.get(i).getLoginName());

        newcell.setCellStyle(style);

        sheet.setColumnWidth(1,4000);// 设置列的宽度

        newcell =newrow.createCell(2);

        if(list.get(i).getState() == 1) {

          newcell.setCellValue("正常订单");

        } else if(list.get(i).getState() == 2) {

          newcell.setCellValue("退款订单");

        } else if(list.get(i).getState() == 3) {

          newcell.setCellValue("换货订单");

        } else if(list.get(i).getState() == 4) {

          newcell.setCellValue("未确认订单");

        } else {

          newcell.setCellValue("作废订单");

        }

        newcell.setCellStyle(style);

        sheet.setColumnWidth(2,4000);// 设置列的宽度

        newcell =newrow.createCell(3);

        DateFormat df3 =DateFormat.getDateInstance(DateFormat.MEDIUM);

        newcell.setCellValue(df3.format(list.get(i).getOtime()));

        newcell.setCellStyle(style);

        sheet.setColumnWidth(3,4000);// 设置列的宽度

        newcell =newrow.createCell(4);

        newcell.setCellValue(list.get(i).getPsum());

        newcell.setCellStyle(style);

        sheet.setColumnWidth(4,4000);// 设置列的宽度

     }

     // 生成导出文件日期

     Calendar c = Calendar.getInstance();

     String time =c.get(Calendar.YEAR) + "_" +c.get(Calendar.MONTH) + "_"

          + c.get(Calendar.DATE) + "_" +c.get(Calendar.HOUR) + "_"

          + c.get(Calendar.MINUTE) + "_" +c.get(Calendar.SECOND);

     // 生成文件名(以下涉及到文件的输入和输出流)

     File excelFile = newFile("订单统计_" + time + "_" +page + ".xls");

     FileOutputStream fos = newFileOutputStream(excelFile);

     wb.write(fos);

     fos.close();

     // 读到流中

     InputStream inStream = newFileInputStream(excelFile);

     // 设置输出的格式

     //httpServletResponse.reset();

     response.setContentType("application/x-download;charset=utf-8");

     response.setHeader("Content-Disposition", "filename="

          + newString(excelFile.toString().getBytes("gb2312"),

               "iso8859-1"));// 设置文件头编码方式和文件名,解决文件名中文乱码

     //httpServletResponse.addHeader("Content-Disposition",

     //"attachment; filename=\"" + excelFile + "\"");

     // 循环取出流中的数据

     byte[] b= new byte[1024];

     try {

        OutputStream out =response.getOutputStream();

        wb.write(out);

        while(inStream.read(b) > 0) {

          out.write(b);

        }

        inStream.close();

        out.close();

        excelFile.delete();

     } catch(IOException ex) {

        ex.printStackTrace();

     }

 

   }


jsp页面:

function download() {

     if ('${pageBean.pageNo}' == null || '${pageBean.pageNo}' == 0) {

        alert('没有要导出的数据');

        return;

     }

     document.logaction.ocode.value = '${ocode}';

     document.logaction.otime1.value = '${otime1}';

     document.logaction.otime2.value = '${otime2}';

     document.logaction.action = "admin/downloadOrder/${pageBean.pageNo}.do";

     document.logaction.submit();

   }

<form action="" method="post"id="logaction" name="logaction">

               <table>

               <input type="hidden" id="formpath" name="formpath"/>

                  <tr>

                    <td>

                      

                         订单号: &nbsp; &nbsp;<input name="ocode" id="ocode"type="text">

                   

                       <select name="source" id="source"onchange="changetype(this.value)">

                       <option value="0" selected="selected">--发布角色--</option>

                       <option value="1">平台</option>

                       <option value="2">供应商</option>

                       </select>

                        

                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="button"

                       value="搜索信息" class="inputbutton" key="third_relation"

                       onclick="check(this)">

                    </td>

 

                    <td>

                   

                         <input name="otime1" id="otime1"type="text"

                            onClick="WdatePicker()" class="Wdate"> -<input

                            name="otime2" id="otime2"type="text" onClick="WdatePicker()"

                            class="Wdate">

                       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="button"

                       value="下单时间" class="inputbutton" key="third_relation"

                       onclick="check(this)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input

                       type="button"value="订单下载" class="inputbutton"

                       key="third_relation" onclick="download()">

                    </td>

                    <td>

                       <div class="cell" style="float: right;">总共购买:${count}个</div>

                    </td>

                  </tr>

               </table>

             </form>





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值