Java导出数据到Excel

1、设置模版,将Excel的数据域设置唯一的标识
2、Java后台
Workbook wb = null;
Row r = null;
Cell c = null;
String appKey = "";
String l = "";
try {
wb = new HSSFWorkbook(new FileInputStream(fcRegPath.toString() + fcRegFileName));
Sheet sheet = wb.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
// 遍历行
while (rows.hasNext()) {
r = rows.next();
Iterator<Cell> cells = r.cellIterator();
// 遍历列
while (cells.hasNext()) {
l = "";
c = cells.next();
// 取得需要替换的Cell
if (c.toString().indexOf("#") > -1) {
appKey = c.toString().replace("#", "");
String dataType = "";
// 数据类型转换
if (appKey.indexOf(":") > -1) {
String keyArray[] = appKey.split(":");
dataType = keyArray[0];
appKey = keyArray[1];
if (keyArray.length == 3) {
l = keyArray[2];
}
}
// 填充数据,json的key必须与模板的key一致
if (jsonNode.has(appKey) && "".equals(l)) {
setCellValue(c, jsonNode.get(appKey), dataType);
continue;
} else if (jsonNode.has(appKey) && !"".equals(l)) {
setCellValue(cells, c, jsonNode.get(appKey), dataType, l);
continue;
}
c.setCellValue("");
}
}
}

// 保存
FileOutputStream fileOut = new FileOutputStream(fcRegPath.toString() + fcRegFileName);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
if (log.isErrorEnabled()) {
log.error("未找到文件!", e);
}
throw new SystemException(e, "未找到文件!");
} catch (IOException e) {
if (log.isErrorEnabled()) {
log.error("文档制作失败!", e);
}
throw new SystemException(e, "文档制作失败!");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值