POI根据模板导出Excle
2013-04-23 17:05
375 查看
本文用Struts2 + POI
BIZ层
domain
JSP导出代码
ExtJS
临时文件路径
通过Spring注入
/** * <action name="exportBiz" class="com.cdg.action.ZTreeAction" method="exportBiz"> * <result type="json" name="success" /> * </action> * <action name="downloadExcle" class="com.cdg.action.ZTreeAction" method="downloadExcle"> * <result type="stream"> * <param name="contentType">application/vnd.ms-excel</param> * <param name="inputName">stream</param> * <param name="contentDisposition">attachment;filename="${fileName}.xls"</param> * <param name="bufferSize">4096</param> * </result> * </action> * @return */ public String exportBiz() { try { fileName = zTreeService.exportBiz(id); } catch (RuntimeException e) { e.printStackTrace(); } // MD5 return SUCCESS; } /** * 保存报表 * @return */ public String downloadExcle() { try { File file = new File(com.cdg.utils.Config.getInstance().getTempPath() + File.separator + fileName); FileInputStream fis = new FileInputStream(file); byte[] data = new byte[fis.available()]; fis.read(data); stream = new ByteArrayInputStream(data); downloadFileName = "报表"; //这里得转换字符,否则页面有下载异常 fileName = new String(downloadFileName.getBytes("GBK"), "ISO8859-1"); fis.close(); file.delete(); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } private InputStream stream; private String downloadFileName; private String fileName; public InputStream getStream() { return stream; } public String getDownloadFileName() { return downloadFileName; } public void setDownloadFileName(String downloadFileName) { this.downloadFileName = downloadFileName; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; }
BIZ层
private final static String YYYYTMMTDDTHH24 = "yyyy-MM-dd HH:mm:ss"; public static SimpleDateFormat getYYYYTMMTDDTHH24Fmt() { return new SimpleDateFormat(YYYYTMMTDDTHH24); } public String exportBiz(String ids) { String exportFileName = "aa"; try { InputStream template = this .getClass() .getResourceAsStream( "/com/cdg/template/loginLog.xls"); HSSFWorkbook workbook = new HSSFWorkbook(template); String date = getYYYYTMMTDDTHH24Fmt().format(new Date()); Collection<LoginLog> list = new ArrayList<LoginLog>(); LoginLog e = new LoginLog(1,"qqq",21,"123123","123123123"); list.add(e); if (ids != null && !"".equals(ids)) { // list = this.loginLogDao.findCheckBy(ids); } else { // list = this.loginLogDao.findBy(queryObj); } HSSFSheet sheet = workbook.getSheetAt(0); workbook.setSheetName(workbook.getSheetIndex(sheet), "报表"); writeHeadToSheet(sheet, "报表", date); fillReportData(sheet, list); String fileName = com.cdg.utils.Config.getInstance().getTempPath() + File.separator + exportFileName; FileOutputStream fos = new FileOutputStream(fileName); workbook.write(fos); fos.close(); template.close(); } catch (Exception e) { throw new RuntimeException(e); } return exportFileName; } /** * 写头数据 * * @param sheet * @param deptName * @param date */ private void writeHeadToSheet(HSSFSheet sheet, String deptName, String date) { sheet.getRow(1).getCell(5).setCellValue(new HSSFRichTextString(date)); sheet.getRow(1).getCell(2).setCellValue( new HSSFRichTextString(deptName)); } /** * 生成报表数据 * * @param sheet * @param data */ private void fillReportData(HSSFSheet sheet, Collection<LoginLog> data) { int rownum = 2; List<HSSFCellStyle> style = new ArrayList<HSSFCellStyle>(0); HSSFRow rowModel = sheet.getRow(3); for (int i = 0; i < 6; i++) { style.add(rowModel.getCell(i).getCellStyle()); } sheet.removeRow(rowModel); for (LoginLog bean : data) { HSSFRow row = sheet.getRow(++rownum); if (row == null) { row = sheet.createRow(rownum); int column = 1; // 序号 // HSSFCell cell0 = (row.getCell(column) == null ? row // .createCell(column) : row.getCell(column)); // cell0.setCellStyle(style.get(0)); // cell0.setCellValue(new HSSFRichTextString()); // column++; // 1 HSSFCell cell1 = (row.getCell(column) == null ? row .createCell(column) : row.getCell(column)); cell1.setCellStyle(style.get(column)); if (bean.getId() != null) { cell1.setCellValue(new HSSFRichTextString(bean.getId() .toString())); } column++; // 2 HSSFCell cell2 = (row.getCell(column) == null ? row .createCell(column) : row.getCell(column)); cell2.setCellStyle(style.get(column)); if (bean.getName() != null) { cell2.setCellValue(new HSSFRichTextString(bean.getName())); } column++; // 3 HSSFCell cell3 = (row.getCell(column) == null ? row .createCell(column) : row.getCell(column)); cell3.setCellStyle(style.get(column)); if (bean.getAge() != null) { cell3.setCellValue(new HSSFRichTextString(bean.getAge() .toString())); } column++; // 34 HSSFCell cell4 = (row.getCell(column) == null ? row .createCell(column) : row.getCell(column)); cell4.setCellStyle(style.get(column)); if (bean.getPhone() != null) { cell4.setCellValue(new HSSFRichTextString(bean.getPhone())); } column++; // 5 HSSFCell cell5 = (row.getCell(column) == null ? row .createCell(column) : row.getCell(column)); cell5.setCellStyle(style.get(column)); if (bean.getAddress() != null) { cell5.setCellValue(new HSSFRichTextString(bean.getAddress())); } column++; } } }
domain
public class LoginLog { private Integer id; private String name; private Integer age; private String phone; private String address; public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public LoginLog(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public LoginLog(Integer id, String name, Integer age, String phone, String address) { this.id = id; this.name = name; this.age = age; this.phone = phone; this.address = address; } }
JSP导出代码
ExtJS
form.submit({ url: 'exportBiz.action', success: function(form, action) { //保存导出报表 var fileName = action.result.fileName; var downloadFileName = action.result.downloadFileName; var url = "downloadExcle.action?fileName=" + fileName + "&downloadFileName="+downloadFileName; window.location = url; }, failure: function(form, action) { Ext.Msg.alert('Failed', action.result); } });
临时文件路径
import java.io.File; public class Config { // 导出文件临时目录 private String tempPath; private static Config instance; private Config() { instance = this; } public static Config getInstance() { return instance; } public String getTempPath() { return tempPath; } public void setTempPath(String tempPath) { this.tempPath = tempPath; File dir = new File(this.tempPath); dir.mkdirs(); } }
通过Spring注入
<bean id="lscmConfig" class="com.cdg.utils.Config"> <property name="tempPath" value="/tmp/" /> </bean>
相关文章推荐
- POI根据EXCEL模板,修改内容导出新EXCEL
- POI根据EXCEL模板,修改内容导出新EXCEL (只支持HSSF)
- poi根据模板导出excel
- poi根据excel模板文件六步完成替换导出
- POI之根据模板导出word-yellowcong
- java-导入到Excel_POI根据模板(excel表头)导出excel
- poi根据模板导出word(包含图片、动态生成表格、合并单元格)
- Java根据模板导出Excel报表并复制模板生成多个Sheet页
- SpringMVC+POI 实现Excel按模板方式简单导出功能
- 【Apache POI】Java Web根据模板导出word文件
- 根据html模板以另存为方式导出pdf
- java poi 从服务器下载模板写入数据再导出
- JAVA使用POI根据模版导出EXCEL
- 利用JXLS根据模板导出Excel实例教程
- 根据模板导出EXCEL
- java根据模板导出pdf
- Java 根据模板导出Excel时,如何根据后台数据设置复选框的勾选
- Aspose.Cells 根据Excel模板导出数据统计
- java利用POI通过模板导出excel的一个实例
- JAVA使用POI根据模版导出EXCEL