spring mvc、struts excel导入导出

1、poi,jxl

2、导入excel的日志,生成text类型日志下载后会直接打开,可能会乱码(浏览器默认编码不一样),换成生成word类型日志下载解决

    File file = new File(logFilename);

    byte[] b= ("<html>"+logs.toString()+"</html>").getBytes("gbk");
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    POIFSFileSystem fs = new POIFSFileSystem();
    fs.createDocument(bais, "WordDocument");  
    
    FileOutputStream ostream = new FileOutputStream(file);
    fs.writeFilesystem(ostream);
    ostream.close();
    bais.close();

3、spring mvc excel导出

 @RequestMapping(value="exportBindCode",method=RequestMethod.GET)
 public  ModelAndView exportBindCode(ModelMap model,DeviceBindCode deviceBindCode){
  ModelAndView mav = new ModelAndView();
  try{
   String device_version = deviceBindCode.getDevice_version();
   Integer recordTotal = deviceBindCodeService.queryDeviceBindCodeCount(deviceBindCode);
   List<DeviceBindCode> deviceBindCodeList = new ArrayList<DeviceBindCode>();
   if(recordTotal != null && recordTotal >=0){
    deviceBindCodeList =deviceBindCodeService.queryDeviceBindCodeList(deviceBindCode);
   }

   model.put("deviceBindCodeList", deviceBindCodeList);
   model.put("device_version", device_version);
   DeviceBindCodeExcelView excelView = new DeviceBindCodeExcelView();
   return new ModelAndView(excelView,model);
  }catch(Exception ex){
      logger.error(ex.getMessage(),ex);
  }
  return mav;
 }

 

public class DeviceBindCodeExcelView extends AbstractExcelView {

 @Override
 protected void buildExcelDocument(Map<String, Object> map, HSSFWorkbook workbook, HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  String device_version = (String)map.get("device_version");
  List<DeviceBindCode>  deviceBindCodeList = (List<DeviceBindCode>)map.get("deviceBindCodeList");

 

---------excel操作,写入excel文件

HSSFSheet sheet = workbook.createSheet("sheet名");

 

  response.setContentType("application/vnd.ms-excel");
  String filename = new String("excel文件名.xls".getBytes("GBK"), "ISO-8859-1");
  response.setHeader("Content-disposition", "attachment;filename=" + filename);    
  OutputStream ouputStream = response.getOutputStream();    
  workbook.write(ouputStream);    
  ouputStream.flush();    
  ouputStream.close(); 

}

 

4、struts导出excel

struts.xml配置

    <action name="TestAction_*" class="TestAction" method="{1}">
     <result type="json" name="success"/>
     <result  name="export" type="stream">
      <param name="contentType">application/vnd.ms-excel</param>
      <param name="inputName">excelStream</param>
      <param name="contentDisposition">attachment;filename="${filename}.xls"</param>
      <param name="bufferSize">1024</param>
     </result>
     <result  name="fresh">/jsp/test.jsp</result>
    </action>

Action的处理方法

public String export() {

           HSSFWorkbook workbook = new HSSFWorkbook();
          HSSFSheet sheet = workbook.createSheet("sheet名");
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          workbook.write(out);
          out.flush();
   
           filename = new String("excel文件名".getBytes("GBK"), "ISO-8859-1");
           byte[] content = out.toByteArray();
           out.close(); 
           excelStream=new ByteArrayInputStream(content);
 
          return "export";

}

filename ,excelStream为Action属性

 private String filename;

private ByteArrayInputStream excelStream

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值