本教程使用jxls将数据库中的数据导出到excel。
1.jsp也页面调用导出方法
<button type="button" onclick="excelExport()" class="btn btn-primary">导出</button>
//js
function excelExport(){
window.location.href="${ctx}/manage/faePrd/excelDown";
}
- Controller层
@RequestMapping(value = "/excelDown", method = RequestMethod.GET)
public void generateLedger(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> searchParams = CreepersUtil.getParameterMap(request, false);
List<CreepersProductDTO> faePrdList = productService.generateExcelData(searchParams);
Map<String, Object> map = new HashMap<>();
map.put("faePrdList", faePrdList);
new ExcelUtil().export("财富中心报表", map, "/excel/product_fae.xlsx", response);
}
dao层和service层是查询和处理业务逻辑,在此省略。
- ExcelUtil工具类
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;
/**
* <p>
* description:
* </p>
*
* @author
* @since 2017年2月13日
* @see
*/
public class ExcelUtil {
/**
* 根据模板生成Excel文件
*
* @param templateFileName
* 模板文件.
* @param list
* 模板中存放的数据.
* @param resultFileName
* 生成的文件.
* @throws IOException
* @throws InvalidFormatException
* @throws ParsePropertyException
*/
public void createExcel(String templateFileName, Map<String, Object> beanParams, String resultFileName)
throws ParsePropertyException, InvalidFormatException, IOException {
// 创建XLSTransformer对象
XLSTransformer transformer = new XLSTransformer();
// 获取java项目编译后根路径
URL url = this.getClass().getClassLoader().getResource("");
// 得到模板文件路径
String srcFilePath = url.getPath() + templateFileName;
String destFilePath = url.getPath() + resultFileName;
transformer.transformXLS(srcFilePath, beanParams, destFilePath);
}
/**
*
* @param filename
* 导出的文件名
* @param dataMap
* List的集合
* @param model
* @param response
* @throws IOException
*/
public void export(String filename, Map dataMap, String model, HttpServletResponse response) throws Exception {
XLSTransformer transformer = new XLSTransformer();
Workbook workbook = null;
// 此方法需要类与模板放在同一目录下
InputStream in = getClass().getResource(model).openStream();
workbook = transformer.transformXLS(in, dataMap);
outExcel(workbook, response, filename);
}
protected void outExcel(Workbook workbook, HttpServletResponse response, String filename) throws IOException {
response.setContentType("application ns.ms-excel");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setHeader("Content-disposition",
"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "utf-8"));
workbook.write(response.getOutputStream());
}
}
- Excel模板
导出的excel模板放在项目的resources目录下,如图所示:
由于使用的是JXLS方式导出数据,JXLS允许在模板中使用预定义的XML标签来控制XLS转换行为。
<jx:forEach>标签的典型用法如下:
<jx:forEach items="${departments}"var="department">
${department.name}| ${department.chief}
</jx:forEach>
在Excel模板中,第一行为表头。从第二行开始循环插入数据,用EL表达式取值,如下图所示:
在Excel模板中,我们事先将样式都已设定,所以导出数据相对较快。
- 效果图