excel导出
来源:互联网 发布:淘宝客采集软件13.9 编辑:程序博客网 时间:2024/06/02 07:29
java类中
public String exportExcel() throws IOException{ HttpServletRequest request = ServletActionContext.getRequest(); Map beans = new HashMap(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // 填充报表 filePath += "feedBackTemplet.xls"; fileName = "HT.xls"; beans.put("createDate", df.format(new Date())); String sql =""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); /*20160329 yzk 获取归档列表*/ pageInfo.setPageSize(10); sql = " 1=1 "; if (kssj !=null && !"".equals(kssj) && zzsj !=null && !"".equals(zzsj)){ sql += " and qdsj>='"+kssj+"' and qdsj<='"+zzsj+"'"; } if (xmmc !=null && !"".equals(xmmc)){ sql +=" and mc like '%"+xmmc+"%'"; } if (yfmc !=null && !"".equals(yfmc)){ sql +=" and yfmc like '%"+yfmc+"%'"; } if (gdwzid !=null && !"".equals(gdwzid)){ sql +=" and gddm = "+gdwzid; } System.out.println("sql11---"+sql); contractExcelList = contractService.listAllBySql(sql); excelStream = makeReportFromTemplet(request .getRealPath("/WEB-INF") + filePath, beans); if (excelStream == null) { return INPUT; } return "excel"; } public InputStream makeReportFromTemplet(String templetFileName, Map beans) { HSSFWorkbook workbook = new HSSFWorkbook(); String[] title = new String[]{"编号","合同名称","责任单位(或责任人)","签订日期","归档位置","备注"}; HSSFSheet sheet = workbook.createSheet(); HSSFCellStyle style1 = workbook.createCellStyle(); // 设置字体样式 HSSFFont font1 = workbook.createFont(); font1.setColor(HSSFColor.BLACK.index); font1.setFontHeight((short)400); font1.setFontHeightInPoints((short)14); style1.setFont(font1); // 设置表格样式 style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); style1.setBorderTop(HSSFCellStyle.BORDER_THIN); style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); style1.setBorderLeft(HSSFCellStyle.BORDER_THIN); style1.setBorderRight(HSSFCellStyle.BORDER_THIN); HSSFRow row0 = sheet.createRow((short)0); for ( int i =0;i<title.length;i++){ HSSFCell cell0 = row0.createCell((short)i); cell0.setCellValue(new HSSFRichTextString(title[i])); cell0.setCellStyle(style1); } for(int i=0;i<contractExcelList.size();i++){ HSSFRow row = sheet.createRow((short)(i+1)); HSSFCell cell1 = row.createCell((short)0); cell1.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("bh")))); cell1.setCellStyle(style1); HSSFCell cell2 = row.createCell((short)1); cell2.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("mc")))); cell2.setCellStyle(style1); HSSFCell cell3 = row.createCell((short)2); cell3.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("yfmc")))); cell3.setCellStyle(style1); HSSFCell cell4 = row.createCell((short)3); cell4.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("qdsj")))); cell4.setCellStyle(style1); HSSFCell cell5 = row.createCell((short)4); cell5.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("gdmc")).equals("null")?"":contractExcelList.get(i).get("gdmc"))); cell5.setCellStyle(style1); HSSFCell cell6 = row.createCell((short)5); cell6.setCellValue(new HSSFRichTextString(String.valueOf(contractExcelList.get(i).get("bz")).equals("null")?"":contractExcelList.get(i).get("bz"))); cell6.setCellStyle(style1); } /*HSSFRow row1 = sheet.createRow((short)1); HSSFCell cell1 = row1.createCell(0); cell1.setCellStyle(style1); cell1.setCellValue("序号"); HSSFCell cell2 = row1.createCell(1); cell2.setCellStyle(style1); cell2.setCellValue("序号");*/ HSSFRow row = null; int count = 1; //读取文件 try { // 将文件流写入相应流中 ByteArrayOutputStream out = new ByteArrayOutputStream(); workbook.write(out); //返回一个inputstream类型 InputStream inputStream = new ByteArrayInputStream(out.toByteArray()); return inputStream; } catch (Exception e) { JmstChartException.when(true, "写入excel失败!"); } return null; }
struts 配置文件 excelStream,fileName值与类中的一致,且需要getter,setter方法
<action name="contract" class="contractAction"> <result name="excel" type="stream"> <param name="contentType"> application/vnd.ms-excel </param> <param name="inputName">excelStream</param> <param name="contentDisposition"> attachment;filename="${fileName}" </param> <param name="bufferSize">1024</param> </result> </action>
0 0
- 导出EXCEL
- Excel导出
- 导出Excel
- 导出EXCEL
- 导出Excel
- EXCEL导出
- 导出Excel
- 导出excel
- 导出excel
- EXCEL导出
- 导出excel
- 导出Excel
- Excel导出。
- 导出excel
- Excel 导出
- 导出excel
- 导出excel
- 导出excel
- Jenkins安装使用
- 线程的五大状态
- Java上传下载excel、解析Excel、生成Excel的问题
- Android 数据存储(二)之SharedPreferences存储
- MyEclipse Debug模式下无法显示debug信息
- excel导出
- 安卓开发:SQLite数据库存储简析
- 文字也可以这样简单 这样优美的阅读
- 大龄程序员的界定
- Android Studio 中依赖库v4与v7冲突问题
- Android EditText设置默认不弹出软键盘,点击EditText外部隐藏软键盘,EditText获取焦点时隐藏hint
- iOS 修改状态栏的颜色
- lftp使用
- 已知一个几乎有序的数组,几乎有序是指,如果把数组排好顺序的话,每个元素移动的距离可以不超过k,并且k相对于数组来说比较小。请选择一个合适的排序算法针对这个数据进行排序。 给定一个int数组A,同时给定