spring apache poi 生成excel报表文件。
我不是专业的做报表的,只是在一次偶然的机会中看到这段代码,最一个备份,以后说不定能提供个思路,如果对大家有帮助,欢迎参考:
前台form表单:
<form method="post" action="**.do"> <ul class="**"> <li><input type="submit" value="导出Excel文件"></input></li> </ul> </form>
用户提交请求。
后台接受数据(后台使用的spring mvc这里不是重点,所以不做介绍)
package com.mark.test; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.web.servlet.view.document.AbstractExcelView; public class ExcelView extends AbstractExcelView { @Override protected void buildExcelDocument(Map arg0, HSSFWorkbook arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception { arg3.setContentType("application/octet-stream"); arg3.setHeader("Content-Disposition", "attchment;fileneame=test.xls"); HSSFSheet sheet = arg1.createSheet(); int index = 0; for (int i = 0; i < 10; i++) { HSSFRow row = sheet.createRow(index); } } public void buildCell(HSSFRow row, int index, double value) { HSSFCell cell = row.createCell((short) index, HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(value); } }
其实就是用了一下spring view层的一些API,以前使用spring在这些地方没有下太多的功夫。
1 楼 lyqidao 2010-03-22
朋友希望你能加入到spring的圈子来 和大家一起分享 学习