📄 exceloutput.java
字号:
package com.work.excel;
/**
* <p>Title: 公共项目类库</p>
*
* <p>Description: 公共项目类库</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: juling studio</p>
*
* @author wangmj
* @version 1.0
*/
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.work.db.DbUtil;
public class ExcelOutput extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -3567929688799419733L;
private static Log log = LogFactory.getLog(ExcelOutput.class);
public ExcelOutput() {
}
/**
* 实现查询数据输出到excel,供用户下载查看。 暂时写到jsp中。目前这只是一个例子。
*
* @param request
* HttpServletRequest
* @param response
* HttpServletResponse
* @throws ServletException
* @throws IOException
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String type = request.getParameter("type"); // 记录需要导出什么计划
if (type == null) {
return;
}
String fileName = request.getParameter("fileName"); // 首先获取文件名称
if (fileName == null) {
fileName = System.currentTimeMillis() + ""; // 如果文件名称获取失败,那么就以当前时间的毫秒数为文件名
log.warn("获取文件名称失败,暂时用当前时间的毫秒数来代替。");
}
// 首先对文件的名字中的特殊字符进行剔除。
String tempFileName = new String(fileName.getBytes("gb2312"),
"iso_8859_1").replaceAll("[/|\\\\|\\?|\\:|\\*|\"|<|>|\\|]", "");
// 产生excel文件
ExcelCreate ec = new ExcelCreate();
HSSFWorkbook wb = null;
// 表头的名称 ,每一列的类型,然后从数据库中查找到对应的数据值。
// TODO
if ("ndhyjh".equals(type)) { //年度会议计划
String[] tHeadCnName = new String[]{"序号","会议名称","会议内容","承办处室","地点",
"时间","天数","人数","补助金额","经费预算","是否采纳","是否提交","是否请求修改","备注"};
String[] tHeadType = null;
String tempSql = " select xh,hymc,hynr,cbcs,dd,sj,ts,rs,bzbz,jfys,sfcn,sftj,sfqqxg,remark from zx_ndhyjh ";
List valueList = DbUtil.executeQueryStringList(tempSql);
wb = ec.createExcel(tHeadCnName, tHeadType, valueList);
}
response.setContentType("text/html; charset=GBK");
// String wyxq = new String("网元详情".getBytes("gb2312"), "iso_8859_1");
// 设置excel文件名字
response.addHeader("Content-Disposition", "attachment; filename="
+ tempFileName + ".xls");
// 把excel文件输出到客户端
OutputStream out = response.getOutputStream();
try {
wb.write(out);
out.close();
} catch (Exception ex) {
log.debug("Servlet:ExcelOutput, method:doGet(),the error:" + ex);
ex.printStackTrace();
request.setAttribute("message", "生成exclel出错!");
request.getRequestDispatcher("/error.jsp").forward(request,
response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -