Echarts图表Java后端生成Base64图片格式,POI写入Base64图片到Word中

Echarts图表Java后端生成请看上篇,此篇为Base64图片插入Word文档中
Java后台生成ECharts图片,并以Base64字符串返回_青冘的博客-CSDN博客

 


		
try {
			XWPFParagraph xwpfParagraphimage = doc.createParagraph(); // 创建图片段落
			xwpfParagraphimage.setAlignment(ParagraphAlignment.CENTER);
			XWPFRun xwpfRunimage = xwpfParagraphimage.createRun(); // 创建段落文本
			String jsonData = echartsOption.getJsonDataZCRLQK();//获取OPT的Json配置
			EchartData echartData = new Gson().fromJson(EchartData2Base64.echartToBase64(jsonData), EchartData.class);
			if (echartData.getCode() == 1) {
//   依赖包sun.misc.BASE64Decoder
				BASE64Decoder decoder = new BASE64Decoder();
				byte[] decoderBytes = decoder.decodeBuffer(echartData.getData());
				InputStream imageis = new ByteArrayInputStream(decoderBytes);
				BufferedImage imagebi = ImageIO.read(new ByteArrayInputStream(decoderBytes));
				xwpfRunimage.addPicture(imageis, Document.PICTURE_TYPE_PNG, "",
					Units.pixelToEMU(Math.min(imagebi.getWidth(), 360)), Units.pixelToEMU(Math.min(imagebi.getHeight(), 240)));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是Java代码示例,可以将一个文件夹所有的图片转换成base64格式,并将结果保存到Excel: ```java import java.io.*; import java.util.Base64; import org.apache.poi.ss.usermodel.*; public class ImageToBase64Excel { public static void main(String[] args) { String folderPath = "C:/images"; // 图片所在文件夹路径 String excelPath = "C:/images.xlsx"; // 保存Excel文件路径 // 创建Excel工作簿 Workbook workbook = WorkbookFactory.create(new File(excelPath)); Sheet sheet = workbook.createSheet("Images"); int rowNum = 0; // 遍历文件夹的所有图片文件 File folder = new File(folderPath); File[] files = folder.listFiles(); for (File file : files) { if (file.isFile() && file.getName().toLowerCase().endsWith(".jpg")) { // 读取图片文件并转换成base64格式 byte[] imageBytes = readBytesFromFile(file); String base64String = Base64.getEncoder().encodeToString(imageBytes); // 保存base64格式到Excel Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(file.getName()); row.createCell(1).setCellValue(base64String); } } // 保存Excel文件 try (FileOutputStream outputStream = new FileOutputStream(excelPath)) { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } } // 读取文件字节流 private static byte[] readBytesFromFile(File file) { try (FileInputStream inputStream = new FileInputStream(file)) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } return outputStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); return null; } } } ``` 该代码使用了Apache POI库来创建和操作Excel文件。在遍历文件夹的所有图片文件时,只处理以“.jpg”为后缀的文件。对于每个图片文件,先读取其字节流,再将其转换成base64格式。最后,将文件名和base64格式保存到Excel文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值