欢迎光临
我们一直在努力

表格转换成片格式,片转换成电子版表格

前景:

? ?某次的工作内容中需要适配微信小程序的文章正常显示表格内容,但是原公司的采编系统没有一个好的自动适配第三方平台的表格属性转换功能模块。经用户与前端开发探讨商量,觉得使用表格转图片的方式比较稳当的适配第三方平台展示表格数据的问题。

?

技术:

? 使用Html2image的工具方法(HtmlImageGenrator)进行表格转图片操作

<dependency> <groupId>com.github.xuwei-k</groupId> <artifactId>html2image</artifactId> <version>0.1.0</version></dependency>

?

说明:

? ?先将表格的html属性构成一个完整的html页面属性,然后使用HtmlImageGenrator的loadHtml加载下html信息;再使用HtmlImageGenrator的getBufferedImage方法获取到图片的字符串,然后对BufferedImage进行操作。先实例一个新的图片绘画对象BufferedImage,设置各种背景颜色、图片宽度、高度,最后把HtmlImageGenrator.getBufferedImage()的图片信息逐个写入到新的BufferedImage中,再使用ImageIo的write方法输出到对应的目录的图片文件。

代码案例:

//tableContent是表格HTML内容String tableContent=”<table align=”center” bgcolor=”#559eea” cellpadding=”10″ cellspacing=”1″ style=”font-size: 12px; height: 26px; color: rgb(51, 51, 51); line-height: 26px; font-family: 宋体, &quot;arial narrow&quot;, helvetica; font-stretch: normal;” width=”600″><tbody sizcache=”0″ sizset=”0″><tr><td bgcolor=”#559eea” colspan=”2″ style=”margin: 0px; padding: 5px 10px; white-space: nowrap;”><div align=”center” style=”margin: 0px; padding: 0px; height: auto; overflow: visible; font-stretch: normal; font-size: 16px; line-height: 30px; font-family: &quot;Microsoft Yahei&quot;; word-break: break-all;”><font color=”#ffffff”><b>基本性能评价</b></font></div></td></tr><tr><td style=”margin: 0px; padding: 0px; width: 300px; text-align: center; white-space: nowrap; background-color: rgb(236, 240, 248);”>CPU常规性能</td><td bgcolor=”#ffffff” style=”margin: 0px; padding: 0px; text-align: center; white-space: nowrap;”>正常</td></tr><tr><td bgcolor=”#ecf0f8″ style=”margin: 0px; padding: 0px; text-align: center; white-space: nowrap;”>内存性能</td><td bgcolor=”#ffffff” style=”margin: 0px; padding: 0px; text-align: center; white-space: nowrap;”>正常</td></tr><tr><td bgcolor=”#ecf0f8″ style=”margin: 0px; padding: 0px; text-align: center; white-space: nowrap;”>3D游戏性能</td><td bgcolor=”#ffffff” style=”margin: 0px; padding: 0px; text-align: center; white-space: nowrap;”>正常</td></tr><tr><td bgcolor=”#ecf0f8″ style=”margin: 0px; padding: 0px; text-align: center; white-space: nowrap;”>磁盘性能</td><td bgcolor=”#ffffff” style=”margin: 0px; padding: 0px; text-align: center; white-space: nowrap;”>正常</td></tr></tbody></table>”;String htmlTemplate=”<!DOCTYPE html>\n” + “<html lang=\”en\”>\n” + “<head>\n” + ” <meta charset=\”GBK\”>\n” + ” <title>Title</title>\n” + “</head>\n” + “<body>”;htmlTemplate=htmlTemplate+tableContent+”</body>\n” + “</html>”;HtmlImageGenerator imageGenerator = new HtmlImageGenerator();//加载html模版imageGenerator.loadHtml(htmlTemplate);BufferedImage image=imageGenerator.getBufferedImage();//取得图片的长和宽int imgHeight = image.getHeight();int imgWidth = image.getWidth();//取得背景色的代码int c = image.getRGB(3, 3);BufferedImage bi = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_BGR); //设置有8位RGB颜色分量图像//逐点扫描image,把需要透明的颜色setRGB成控件背景色for(int i = 0; i < imgWidth; ++i)//把原图片的内容复制到新的图片,同时把背景设为透明{ for(int j = 0; j < imgHeight; ++j) { if(image.getRGB(i, j) == c) bi.setRGB(i, j, c & 0x00ffffff);//这里把背景设为透明 else bi.setRGB(i, j, image.getRGB(i, j)); }}//存放地址路径String imgName=System.currentTimeMillis() + “.jpg”;String file=”d:/”+imgName ;try { ImageIO.write(bi, “jpg”, new File(file));}catch (Exception e){ e.printStackTrace();}

46352934

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。