Java-BufferedImage动态生成一个表格图片
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java-BufferedImage动态生成一个表格图片,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3708字,纯文字阅读大概需要6分钟。
内容图文
![Java-BufferedImage动态生成一个表格图片](/upload/InfoBanner/zyjiaocheng/598/fb8225cf9b19435ebc65eec08d40721c.jpg)
需求:一个word模板,里边有一个表格,需要根据需求动态填充数据,需要转成pdf,但是我发现转成pdf的方式并不方便,想着还不如将动态的部分生成图片,然后由前端整合其他部分用于展示,也满足了需求。
生成完之后的图:(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;
}
内容总结
以上是互联网集市为您收集整理的Java-BufferedImage动态生成一个表格图片全部内容,希望文章能够帮你解决Java-BufferedImage动态生成一个表格图片所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。