JSP页面数据导出为excel 做了一个考勤系统,查询出勤记录时会在页面上以表格的形式列出.请问怎么做能实现一个按钮来导出为单独的excel文件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不知道是不是我理解错了。楼主应该会生成excel文件的方法,不会的话,网上多了去了是不是想说前后台怎么交互啊servlet可以搞定不过比较喜欢用dwr,基于ajax,方便 有歧义么~ 把查询出来已经以表格形式放在页面上的数据 怎么样导出为excel啊~ OutputStream os=response.getOutputStream(); //String fileName = "c:\\aaa.xls";// WritableWorkbook wbook = Workbook.createWorkbook(os); // WritableWorkbook wbook = Workbook.createWorkbook(os); // // 建立excel文件 WritableSheet wsheet = wbook.createSheet("固定资产表", 0); // 工作表名称 // 设置Excel字体 WritableFont wfont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); WritableCellFormat titleFormat = new WritableCellFormat(wfont); String[] title = {"资产编号","部门组织","审批单号","请购单号","设备类别","资产类型","品牌","型号","用户", "序列号","主机名","IP地址","网卡MAC地址" ,"CPU型号","显示器","显示器序列号","内存","硬盘","操作系统", "大区/BU","省办/加工厂","存放地","安装的商用软件","备注","状态","购买日期"}; // 设置Excel表头 for (int i = 0; i < title.length; i++) { Label excelTitle = new Label(i, 0, title[i], titleFormat); wsheet.addCell(excelTitle); } int c = 1; // 用于循环时Excel的行号 for(AssetsPO temp:lsPO){ Label content1 = new Label(0, c, temp.getAssets().getAssetNo()); Label content2 = new Label(1 , c, temp.getAssets().getDeptFullName()); Label content3 = new Label(2 , c, temp.getAssets().getApprovalId()); Label content4 = new Label(3 , c, temp.getAssets().getRequistionId()); Label content5 = new Label(4 , c, temp.getDeviceName()); Label content6 = new Label(5 , c, temp.getAssetsTypeName()); Label content7 = new Label(6 , c, temp.getBrandName()); Label content8 = new Label(7 , c, temp.getModelName()); Label content9 = new Label(8 , c, temp.getAssets().getUserId()); Label content10 = new Label(9 , c, temp.getAssets().getSerialNo()); Label content11 = new Label(10, c, temp.getAssets().getHostName()); Label content12 = new Label(11, c, temp.getAssets().getIp()); Label content13 = new Label(12, c, temp.getAssets().getMac()); Label content14 = new Label(13, c, temp.getAssets().getCpu()); Label content15 = new Label(14, c, temp.getDisplayName()); Label content16 = new Label(15, c, temp.getAssets().getDisplaySerialNo()); Label content17 = new Label(16, c, temp.getAssets().getRam()); Label content18 = new Label(17, c, temp.getAssets().getHardDisk()); Label content19 = new Label(18, c, temp.getOsName()); Label content20 = new Label(19, c, temp.getRegionName()); Label content21 = new Label(20, c, temp.getFactoryName()); Label content22 = new Label(21, c, temp.getAssets().getPlace()); Label content23 = new Label(22, c, temp.getSoftwareNames()); Label content24 = new Label(23, c, temp.getAssets().getRe()); Label content25 = new Label(24, c, temp.getStatusName()); Label content26 = new Label(25, c, new SimpleDateFormat("yyyy-MM-dd").format(temp.getAssets().getPurchaseDate())); wsheet.addCell(content1 ); wsheet.addCell(content2 ); wsheet.addCell(content3 ); wsheet.addCell(content4 ); wsheet.addCell(content5 ); wsheet.addCell(content6 ); wsheet.addCell(content7 ); wsheet.addCell(content8 ); wsheet.addCell(content9 ); wsheet.addCell(content10); wsheet.addCell(content11); wsheet.addCell(content12); wsheet.addCell(content13); wsheet.addCell(content14); wsheet.addCell(content15); wsheet.addCell(content16); wsheet.addCell(content17); wsheet.addCell(content18); wsheet.addCell(content19); wsheet.addCell(content20); wsheet.addCell(content21); wsheet.addCell(content22); wsheet.addCell(content23); wsheet.addCell(content24); wsheet.addCell(content25); wsheet.addCell(content26); c++; } response.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode("固定资产表.xls","UTF-8")); response.setContentType("application/vnd.ms-excel; charset=\"utf-8\""); wbook.write(); // 写入文件 wbook.close();有用的,我在用的 在页面用正常的ACTION或者servlet 后台的时候就把上面的那个代码参考一下你可以在查询出 考勤的数据后 调用一个方法 该方法 就是楼上贴的关键是思路你要清楚思路 就很简单了 Struts2怎么用A标签传对象并封装到Action中? 分页显示的SQL语句应该如何写 数据库连接池的问题 XML的Public ID和System ID!! org.xml.sax.SAXParseException: 缺少文件根组件 从SESSION取得的JAVABEAN对象,不能被序列化,作为EJB的参数来传递,为什么?--谢谢各位,急急急急!急 lookup哪个接口? 解决这个问题,给200分,大恩劳记于心! JDK1.4.0的环境配置问题 -- 在线 菜鸟问题,Cmp和bmp是什么东西? 请教日期解析的高手 一个SVN的问题 求大神
楼主应该会生成excel文件的方法,不会的话,网上多了去了
是不是想说前后台怎么交互啊
servlet可以搞定
不过比较喜欢用dwr,基于ajax,方便
有歧义么~ 把查询出来已经以表格形式放在页面上的数据 怎么样导出为excel啊~
//String fileName = "c:\\aaa.xls";//
WritableWorkbook wbook = Workbook.createWorkbook(os);
// WritableWorkbook wbook = Workbook.createWorkbook(os); //
// 建立excel文件
WritableSheet wsheet = wbook.createSheet("固定资产表", 0); // 工作表名称
// 设置Excel字体
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.BOLD, false,
jxl.format.UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
WritableCellFormat titleFormat = new WritableCellFormat(wfont);
String[] title = {"资产编号","部门组织","审批单号","请购单号","设备类别","资产类型","品牌","型号","用户",
"序列号","主机名","IP地址","网卡MAC地址" ,"CPU型号","显示器","显示器序列号","内存","硬盘","操作系统",
"大区/BU","省办/加工厂","存放地","安装的商用软件","备注","状态","购买日期"};
// 设置Excel表头
for (int i = 0; i < title.length; i++) {
Label excelTitle = new Label(i, 0, title[i], titleFormat);
wsheet.addCell(excelTitle);
}
int c = 1; // 用于循环时Excel的行号
for(AssetsPO temp:lsPO){
Label content1 = new Label(0, c, temp.getAssets().getAssetNo());
Label content2 = new Label(1 , c, temp.getAssets().getDeptFullName());
Label content3 = new Label(2 , c, temp.getAssets().getApprovalId());
Label content4 = new Label(3 , c, temp.getAssets().getRequistionId());
Label content5 = new Label(4 , c, temp.getDeviceName());
Label content6 = new Label(5 , c, temp.getAssetsTypeName());
Label content7 = new Label(6 , c, temp.getBrandName());
Label content8 = new Label(7 , c, temp.getModelName());
Label content9 = new Label(8 , c, temp.getAssets().getUserId());
Label content10 = new Label(9 , c, temp.getAssets().getSerialNo());
Label content11 = new Label(10, c, temp.getAssets().getHostName());
Label content12 = new Label(11, c, temp.getAssets().getIp());
Label content13 = new Label(12, c, temp.getAssets().getMac());
Label content14 = new Label(13, c, temp.getAssets().getCpu());
Label content15 = new Label(14, c, temp.getDisplayName());
Label content16 = new Label(15, c, temp.getAssets().getDisplaySerialNo());
Label content17 = new Label(16, c, temp.getAssets().getRam());
Label content18 = new Label(17, c, temp.getAssets().getHardDisk());
Label content19 = new Label(18, c, temp.getOsName());
Label content20 = new Label(19, c, temp.getRegionName());
Label content21 = new Label(20, c, temp.getFactoryName());
Label content22 = new Label(21, c, temp.getAssets().getPlace());
Label content23 = new Label(22, c, temp.getSoftwareNames());
Label content24 = new Label(23, c, temp.getAssets().getRe());
Label content25 = new Label(24, c, temp.getStatusName());
Label content26 = new Label(25, c, new SimpleDateFormat("yyyy-MM-dd").format(temp.getAssets().getPurchaseDate()));
wsheet.addCell(content1 );
wsheet.addCell(content2 );
wsheet.addCell(content3 );
wsheet.addCell(content4 );
wsheet.addCell(content5 );
wsheet.addCell(content6 );
wsheet.addCell(content7 );
wsheet.addCell(content8 );
wsheet.addCell(content9 );
wsheet.addCell(content10);
wsheet.addCell(content11);
wsheet.addCell(content12);
wsheet.addCell(content13);
wsheet.addCell(content14);
wsheet.addCell(content15);
wsheet.addCell(content16);
wsheet.addCell(content17);
wsheet.addCell(content18);
wsheet.addCell(content19);
wsheet.addCell(content20);
wsheet.addCell(content21);
wsheet.addCell(content22);
wsheet.addCell(content23);
wsheet.addCell(content24);
wsheet.addCell(content25);
wsheet.addCell(content26);
c++;
}
response.setHeader("Content-disposition",
"attachment; filename="+URLEncoder.encode("固定资产表.xls","UTF-8"));
response.setContentType("application/vnd.ms-excel; charset=\"utf-8\"");
wbook.write(); // 写入文件
wbook.close();
有用的,我在用的
你可以在查询出 考勤的数据后 调用一个方法 该方法 就是楼上贴的
关键是思路
你要清楚思路 就很简单了