将数据读进内存再下载为excel

 

后台:
 public InputStream jxl2ExcelProduct() {       
     

         ByteArrayOutputStream baos = new ByteArrayOutputStream();     
      try {         
                 
          // 2.生成工作簿         
          WritableWorkbook wb = Workbook.createWorkbook(baos);         
          // 3.生成工作表         
          WritableSheet sheet = wb.createSheet("工作表1", 0);      //sheet取名为“工作表1”    
               
          // 5.工作表中加入单元格 
          WritableFont wfont = new WritableFont(WritableFont.ARIAL, 12,
      WritableFont.BOLD, false,
      jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
    WritableCellFormat titleFormat = new WritableCellFormat(wfont);
          String title[] = { "类别/名称", "编号", "助记码", "规格型号", "类型", "默认单价", "包装", "库存",
      "单位", "批准文号", };
    for (int i = 0; i < title.length; i++) {
     Label titleLabel = new Label(i, 0, title[i], titleFormat);
     try {
      sheet.addCell(titleLabel);
     } catch (RowsExceededException e) {
      e.printStackTrace();
     } catch (WriteException e) {
      e.printStackTrace();
     }
    }        
          List list =  getMsgProduct();
          Product pd;

         Label content;

          int col = 1;


    for (int c = 0; c < list.size(); c++) {
     pd = new Product();
     pd = (Product) list.get(c);
     content = new Label(0, col, pd.getName());

     sheet.addCell(content);
     content= new Label(1, col, pd.getSn());

     sheet.addCell(content);
     content= new Label(2, col, pd.getMnemonicCode());

     sheet.addCell(content);

     content= new Label(3, col, pd.getStyle());

     sheet.addCell(content);

     content= new Label(4, col, pd.getType());

     sheet.addCell(content);

     content= new Label(5, col, pd.getCostPrice());

    sheet.addCell(content);

     content= new Label(6, col, pd.getPack());

    sheet.addCell(content);

     jxl.write.Number number8 = new jxl.write.Number(7, col, pd.getSize());

    sheet.addCell(number8); 

     content= new Label(8, col, pd.getUnit());

     sheet.addCell(content);
     content= new Label(9, col, pd.getApprovalNumber());

      sheet.addCell(content);
      col++;
    }
          
          wb.write();         
          wb.close();         
          byte[] ba = baos.toByteArray();    
          ByteArrayInputStream bais = new ByteArrayInputStream(ba);    
          return bais;  
      } catch (IOException e) {         
          e.printStackTrace();         
      } catch (RowsExceededException e) {         
          // TODO Auto-generated catch block         
          e.printStackTrace();         
      } catch (WriteException e) {         
          // TODO Auto-generated catch block         
          e.printStackTrace();         
      } finally {         
          if (baos!= null) {         
              try {         
                  baos.close();         
              } catch (IOException e) {         
                  e.printStackTrace();         
              }         
          }         
      }         
     return null;  
  } 
前台:

 

<%@ page language="java" import="java.io.*,com.ly.jxcm.web.JxlExport" pageEncoding="utf-8"%>//引入java类

<%  
String sub =request.getMethod();   
if(sub.equals("POST")){
JxlExport ts = new JxlExport();//实例化类
InputStream iso = ts.jxl2Excel();      //调用方法获得输入流
String filename = "stuff.xls";   //设置文件名
response.setHeader("Content-disposition", "attachment; filename="+new String(filename.getBytes("utf-8"),"iso8859-1"));  
        int b;  
        while ((b = iso.read()) != -1 ) {  
              response.getOutputStream().write(b);
        }  
        response.getOutputStream().flush();  
        if(iso!=null)  
            iso.close();  
}  
%> 

<html>

<head>

<title> </title>

</head>

<script>

//此处定义了

function exportxls(){
 $("stuffList").action="fowardBranch.action";
 $("stuffList").method="post";//注意method必须是post
 $("stuffList").submit();
 $("stuffList").method="get";
}

</script>

</head>

<body>
 <form>

<table>

<tr style="cursor: pointer;" >
                    <td class="STYLE1"><div align="center"></div></td>
                        <td>
                       <input type="button" name="sub" value="" οnclick="exportxls();" style="background-image:url(images/icon31.gif);border:0px;height:26px;width:101px;margin-right:4px;"/>
                        </td>
                  </tr>

</table>

</form>

</body>

</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值