java后台导出Excel

很久以前写的,可能有遗漏的配置。
一、加入包maven包
             
org.apache.poi
poi
3.14
二、java代码服务层
@SuppressWarnings({ "unchecked", "resource" })
public void export(HttpServletRequest request, HttpSession session,
HttpServletResponse response, Map map) throws Exception {
JSONObject json = getGzList(map);
                //要导出的结果
List > resultList = (List >) json.get("resultList");
String stationName = map.get("stationName");
if(StringUtils.isNotEmpty(stationName)) {
String[] temp = stationName.split(":");
stationName = temp.length>1?temp[1]:temp[0];
}
response.setContentType("application/vnd.ms-excel"); 
response.setHeader("content-disposition", 
"attachment;filename=gzsheet.xls");
OutputStream fOut = null;
try {
HSSFWorkbook wk = new HSSFWorkbook(); //工作簿
HSSFSheet sheet = wk.createSheet("Sheet1"); // 工作表
sheet.setColumnWidth(0, 5000);
sheet.setColumnWidth(1, 5000);
sheet.setColumnWidth(2, 8000);
sheet.setColumnWidth(3, 5000);
sheet.setColumnWidth(4, 15000);
HSSFCellStyle style = wk.createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
style.setWrapText(true);
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
HSSFFont font = wk.createFont();
font.setColor(HSSFColor.BLACK.index);// HSSFColor.VIOLET.index
// //字体颜色
font.setFontHeightInPoints((short) 20);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体增粗
             //把字体应用到当前的样式
            style.setFont(font);
HSSFCellStyle style1 = wk.createCellStyle(); // 创建单元格样式
style1.setWrapText(true);
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
style1.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 水平
HSSFFont font1 = wk.createFont();
font1.setFontHeightInPoints((short) 12);
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style1.setFont(font1);
 
HSSFCellStyle style2 = wk.createCellStyle(); // 创建单元格样式
style2.setWrapText(true);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
style2.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 水平
HSSFFont font2 = wk.createFont();
font2.setFontHeightInPoints((short) 10);
style2.setFont(font2);
HSSFCellStyle style3 = wk.createCellStyle(); // 创建单元格样式
style3.setWrapText(true);
style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
style3.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 水平
HSSFFont font3 = wk.createFont();
font3.setFontHeightInPoints((short) 10);
style3.setFont(font3);
 
             //////////////////////////////////////////////////////////////////////////////////////
            //第一行
            HSSFRow row = sheet.createRow(0);
row.setHeightInPoints((short)50);
HSSFCell cell = row.createCell(0);
//sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 5));
sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));
cell.setCellStyle(style); // 样式,居中   
cell.setCellValue("故障报表("+ map.get("station") + ":" + stationName + ")");
            
for (int i = 0; i < resultList.size(); i++) {
row = sheet.createRow(i+1); // 第i行
row.setHeightInPoints((short)20);
Map m = resultList.get(i);
cell = row.createCell(0);
cell.setCellValue(m.get("BT").toString());
cell.setCellStyle(style1);
cell = row.createCell(1);
cell.setCellValue(m.get("COUN").toString());
cell.setCellStyle(style3);
cell = row.createCell(2);
cell.setCellValue(m.get("DESCRIPTION").toString());
cell.setCellStyle(style1);
cell = row.createCell(3);
cell.setCellValue(m.get("CXSJ").toString());
cell.setCellStyle(style2);
cell = row.createCell(4);
cell.setCellValue(m.get("SJ").toString());
cell.setCellStyle(style2);
}
fOut = response.getOutputStream();
wk.write(fOut);
fOut.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
fOut.close();
}
}

猜你喜欢

转载自www.cnblogs.com/ygkeke/p/10874240.html