excel

 生成excel

   action
        /*
     *  生成excel 表格
     */
    public String generateExcel() throws Exception{
        getDownloadFile();
        return SUCCESS;
    }
    @SuppressWarnings("unchecked")
    public InputStream getDownloadFile() {
        liangConditionList = (List<OptNumberDto>)this.getSession().get("liangListss");
        Pagination pagination = new Pagination(pageNo, 10);
        return ServiceManager.agent.getAdminNumberService().getInputStream(
                liangConditionList,pagination);
    }


      //service层的文件流
 
     @SuppressWarnings({ "deprecation", "deprecation", "unchecked" })
 @Override
 public InputStream getInputStream(List<OptNumberDto> liangList, Pagination pagination) {
  HSSFWorkbook wb = new HSSFWorkbook();
  HSSFSheet sheet = wb.createSheet("sheet1");

  HSSFRow row = sheet.createRow(0);

  HSSFCell cell = row.createCell((short) 0);
  cell.setCellValue("靓号号码");

  cell = row.createCell((short) 1);
  cell.setCellValue("消费者名称");

  cell = row.createCell((short) 2);
  cell.setCellValue("号码类型");

  cell = row.createCell((short) 3);
  cell.setCellValue("备注");
      
  cell = row.createCell((short) 4);
  cell.setCellValue("级别");
  
  cell = row.createCell((short) 5);
  cell.setCellValue("靓号价格");
  
  List<OptNumberDto> list= liangList;
        if(list !=null && list.size() >0){
         for (int i = 0; i < list.size(); i++)
      {
       OptNumberDto optDto = list.get(i);

       row = sheet.createRow(i + 1);

       cell = row.createCell((short) 0);
       cell.setCellValue(optDto.getSpNo());
             
       cell = row.createCell((short) 1);
       cell.setCellValue(optDto.getConsuName());
       
       cell = row.createCell((short) 2);
       if(optDto.getSpType() == 1){
         cell.setCellValue("消费者");
       }else if(optDto.getSpType() == 2){
         cell.setCellValue("实体店");
       }
       
       cell = row.createCell((short) 3);
       cell.setCellValue(optDto.getSpComment());

       cell = row.createCell((short) 4);
       cell.setCellValue(optDto.getSpLevel());
       
       cell = row.createCell((short) 5);
       cell.setCellValue(optDto.getSpBuyFee());
      }
        }
    
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  
  try
  {
   wb.write(os);
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }
  
  byte[] content = os.toByteArray();
  
  InputStream is = new ByteArrayInputStream(content);
  
  return is;
 }

   而后在struts.xml 做相应的配置就可以了
     <action name="generateExcel" class="myGoodNumberAction" method="generateExcel">
   <result name="success" type="stream">
         <param name="contentType">application/vnd.ms-excel</param>
         <param name="contentDisposition">attachment;filename="goodNumbers.xls" </param>
         <param name="inputName">downloadFile</param>
         </result>
     </action>

    页面只要写给form调用 generateExcel.do 就可以了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值