java绘制数据表格并导出为图片格式
好久没写了,其实不是不写,都记在笔记里了,感觉在这里写有点慢而已。。
话不多说直接上代码!!!
本文参考连接:https://blog.csdn.net/weixin_34137799/article/details/91749037,感谢这位老哥,如有侵权,请联系我
1.前端发送请求,后端进行处理下载
/** * @Description : 导出图片<br> * @param:</b> <br> * <br> * @return:</b> <br> * 2020-04-23 */ public void actionExportReport(HttpServletRequest request, HttpServletResponse response) throws Exception { // 1. 获取请求参数 String SID = request.getParameter("SID"); if (SID == null) { SID = ""; } String reportSn = request.getParameter("reportSn"); if (reportSn == null) { reportSn = ""; } // 2. 根据请求参数,取后台数据 Res_inst_checkLineObject lineObject = checkMgr.getObjectById(reportSn); Res_inst_scheduresultObject object = resultMgr.getObjectById(lineObject.getMissionsn()); lineObject.setSchedulNum(object.getSchedulenumber()); ArrayList<Res_inst_checkDetailsObject> arrayList = null; if (lineObject.getVersion().equals("1")) { arrayList = cDetailMgr.queryListBySn(reportSn); } else { arrayList = cDetailMgr.queryListByParentSn(reportSn); } /* * 3. 对取出来的数据进行处理 * ArrayList<ArrayList<String>> 我用这个代替的二位数组,我设计的表是一共8列,所以在每个子List中都放入了8条数据 * getDataList()方法就是处理数据的,代码就不展示了 */ ArrayList<ArrayList<String>> list = getDataList(lineObject, arrayList); /* * 4. 封装了画图类 ImageUtil,用于画图初始化,设置图标题、表格行列数 */ ImageUtil util = new ImageUtil(); BufferedImage bufferedImage = util.drawImage(lineObject.getName(), list.size() ); Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics(); /* * 5. 继续向初始化后的graphics2D,存储刚处理的数据内容 */ Font font = null; int colwidth = (int) ((util.imageWidth - 20) / util.totalcol); for(int n=0;n< list.size();n++){ for(int l=0;l<list.get(n).size();l++){ font = new Font("微软雅黑",Font.PLAIN, 14); graphics2D.setFont(font); graphics2D.setColor(Color.BLACK); if(n == 3){ graphics2D.setFont( new Font("微软雅黑",Font.BOLD, 16)); graphics2D.setColor(Color.RED); graphics2D.setBackground(new Color(135,206,235)); } graphics2D.drawString(list.get(n).get(l), util.startWidth+ colwidth*l+5, util.startHeight+util.rowheight*(n+2)-10); } } /* * 6. 此处定义,用户可自定义保存位置 */ response.setContentType("image/jpeg;charset=gbk"); response.setHeader("Content-Disposition", "attachment;filename=" + new String((new Date().getTime() + ".jpg").getBytes(), "iso-8859-1")); // 2.将图片写到流里,如果指定保存位置,更改response.getOutputStream() 为 自己 的位置就可以 ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); }
2.ImageUtil
/** * @Date : 2020年4月23日 * @author:HuGY * @Description: */ public class ImageUtil { // 竖线的行数 public static final int totalcol = 8; // 图片宽度 public static final int imageWidth = 1850; // 行高 public static final int rowheight = 40; // 起始高度 public static final int startHeight = 10; // 起始宽度 public static final int startWidth = 10; /* * 向表格中画内容 */ public BufferedImage drawImage(String title, int totalrow) throws SQLException{ // 图片高度 int imageHeight = totalrow * rowheight + 50; // 单元格宽度 int colwidth = (int) ((imageWidth - 20) / totalcol); // 1.获取数据 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 = new Font("微软雅黑", Font.BOLD, 16); graphics2D.setFont(font); graphics2D.setColor(Color.RED); // 写标题 graphics2D.drawString(title, startWidth, startHeight + rowheight - 10); return bufferedImage; } }
3. 之前看到一篇博客说,ImageIO不适用于Ajax请求,我没有尝试,我是前端直接访问的这个路径进行下载的。看下效果
由于隐私问题,对数据打马了