上代码!
/**
* 下载模板
*
* @throws IOException
*/
@RequestMapping("/exportExcel")
@ResponseBody
public Object exportExcel(HttpServletResponse response) throws IOException {
response.setCharacterEncoding("utf-8");
OutputStream out = null;
//表头列名
List<String> title = new ArrayList<>();
title.add("第一列");
title.add("第二列");
// 2.1创建excel对象
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle stylePALE_BLUE = workbook.createCellStyle();
stylePALE_BLUE.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
stylePALE_BLUE.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 2.2创建工作表sheet
HSSFSheet sheet = workbook.createSheet();
// 添加表头
HSSFRow row = sheet.createRow(0);
// 设置高度
row.setHeight((short) (17.5 * 20));
// 插入第一行数据的表头
HSSFCell cell = null;
HSSFPalette pt = workbook.getCustomPalette();
for (int i = 0; i < title.size(); i++) {
// 宽度
sheet.setColumnWidth(i, 21 * 256);
// 设置样式赋值
cell = row.createCell(i);
pt.setColorAtIndex(HSSFColor.LIME.index, (byte) 0, (byte) 176, (byte) 80);
cell.setCellStyle(stylePALE_BLUE);
cell.setCellValue(title.get(i));
}
out = response.getOutputStream();
String fileName = "test.xls";// 文件名
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(out);
out.close();
return null;
}
戴卡五楼