poi表报下载 前端加后端及工具类 傻瓜式粘贴就可以用

前台a标签访问
<a class='a' href='<%=path%>frontPageInterface/frontPageInterfaceFormDownload.action?id=1></a>

后台
springmvc 接收
@RequestMapping(value = "frontPageInterfaceFormDownload")
public void frontPageInterfaceFormDownload(HttpServletRequest request, HttpServletResponse response,String id){
try {
Map<String, Object> map=frontPageInterfaceService.frontPageInterfaceFormDownload(id);
int [] rowDouble = (int[]) map.get("rowDouble");
List<String[]> maps = (List<String[]>) map.get("dataList");
String title = (String) map.get("title");
FileExportFormUtil.exportExcel(maps, title,rowDouble, response); //封装方法
} catch (Exception e) {
logger.info("异常---frontPageInterface---exportExcel---"+e.getMessage());
e.printStackTrace();
}
}

export Excel 源码
/**
*
* @Title: exportExcel
* @Description: (这里用一句话描述这个方法的作用)
* @param List 配装一个 List<String[]> dataList 集合 一条就是一行
* @param fileName 标题名
* @param rowsDouble 一个int数组 传一个 0长度就可以
* @param response 正常传 springmvc注解方法就可以
* @throws Exception
* @version XM1200.
*/
@SuppressWarnings("deprecation")
public static void exportExcel(List<String[]> List, String fileName,
int[] rowsDouble, HttpServletResponse response) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");

HSSFFont columnHeadFont = wb.createFont();
columnHeadFont.setFontName("黑体");
columnHeadFont.setFontHeightInPoints((short) 10);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

HSSFCellStyle columnHeadStyle = wb.createCellStyle();
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setBorderLeft((short) 1);// 边框的大小
columnHeadStyle.setBorderRight((short) 1);// 边框的大小

if ((List != null) && (List.size() >= 1)) {
for (int i = 0; i < List.get(0).length; i++) {
sheet.setColumnWidth(toShort(i), toShort(6000));
}

HSSFRow rowFirst = sheet.createRow(0);

HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(toShort(2));
HSSFFont cellFont = wb.createFont();
cellFont.setBoldweight(toShort(700));
cellFont.setFontHeightInPoints(toShort(9));
cellStyle.setFont(cellFont);
cellStyle.setBorderLeft((short) 1);// 边框的大小
cellStyle.setBorderTop((short) 1);// 边框的大小
cellStyle.setBorderRight((short) 1);// 边框的大小
cellStyle.setBorderBottom((short) 1);// 边框的大小

for (int i = 0; i < List.get(0).length; i++) {
HSSFCell cell = rowFirst.createCell(toShort(i));
cell.setCellStyle(columnHeadStyle);
// cell.setEncoding(toShort(1));
cell.setCellType(1);
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(List.get(0)[i]);
}

cellFont.setBoldweight(toShort(400));
cellStyle.setFont(cellFont);

for (int i = 1; i < List.size(); ++i) {
HSSFRow rows = sheet.createRow((short) (i));
for (int j = 0; j < List.get(i).length; j++) {
HSSFCell cell = rows.createCell(toShort(j));
cell.setCellStyle(cellStyle);
// cell.setEncoding(toShort(1));
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellType(1);
if (contians(j, rowsDouble) && !"-".equals(List.get(i)[j])) {
cell.setCellValue(Double.parseDouble(List.get(i)[j]));
} else {
cell.setCellValue(List.get(i)[j]);
}

}
}
}
fileName = new String(fileName.getBytes(), "ISO8859-1");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.addHeader("content-disposition", "attachment;filename=\""
+ fileName + ".xls\"");
response.setCharacterEncoding("utf-8");
OutputStream sos = response.getOutputStream();
wb.write(sos);
if (sos != null)
sos.close();
}


猜你喜欢

转载自blog.csdn.net/a1ccwt/article/details/78049145