⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceloutput.jsp

📁 一个真实项目的源代码。有一个比较优秀的时间类
💻 JSP
字号:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="javax.servlet.http.*,thtf.ebuilder.UP.*"%>
<%@ page import="java.io.*,java.sql.*,javax.sql.DataSource,javax.naming.InitialContext,java.util.Properties,java.util.Hashtable" %> 
<%@ page import="org.apache.log4j.Logger"%>
<%@ page import="java.util.*,com.work.db.*,com.zx.dangangl.*"%>
<%@ page import="com.work.util.DateUtil,com.work.exception.*,com.work.excel.*,org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%
	request.setCharacterEncoding("gb2312");
	String type = request.getParameter("type"); // 记录需要导出什么计划
		if (type == null) {
			return;
		}
		String fileName = request.getParameter("fileName"); // 首先获取文件名称
		if (fileName == null) {
			fileName = System.currentTimeMillis() + ""; // 如果文件名称获取失败,那么就以当前时间的毫秒数为文件名
			System.out.println("获取文件名称失败,暂时用当前时间的毫秒数来代替。");
		}
		// 首先对文件的名字中的特殊字符进行剔除。
	//	String tempFileName = new String(fileName.getBytes("gb2312"),
	//			"iso_8859_1").replaceAll("[/|\\\\|\\?|\\:|\\*|\"|<|>|\\|]", "");
		String tempFileName = fileName.replaceAll("[/|\\\\|\\?|\\:|\\*|\"|<|>|\\|]", "");
		// 产生excel文件

		ExcelCreate ec = new ExcelCreate();
		HSSFWorkbook wb = null;
		// 表头的名称 ,每一列的类型,然后从数据库中查找到对应的数据值。
		// TODO
try {		
		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 ";

			String tempIds = request.getParameter("ids");
			tempIds = tempIds.substring(0,tempIds.length()-1);
			
			tempSql = tempSql + " where id in("+tempIds+")";
			
			List valueList = DbUtil.executeQueryStringList(tempSql);
			wb = ec.createExcel(tHeadCnName, tHeadType, valueList);
		}
		
		
		response.setContentType("text/html; charset=GBK");
		// 设置excel文件名字
		response.addHeader("Content-Disposition", "attachment; filename="
				+ tempFileName + ".xls");
		// 把excel文件输出到客户端
		OutputStream outExcel = response.getOutputStream();

			wb.write(outExcel);
			outExcel.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			request.setAttribute("message", "生成exclel出错!");
			request.getRequestDispatcher("/Application/error.jsp").forward(request,
					response);
		}
%>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -