需求:一个word模板,里边有一个表格,需要根据需求动态填充数据,需要转成pdf,但是我发现转成pdf的方式并不方便,想着还不如将动态的部分生成图片,然后由前端整合其他部分用于展示,也满足了需求。
word生成png-效果图
word生成png-结果图
生成完之后的图:(ps:字体大小和颜色啥的都可以自己变动,此仅仅是完成了功能)
核心代码
数据整合部分
public String actionExportReport(HttpServletRequest request, List<Map<String, String>> mapList,
Double totalAmount) {
//中间核心值
ArrayList<List<String>> list = new ArrayList<>();
mapList.stream().forEach(e -> {
Collection<String> stringList = e.values();
List<String> listString = new ArrayList<>(stringList);
list.add(listString);
});
//表格第一行的名称
ArrayList<List<String>> listNew = new ArrayList<>();
List<String> titleList = new ArrayList<>();
titleList.add("名称");
titleList.add("规格");
titleList.add("单位");
titleList.add("单价");
titleList.add("数量");
titleList.add("金额(元)");
titleList.add("备注");
listNew.add(titleList);
listNew.addAll(list);
//编写最后一行的数值统计
List<String> statisticsList = new ArrayList<>();
statisticsList.add("合计");
statisticsList.add("大写");
String amount = String.format("%.2f", totalAmount);
statisticsList.add(numberUppercase.toChinese(amount));
statisticsList.add("");
statisticsList.add("金额");
String amountString = "¥" + amount + "元";
statisticsList.add(amountString);
listNew.add(statisticsList);
//核心代码
BufferedImage bufferedImage = ImageQrCoderShare.wordPngBase(listNew);
String shareImageUrl;
if (null != bufferedImage) {
//存储服务器代码,忽略
String urlPath = "/images/contract/info";
shareImageUrl = getImageUrl(bufferedImage, request,111L, urlPath);
} else {
shareImageUrl = "";
}
return shareImageUrl;
}
表格设置部分
public static BufferedImage wordPngBase(ArrayList<List<String>> list) {
//封装了画图类 用于画图初始化,设置图标题、表格行列数
BufferedImage bufferedImage = ImageQrCoderShare
.drawImage("一、买卖物名称、数量、价款(货物采购计划单):", list.size());
Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
//继续向初始化后的graphics2D,存储刚处理的数据内容
Font font;
int colwidth = ((ImageQrCoderShare.imageWidth - 20) / ImageQrCoderShare.totalcol);
for (int n = 0; n < list.size(); n++) {
for (int l = 0; l < list.get(n).size(); l++) {
//封装的字体类,可以直接new就行
font = definedFontPF.deriveFont(Font.PLAIN, 16);
graphics2D.setFont(font);
graphics2D.setColor(Color.BLACK);
//循环输出,根据表格设置的列设置宽度,也就是起始点
graphics2D.drawString(
String.valueOf(list.get(n).get(l)), ImageQrCoderShare.startWidth + colwidth * l + 5,
ImageQrCoderShare.startHeight + ImageQrCoderShare.rowheight * (n + 2) - 10);
}
}
return bufferedImage;
}
数据动态生成部分
//封装了画图类 用于画图初始化,设置图标题、表格行列数
public static BufferedImage drawImage(String title, int totalRow) {
// 图片高度
int imageHeight = totalRow * rowheight + 50;
// 单元格宽度
int colwidth = ((imageWidth - 20) / totalcol);
BufferedImage bufferedImage = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
graphics2D.setColor(Color.WHITE);
graphics2D.fillRect(0, 0, imageWidth, imageHeight);
graphics2D.setColor(new Color(220, 240, 240));
for (int j = 0; j < totalRow; j++) {
graphics2D.setColor(Color.black);
graphics2D
.drawLine(startWidth, startHeight + (j + 1) * rowheight, startWidth + colwidth * totalcol,
startHeight + (j + 1) * rowheight);
}
// 画竖线
for (int k = 0; k < totalcol + 1; k++) {
graphics2D.setColor(Color.black);
graphics2D
.drawLine(startWidth + k * colwidth, startHeight + rowheight, startWidth + k * colwidth,
startHeight + rowheight * totalRow);
}
// 设置字体
Font font = definedFontPF.deriveFont(Font.PLAIN, 24);
graphics2D.setFont(font);
graphics2D.setColor(Color.black);
// 写标题
graphics2D.drawString(title, startWidth, startHeight + rowheight - 10);
return bufferedImage;
}