JSP导出数据到excel

把页面上的数据导出到excel表格,数据可以是从页面传到后台,也可以是后台查询出来的。


把查询出来的数据传到后台,在后天生成excel表格,然后输出:


<a href='<%=path%>/OrderAction!exportToExcel?exportString="+jsonObject.forexportexcle+"'>导出数据到excel</a>

后台action中代码:

public String <span style="font-family: Arial, Helvetica, sans-serif;">exportToExcel </span>() throws Exception {
			exportString = exportString.substring(1,exportString.length()-1);
			String[] tableHeader = {"日期","件数1", "收入1", "件数2", "收入2"
					, "单数3", "收入3", "单数4", "收入4", "合计收入", "金额合计"};
			short cellNumber = (short) tableHeader.length;// 表的列数
			HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个excel
			HSSFCell cell = null; // Excel的列
			HSSFRow row = null; // Excel的行
			HSSFCellStyle style = workbook.createCellStyle(); // 设置表头的类型
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			HSSFCellStyle style1 = workbook.createCellStyle(); // 设置数据类型
			style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			HSSFFont font = workbook.createFont(); // 设置字体
			HSSFSheet sheet = workbook.createSheet("sheet1"); // 创建一个sheet
			HSSFHeader header = sheet.getHeader();// 设置sheet的头
			try {
				if (exportString==null || exportString.trim().length() == 0 || exportString.equals("")) {
					header.setCenter("查无资料");
				} else {
					header.setCenter("总收入统计表");
					row = sheet.createRow(0);
					row.setHeight((short) 400);
					for (int k = 0; k < cellNumber; k++) {
						cell = row.createCell(k);// 创建第0行第k列
						cell.setCellValue(tableHeader[k]);// 设置第0行第k列的值tableHeader[k]
						sheet.setColumnWidth(k,4000);// 设置列的宽度
						font.setColor(HSSFFont.COLOR_NORMAL); // 设置单元格字体的颜色.
						font.setFontHeight((short) 240); // 设置单元字体高度
						style1.setFont(font);// 设置字体风格
						cell.setCellStyle(style1);
					}
					String[] infostr = exportString.split(",");
					for (int i = 0; i < infostr.length; i++) {
						String[] pointinfo = infostr[i].split("__");  //单个信息
						
						row = sheet.createRow((short) (i + 1));// 创建第i+1行
						row.setHeight((short)260);// 设置行高
						
						cell = row.createCell(0);// 创建第i+1行第0列
						cell.setCellValue(pointinfo[0]);// 设置第i+1行第0列的值
						cell.setCellStyle(style);// 设置风格
					
						cell = row.createCell(1); // 创建第i+1行第1列
						cell.setCellValue(pointinfo[1]);// 设置第i+1行第1列的值
						cell.setCellStyle(style); // 设置风格
					
						cell = row.createCell(2);		//注释同上
						cell.setCellValue(pointinfo[2]);
						cell.setCellStyle(style);
					
						cell = row.createCell(3);
						cell.setCellValue(pointinfo[3]);
						cell.setCellStyle(style);
					
						cell = row.createCell(4);
						cell.setCellValue(pointinfo[4]);
						cell.setCellStyle(style);
					
						cell = row.createCell( 5); // 创建第i+1行第1列
						cell.setCellValue(pointinfo[5]);// 设置第i+1行第1列的值
						cell.setCellStyle(style); // 设置风格
					
						cell = row.createCell( 6);
						cell.setCellValue(pointinfo[6]);
						cell.setCellStyle(style);
					
						cell = row.createCell( 7);
						cell.setCellValue(pointinfo[7]);
						cell.setCellStyle(style);
						
						cell = row.createCell(8);
						cell.setCellValue(pointinfo[8]);
						cell.setCellStyle(style);
					
						cell = row.createCell(9); // 创建第i+1行第1列
						cell.setCellValue(pointinfo[9]);// 设置第i+1行第1列的值
						cell.setCellStyle(style); // 设置风格
					 
						cell = row.createCell(10);
						cell.setCellValue(pointinfo[10]);
						cell.setCellStyle(style);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			HttpServletResponse response = null;// 创建一个HttpServletResponse对象
			OutputStream out = null;// 创建一个输出流对象
			try {
				response = ServletActionContext.getResponse();// 初始化HttpServletResponse对象
				out = response.getOutputStream();//
				String headerStr = "业务总收入统计表";
				headerStr = new String(headerStr.getBytes("gb2312"), "ISO8859-1");// headerString为中文时转码
				response.setHeader("Content-disposition", "attachment; filename="
						+ headerStr + ".xls");		//filename是下载的xls的名,建议最好用英文
				response.setContentType("application/msexcel;charset=UTF-8");// 设置类型
				response.setHeader("Pragma", "No-cache");// 设置头
				response.setHeader("Cache-Control", "no-cache");// 设置头
				response.setDateHeader("Expires", 0);// 设置日期头
				workbook.write(out);
				out.flush();
				workbook.write(out);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (out != null) {
						out.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return null;
		}


此处用的是JSON数据处理格式,其他的数据格式也可以,只要把数据传过来就可以。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值