操作Excel的经典类

package com.css.common.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.css.common.util.DateUtil;
import com.opensymphony.xwork2.ActionSupport;


public class BaseExcelAction extends ActionSupport implements
  ServletResponseAware, ServletContextAware {
 
 private static final Log log = LogFactory.getLog(BaseExcelAction.class);
 private static final long serialVersionUID = 1L;
 private HttpServletResponse response;
 private ServletContext servletContext;

 protected String excelExport() throws Exception {
  response.setContentType("text/html");
  try {
   String contextTitle = "百强问题调查信息表";
   String realPath = servletContext.getRealPath("/");
   log.debug("realPath=" + realPath);

   List columnContextList = new ArrayList();
   // 拼装
   excelExport(realPath, contextTitle, columnContextList);

  } catch (Exception e) {
   log.fatal("导出问卷信息结束", e);
  }
  return NONE;
 }

 /**
  * 从DB中导出数据到Excel
  */
 protected void excelExport(String realPath, String contextTitle,
   List columnContextList) throws Exception, FileNotFoundException,
   IOException {
  
  // 把数据写入Excel
  if (columnContextList != null) {
   String fileName = DateUtil.getLocalDateTime() + ".xls";
   IExcelExport excel = new QuesExcelExportUtil(contextTitle,columnContextList);
   // 1.输出到指定文件夹
   excel.exportExcel(new File(realPath + fileName));

   log.debug("fileName=" + fileName);
   log.debug("恭喜您!Excel导出成功");

   File file = new File(realPath + fileName);

   long _l = file.length();
   InputStream in = new FileInputStream(file);

   if (in != null) {
    response.reset();
    response.setContentType("application/x-msdownload");
    response.setHeader("Content-disposition","attachment; filename=" + fileName);
    
    // 以上输出文件元信息
    response.setContentLength((int) _l); // 设置输入文件长度
    byte[] _b = new byte[2048];
    int _llen = 0;
    while ((_llen = in.read(_b)) > 0) {
     response.getOutputStream().write(_b, 0, _llen); // 向浏览器输出
    }

    in.close(); // 关闭文件输入流
    response.flushBuffer();
   }

   new File(realPath + fileName).delete();
  }
 }
 
 /**
  * 从Excel中导入数据到DB
  */
 protected void excelImport(){
  //
 }

 public void setServletResponse(HttpServletResponse response) {
  this.response = response;
 }

 public void setServletContext(ServletContext servletContext) {
  this.servletContext = servletContext;
 }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值