基于POI文件导出

首先
导入对应jar包,poi开头的6个,导入POI相关包(http://poi.apache.org/)
在这里插入图片描述
然后编辑对应工具类,
public class PoiOutCarteamExcel extends AbstractExcelView {

@Override
protected void buildExcelDocument(Map<String, Object> model,
		HSSFWorkbook workbook, HttpServletRequest request,
		HttpServletResponse response) throws Exception {
	// TODO Auto-generated method stub
	@SuppressWarnings("unchecked")
	List<CarTeams> list = (List<CarTeams>) model.get("teams");
	// 创建Excel的sheet
	HSSFSheet sheet = workbook.createSheet("车队表");
	// 创建标题行
	HSSFRow header = sheet.createRow(0);

	header.createCell(0).setCellValue("车队名称");
	header.createCell(1).setCellValue("车队联系人");
	header.createCell(2).setCellValue("车队电话");
	header.createCell(3).setCellValue("所属公司");

	// 填充数据
	int rowNum = 1;
	for (CarTeams t : list) {
		HSSFRow row = sheet.createRow(rowNum);
		row.createCell(0).setCellValue(t.getTeamName());
		row.createCell(1).setCellValue(t.getChargePerson());
		row.createCell(2).setCellValue(t.getPhone());
		row.createCell(3).setCellValue(t.getCompName());
		rowNum++;
	}
	// 设置相应头信息,以附件形式下载并且指定文件名
	response.setHeader("Content-Disposition", "attachment;filename="
			+ URLEncoder.encode("车队表.xls", "UTF-8"));
}

在前端的Spring配置文件进行配置(###-servlet)
<–bean name=“ExcelView” class=“包名.PoiOut###Excel”/–>
最后在控制层
@RequestMapping("/###")
public ModelAndView exportVoteExcel() {

	Map<String, Object>maps=new HashMap<>();
	List<CarTeams> list = teamsService.searchAll(maps);
	
	ModelAndView model = new ModelAndView();
	model.addObject("####", list);
	model.setViewName("ExcelView");
	
	return model;
}

https://blog.csdn.net/Jul_11th/article/details/70832834

猜你喜欢

转载自blog.csdn.net/ArexSVN/article/details/83626067
今日推荐