POI Excel插入图片(网络路径、本地路径)

淡淡的烟草味﹌ 2023-09-30 04:19 89阅读 0赞

1 Maven依赖

  1. <!-- easyExcel Excel文档处理工具 -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>easyexcel</artifactId>
  5. <version>2.2.8</version>
  6. </dependency>
  7. <!-- hutool工具包 -->
  8. <dependency>
  9. <groupId>cn.hutool</groupId>
  10. <artifactId>hutool-all</artifactId>
  11. <version>5.5.7</version>
  12. </dependency>

2 实现代码

  1. /**
  2. * 插入网络图片(单个单元格)
  3. *
  4. * @param workbook 文档对象
  5. * @param sheet sheet页对象
  6. * @param url 网络路径
  7. * @param rowIndex 行号
  8. * @param colIndex 开始列号
  9. */
  10. public static void insertUrlImg(Workbook workbook, Sheet sheet, String url, int rowIndex, int colIndex) {
  11. insertUrlImg(workbook, sheet, url, rowIndex, rowIndex, colIndex, colIndex);
  12. }
  13. /**
  14. * 插入网络图片
  15. *
  16. * @param workbook 文档对象
  17. * @param sheet sheet页对象
  18. * @param url 网络路径
  19. * @param beginRowIndex 开始行号
  20. * @param endRowIndex 结束行号
  21. * @param beginColIndex 开始列号
  22. * @param endColIndex 结束列号
  23. */
  24. public static void insertUrlImg(Workbook workbook, Sheet sheet, String url, int beginRowIndex, int endRowIndex
  25. , int beginColIndex, int endColIndex) {
  26. byte[] bytes = HttpUtil.downloadBytes(url);
  27. insertImg(workbook, sheet, bytes, beginRowIndex, endRowIndex, beginColIndex, endColIndex);
  28. }
  29. /**
  30. * 插入本地图片(单个单元格)
  31. *
  32. * @param workbook 文档对象
  33. * @param sheet sheet页对象
  34. * @param url 本地路径
  35. * @param rowIndex 行号
  36. * @param colIndex 开始列号
  37. */
  38. public static void insertFileImg(Workbook workbook, Sheet sheet, String url, int rowIndex, int colIndex) throws Exception {
  39. insertFileImg(workbook, sheet, url, rowIndex, rowIndex, colIndex, colIndex);
  40. }
  41. /**
  42. * 插入本地图片
  43. *
  44. * @param workbook 文档对象
  45. * @param sheet sheet页对象
  46. * @param url 本地路径
  47. * @param beginRowIndex 开始行号
  48. * @param endRowIndex 结束行号
  49. * @param beginColIndex 开始列号
  50. * @param endColIndex 结束列号
  51. */
  52. public static void insertFileImg(Workbook workbook, Sheet sheet, String url, int beginRowIndex, int endRowIndex
  53. , int beginColIndex, int endColIndex) throws Exception {
  54. //将本地路径的图片变成二进制流
  55. FileInputStream fileInputStream = new FileInputStream(new File(url));
  56. byte[] bytes = IoUtil.readBytes(fileInputStream);
  57. fileInputStream.close();
  58. //插入图片
  59. insertImg(workbook, sheet, bytes, beginRowIndex, endRowIndex, beginColIndex, endColIndex);
  60. }
  61. /**
  62. * 插入图片
  63. *
  64. * @param workbook 文档对象
  65. * @param sheet sheet页对象
  66. * @param picture 图片二进制流数组
  67. * @param beginRowIndex 开始行号
  68. * @param endRowIndex 结束行号
  69. * @param beginColIndex 开始列号
  70. * @param endColIndex 结束列号
  71. */
  72. public static void insertImg(Workbook workbook, Sheet sheet, byte[] picture, int beginRowIndex, int endRowIndex
  73. , int beginColIndex, int endColIndex) {
  74. //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
  75. Drawing drawing = sheet.getDrawingPatriarch();
  76. if (drawing == null) {
  77. drawing = sheet.createDrawingPatriarch();
  78. }
  79. //anchor主要用于设置图片的属性
  80. ClientAnchor anchor = null;
  81. if (workbook instanceof HSSFWorkbook) {
  82. anchor = new HSSFClientAnchor(0, 0, 255, 255, (short) beginColIndex, beginRowIndex, (short) (endColIndex + 1), endRowIndex + 1);
  83. } else if (workbook instanceof XSSFWorkbook) {
  84. anchor = new XSSFClientAnchor(0, 0, 255, 255, (short) beginColIndex, beginRowIndex, (short) (endColIndex + 1), endRowIndex + 1);
  85. }
  86. //插入图片
  87. drawing.createPicture(anchor, workbook.addPicture(picture, Workbook.PICTURE_TYPE_JPEG));
  88. }

3 调试代码

  1. /**
  2. * 测试插入网络图片
  3. */
  4. @Test
  5. public void testInsertUrlImg() {
  6. try {
  7. XSSFWorkbook workbook = new XSSFWorkbook();
  8. Sheet sheet = workbook.createSheet();
  9. File file = new File("D:/easyexcel/testInsertUrlImg.xlsx");
  10. FileUtil.createNewFile(file);
  11. String imgUrl = "https://profile.csdnimg.cn/9/5/B/1_qq_38974638";
  12. //插入图片
  13. POIExcelUtil.insertUrlImg(workbook, sheet, imgUrl, 0, 3, 0, 1);
  14. workbook.write(new FileOutputStream(file));
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. /**
  20. * 测试插入本地图片
  21. */
  22. @Test
  23. public void testInsertFileImg() {
  24. try {
  25. XSSFWorkbook workbook = new XSSFWorkbook();
  26. Sheet sheet = workbook.createSheet();
  27. File file = new File("D:/easyexcel/testInsertFileImg.xlsx");
  28. FileUtil.createNewFile(file);
  29. String imgUrl = "C:\\Users\\22185\\Desktop\\875dc98183344af3843c7ac619154c6f_qq_38974638.jpg";
  30. //插入图片
  31. POIExcelUtil.insertFileImg(workbook, sheet, imgUrl, 0, 3, 0, 1);
  32. workbook.write(new FileOutputStream(file));
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }

4 调试结果

watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBA5pet5Lic5oCq_size_17_color_FFFFFF_t_70_g_se_x_16

注:

(1)支持图片地址为网络路径或网络路径,暂时只支持jpg格式图片其他格式图片未测试。

(2)源码请查看Gitee。

xudongbase: 主要是项目中可以用到的共通方法favicon-9007bd527d8a7851c8330e783151df58.icohttps://gitee.com/xudong\_master/xudongbase

发表评论

表情:
评论列表 (有 0 条评论,89人围观)

还没有评论,来说两句吧...

相关阅读