java后台导出excel_java后台导出Excel

@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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值