poi导出报表数据到excel

public String exportExcel3_3() {
// 设置开始导出进度
getSession().setAttribute(“exportedFlag”, “false”);
String realPath = getServletContext().getRealPath("/");// 根目录
String fileName = realPath + “/upload/report/3-3概算清单汇总表.xlsx”;// 模板路径
try {
// 从前台传过的中文乱码
po.setXlmc(new String(po.getXlmc().getBytes(“ISO-8859-1”), “utf-8”));
po.setBdmc(new String(po.getBdmc().getBytes(“ISO-8859-1”), “utf-8”));
list = service.qryTb3_3(po.getXlid(), po.getBdid(), po.getDtid(), po.getQsnf(), po.getQsjd(), “”,“1”);
if (!CollectionUtils.isEmpty(list)) {
InputStream fileInStream = new FileInputStream(fileName);
XSSFWorkbook workbook = new XSSFWorkbook(fileInStream);// 2007
ExcelUtils excelUtils = new ExcelUtils();
int sheet1 = 0;
XSSFSheet sheet = workbook.getSheetAt(sheet1);
int size = list.size();
if (!CollectionUtils.isEmpty(list)) {
String head = " " + po.getQsnf() + " 年 " + po.getQsjd() + " 季度已完工程数量表";
XSSFFont font = workbook.createFont();
font.setUnderline(XSSFFont.U_SINGLE); // 设置下划线
font.setFontHeightInPoints((short) 18); // 设置字体大小
font.setFontName(“宋体”);
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
XSSFFont font1 = workbook.createFont();
font1.setFontHeightInPoints((short) 18); // 设置字体大小
font1.setFontName(“宋体”);
font1.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
XSSFRichTextString richString = new XSSFRichTextString(head);
richString.applyFont(0, 6, font);
richString.applyFont(6, 7, font1);
richString.applyFont(head.indexOf(“年”) + 1, head.indexOf(“季”), font);// 可能存在区间季度
richString.applyFont(head.indexOf(“季”), head.length(), font1);
sheet.getRow(1).getCell(0).setCellValue(richString);
excelUtils.writeToExcel(workbook, sheet1, 2, 0, "工程名称: " + po.getXlmc());
// 标段编号需要专门查找
BdxxEntity bdbh = bdxxService.getBdbh(po.getBdid());
excelUtils.writeToExcel(workbook, sheet1, 2, 3, "标段编号: " + StringUtil.getCode(bdbh.getBdbh()));
excelUtils.writeToExcel(workbook, sheet1, 2, 4, "单位工程名称: " + list.get(0).get(“dtmc”));
excelUtils.writeToExcel(workbook, sheet1, 2, 10, “截止日期:” + DateTime.getCurrentDate_YYYYMMDD());
}
// 设置打印区域
workbook.setPrintArea(0, 0, 11, 0, 5 + size);// sheet//
// index,start//
// column,end//
// column,start//
// row,end row
int num = 6;
for (int i = 0; i < size; i++) {
Map<String, Object> map = list.get(i);
excelUtils.writeToExcel(workbook, sheet1, num, 0, map.get(“indexNo”));
excelUtils.writeToExcel(workbook, sheet1, num, 1, map.get(“xmmc”));
excelUtils.writeToExcel(workbook, sheet1, num, 2, map.get(“jldw”));
excelUtils.writeToExcel(workbook, sheet1, num, 3, map.get(“hszl”));
excelUtils.writeToExcel(workbook, sheet1, num, 4, map.get(“quantity”));
excelUtils.writeToExcel(workbook, sheet1, num, 5, map.get(“bqbfb”));
excelUtils.writeToExcel(workbook, sheet1, num, 6, map.get(“yearQuantity”));
excelUtils.writeToExcel(workbook, sheet1, num, 7, map.get(“bnbfb”));
excelUtils.writeToExcel(workbook, sheet1, num, 8, map.get(“totalQuantity”));
excelUtils.writeToExcel(workbook, sheet1, num, 9, map.get(“klbfb”));
excelUtils.writeToExcel(workbook, sheet1, num, 10, map.get(“syl”));
excelUtils.writeToExcel(workbook, sheet1, num, 11, map.get(“sybfb”));
num++;
}
// sheet.protectSheet(“password”);// 表格加密-加密后表格无法修改
// 重复打印表头
sheet.setRepeatingRows(CellRangeAddress.valueOf(“1:6”));
sheet.setRepeatingColumns(CellRangeAddress.valueOf(“A:L”));
excelUtils.setRowStyle(workbook, sheet1, 6, 0, 5 + size, 11);// 加边框
String upload = MyProperties.getProperties(“download”);
String name = “3-3概算清单计量汇总表.xlsx”;
name = java.net.URLEncoder.encode(name, “UTF-8”);
name = new String(name.getBytes(), “iso-8859-1”);
response.addHeader(“Content-Disposition”, “attachment;filename=” + name);
OutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
// 导出进度结束
getSession().removeAttribute(“exportedFlag”);
} else {
setRequestAttribute(“msg”, “导出表格信息为空”);
return RESULTNO;
}
} catch (Exception e) {
logger.error(“输出3-3概算清单汇总表失败”, e);
setRequestAttribute(“msg”, “输出3-3概算清单汇总表失败”);
return RESULTNO;
}
return null;
}

工具类:
public void writeToExcel(XSSFWorkbook workbook ,int sheetNum, int rowNum, int columnNum, Object value) throws Exception {
try {
XSSFSheet sheet = workbook.getSheetAt(sheetNum);
XSSFRow row = sheet.getRow(rowNum);
if (null == row) {
row = sheet.createRow(rowNum);
}
XSSFCell cell = row.getCell(columnNum);
if (cell == null) {
cell = row.createCell(columnNum);
}
cell.setCellValue(convertString(value));
} catch (Exception e) {
logger.error(e.getMessage(),e);
} finally {
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值