问题描述
web开发中,我实现了通过模板来导出excel数据,是通过写一个模板,和一个目标文件来实现的,这两个文件是写在项目里面的,但是如果是多个人查询内容不同,然后同时导出这个文件的时候可能就出现导出的结果不是自己想要的!请问各位大虾门,怎么才能避免这种情况?也可能是我的方法写的不对,希望指点一下!听说导出excel是根据模板虚拟出一个需要导出的excel表格,当保存后,这个表格就没了!我是不懂!下面是我写的一个jxl导出excel:java代码:public class ExportExcel {public void Excel(RequestData req){ //String path =ExportExcel.class.getResource("").getPath(); String realpath = req.getString("path")+"/模板1.xls"; System.out.println(req.getString("path")); try{ Workbook wb = Workbook.getWorkbook(new File(realpath)); //通过模板得到一个可写的Workbook:第一个参数是一个输出流对象,第二个参数代表了要读取的模板 File targetFile = new File(req.getString("path")+"/worksheet1.xls"); WritableWorkbook wwb = Workbook.createWorkbook(targetFile, wb); //选择模板中名称为StateResult的Sheet: WritableSheet wws = wwb.getSheet(0); //如果需要也可以创建Sheet //WritableSheet wws = wwb.createSheet("Sheet名称",1); //选择单元格,写入动态值,根据单元格的不同类型转换成相应类型的单元格: /********************************public*****************************/ jxl.write.NumberFormat format = new jxl.write.NumberFormat("#"); jxl.write.WritableCellFormat wcf1 = new jxl.write.WritableCellFormat(format); wcf1.setBorder(Border.ALL, jxl.format.BorderLineStyle.THIN); //Border是jxl.format.Border wcf1.setAlignment(jxl.format.Alignment.CENTRE);//设置文本对其方式,水平方向 wcf1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); WritableFont fonte= new WritableFont(WritableFont.createFont("宋体"),12,WritableFont.NO_BOLD); jxl.write.WritableCellFormat wcf2 = new jxl.write.WritableCellFormat(fonte); wcf2.setBorder(Border.ALL, jxl.format.BorderLineStyle.THIN); //Border是jxl.format.Border wcf2.setAlignment(jxl.format.Alignment.CENTRE);//设置文本对其方式,水平方向 wcf2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//设置文本对其方式,垂直方向 /********************************public*****************************/ //(列,行) JiaGeQS instance = new JiaGeQS(); DBSet rs = instance.query(req,1); int i=0; int sum=0; while(rs.next()){ jxl.write.Label label00 = new jxl.write.Label(0, 2+i, rs.getString("htbh"),wcf2); wws.addCell(label00); String str1=""; if(rs.getString("YSFS").equals("0")){ str1="铁路"; }else{ str1="公路"; } if(rs.getString("HTKD").equals("0")){ str1 +="年度"; }else if(rs.getString("HTKD").equals("1")){ str1 +="季度"; }else if(rs.getString("HTKD").equals("2")){ str1 +="月度"; }if(rs.getString("GLFL").equals("1")){ str1 +="出省";}else if(rs.getString("GLFL").equals("0")){str1 +="省内";} jxl.write.Label label01 = new jxl.write.Label(1, 2+i, str1,wcf2); wws.addCell(label01); jxl.write.Label label02 = new jxl.write.Label(2, 2+i, rs.getString("cmr"),wcf2); wws.addCell(label02); jxl.write.Label label03 = new jxl.write.Label(3, 2+i, rs.getString("msr"),wcf2); wws.addCell(label03); jxl.write.Label label04 = new jxl.write.Label(4, 2+i, rs.getString("fz")+"-"+rs.getString("dz"),wcf2); wws.addCell(label04); jxl.write.Label label05 = new jxl.write.Label(5, 2+i, rs.getString("a1"),wcf2); wws.addCell(label05); jxl.write.Label label06 = new jxl.write.Label(6, 2+i, rs.getString("a2"),wcf2); wws.addCell(label06); //jxl.write.Label label07 = new jxl.write.Label(7, 2+i, rs.getString("a16"),wcf2); jxl.write.Number number01 = new jxl.write.Number(7,2+i,rs.getInt("a16"),wcf2); wws.addCell(number01); jxl.write.Label label08 = new jxl.write.Label(8, 2+i, rs.getString("a3"),wcf2); wws.addCell(label08); jxl.write.Label label09 = new jxl.write.Label(9, 2+i, rs.getString("qysj"),wcf2); wws.addCell(label09); wws.setRowView(2+i, 370) ; sum +=rs.getInt("a16"); i++; } jxl.write.Label labeli = new jxl.write.Label(0, 2+i, "数量合计:"+sum+" 吨",wcf2); wws.addCell(labeli); wws.mergeCells(0, 2+i,9, 2+i); /********************************数字格式*****************************/ //份数// jxl.write.Number C4 = new jxl.write.Number(1,3,2222,wcf2); // wws.addCell(C4);// C4.setCellFormat(cellFormat); //保单总印量// jxl.write.Number C5 = new jxl.write.Number(1,4,3333,wcf2); // wws.addCell(C5);// C5.setCellFormat(cellFormat); //.................以此类推 //wws.setColumnView(1, 50) ; //设置指定列的宽度 //设置指定行的高度 //wws.mergeCells(1, 1,3, 2) ; //合并单元格(开始列,开始行,结束列,结束行) /**********************************************************************************/ wwb.write(); wwb.close(); wb.close(); }catch(Exception e){ e.printStackTrace(); } }public static void main(String[] args) { try { //upload(); } catch(Exception e) { e.printStackTrace(); } } }jsp代码,是通过js打开写死的路径:function exportExcel(){//window.location.href="<%=request.getContextPath()%>/proj/xls/worksheet1.xls";}
解决方案
worksheet1当文件名,你可以把它换成用户id+时间 比如user20130521163159.xls,然后把文件名传到页面,url根据文件名来下载