struts.xml
<package name="default" namespace="" extends="struts-default"> <action name="exportExcel" class="ExcelExportAction" method="exportExcel"> <result name="success" type="stream"> <param name="contentType">application/vnd.ms-excel;charset=ISO8859-1</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename="1.xls"</param> <param name="bufferSize">4096</param> </result> </action> </package>
ExcelExportAction
public class ExcelExportAction extends ActionSupport { private static final long serialVersionUID = 1L; public String exportExcel() { System.out.println("exportExcel..."); List<Goods> list1 = new ArrayList<Goods>(); String[] l = list.split(","); for (int i = 0; i < l.length; i++) { Goods goods = goodsservice.getgoodsbybh(l[i]); list1.add(goods); } inputStream = us.getExcelInputStream(list1); return "success"; } getter、setter方法,,,
service>>>dao>>>imp
@Override public InputStream getExcelInputStream(List<Goods> list1) { // TODO Auto-generated method stub ByteArrayOutputStream out = new ByteArrayOutputStream(); putDataOnOutputStream(out,list1); return new ByteArrayInputStream(out.toByteArray()); } private void putDataOnOutputStream(OutputStream os,List<Goods> list1) { jxl.write.Label label; WritableWorkbook workbook; try { workbook = Workbook.createWorkbook(os); // 方法一: 使用jxl默认颜色 WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE, Colour.WHITE);// 字体样式 WritableCellFormat wcf = new WritableCellFormat(font); Color color = Color.decode("#4F81BD"); // 自定义的颜色 workbook.setColourRGB(Colour.ORANGE, color.getRed(), color.getGreen(), color.getBlue()); wcf.setBackground(Colour.ORANGE);// 设置单元格的背景颜色 wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式 WritableSheet sheet = workbook.createSheet("Sheet1", 0); sheet.getSettings().setDefaultColumnWidth(15); label = new jxl.write.Label(0, 0, "资产编号(必填)", wcf); sheet.addCell(label); label = new jxl.write.Label(1, 0, "资产名称(必填)", wcf); sheet.addCell(label); label = new jxl.write.Label(2, 0, "标签编号(必填)", wcf); sheet.addCell(label); label = new jxl.write.Label(3, 0, "施工类型(必填)", wcf); sheet.addCell(label); label = new jxl.write.Label(4, 0, "工具大类(必填)", wcf); sheet.addCell(label); label = new jxl.write.Label(5, 0, "工具小类(必填)", wcf); sheet.addCell(label); label = new jxl.write.Label(6, 0, "摆放货品柜(必填)", wcf); sheet.addCell(label); label = new jxl.write.Label(7, 0, "库存量(必填)", wcf); sheet.addCell(label); label = new jxl.write.Label(8, 0, "单位", wcf); sheet.addCell(label); label = new jxl.write.Label(9, 0, "类型名称", wcf); sheet.addCell(label); label = new jxl.write.Label(10, 0, "规格型号", wcf); sheet.addCell(label); label = new jxl.write.Label(11, 0, "电压等级", wcf); sheet.addCell(label); label = new jxl.write.Label(12, 0, "资产排序", wcf); sheet.addCell(label); label = new jxl.write.Label(13, 0, "出厂日期", wcf); sheet.addCell(label); label = new jxl.write.Label(14, 0, "生产厂家", wcf); sheet.addCell(label); label = new jxl.write.Label(15, 0, "投入日期", wcf); sheet.addCell(label); label = new jxl.write.Label(16, 0, "保管人", wcf); sheet.addCell(label); label = new jxl.write.Label(17, 0, "对象名称", wcf); sheet.addCell(label); label = new jxl.write.Label(18, 0, "对象说明", wcf); sheet.addCell(label); label = new jxl.write.Label(19, 0, "备注", wcf); sheet.addCell(label); WritableFont font1 = new WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.NO_BOLD);// 字体样式 WritableCellFormat wcf1 = new WritableCellFormat(font1); wcf1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式 System.out.println("1"); for (int i = 0; i < list1.size(); i++) { label = new jxl.write.Label(0, i+1, list1.get(i).getGoods_bh(), wcf1); sheet.addCell(label); label = new jxl.write.Label(1, i+1, list1.get(i).getGoods_name(), wcf1); sheet.addCell(label); label = new jxl.write.Label(2, i+1, list1.get(i).getGoods_bqbh(), wcf1); sheet.addCell(label); label = new jxl.write.Label(3, i+1, list1.get(i).getGoods_sglx(), wcf1); sheet.addCell(label); label = new jxl.write.Label(4, i+1, list1.get(i).getGoods_lagerid(), wcf1); sheet.addCell(label); label = new jxl.write.Label(5, i+1, list1.get(i).getGoods_smallid(), wcf1); sheet.addCell(label); label = new jxl.write.Label(6, i+1, list1.get(i).getGoods_bfhpg(), wcf1); sheet.addCell(label); label = new jxl.write.Label(7, i+1, list1.get(i).getGoods_ckl(), wcf1); sheet.addCell(label); label = new jxl.write.Label(8, i+1, list1.get(i).getGoods_dw(), wcf1); sheet.addCell(label); label = new jxl.write.Label(9, i+1, list1.get(i).getGoods_lxmc(), wcf1); sheet.addCell(label); label = new jxl.write.Label(10, i+1, list1.get(i).getGoods_ggxh(), wcf1); sheet.addCell(label); label = new jxl.write.Label(11, i+1, list1.get(i).getGoods_dydj(), wcf1); sheet.addCell(label); label = new jxl.write.Label(12, i+1, list1.get(i).getGoods_zcpx(), wcf1); sheet.addCell(label); label = new jxl.write.Label(13, i+1, list1.get(i).getGoods_ccrq(), wcf1); sheet.addCell(label); label = new jxl.write.Label(14, i+1, list1.get(i).getGoods_sccj(), wcf1); sheet.addCell(label); label = new jxl.write.Label(15, i+1, list1.get(i).getGoods_trrq(), wcf1); sheet.addCell(label); label = new jxl.write.Label(16, i+1, list1.get(i).getGoods_bgr(), wcf1); sheet.addCell(label); label = new jxl.write.Label(17, i+1, list1.get(i).getGoods_dxmc(), wcf1); sheet.addCell(label); label = new jxl.write.Label(18, i+1, list1.get(i).getGoods_dxsm(), wcf1); sheet.addCell(label); label = new jxl.write.Label(19, i+1, list1.get(i).getGoods_bz(), wcf1); sheet.addCell(label); } workbook.write(); workbook.close(); } catch (Exception e) { e.printStackTrace(); } }
JSP
<form action="exportExcel.action" method="get"> <input type="text" id="list" name="list"> <input type="submit" id="sub" value="Submit" /> </form>
JS
$("#export").on('click', function(){ var t = 0; var all = ""; var u = document.getElementsByName("cha"); for ( var i = 0; i < u.length; i++) { if (u[i].checked == true) { t++; all += u[i].value + ","; } } all = all.substring(0, all.length-1); if (t == 0) { layer.msg('请勾选要导出的数据!', {icon : 7,shade : [ 0.4, '#000' ],time : 1500}); }else{ $("#list").val(all); $("#sub").click(); } });
因为ajax好像没用,所以还是选择用form表单提交,因为要传值到后台,所以,在JS里面为表单里的input赋值,并触发submit提交,再在后台接受form表单的值。(记得前台将form表单隐藏起来)