似乎无法将文件直接传上来,我这边提供两个相关信息:
1.用于导入的excel表格,内容和格式都没限制,简单中文+英文+数字即可,如下:
2.Java代码:
- public String createReportImage(TakeMedicineReportsSaveDTO dto) {
- String excelTemplateFilePath = "src/main/resources/static/template.xlsx";
- String outputImagePath = "src/main/resources/static/output.png";
- String outputPath = "src/main/resources/static/";
- try {
- // 打开工作簿
- Workbook workbook = new Workbook();
- workbook.open(excelTemplateFilePath);
- // 选择工作表
- IWorksheet worksheet = workbook.getWorksheets().get(6);
- TakeMedicineReportsImageVO takeMedicineReportsImageVO = createReportsImageVO(dto);
- // 填充数据
- worksheet.getRange("C4").setValue(takeMedicineReportsImageVO.getName());
- worksheet.getRange("C4").getFont().setName("文泉驿微米黑");
- worksheet.getRange("G4").setValue(takeMedicineReportsImageVO.getSex());
- worksheet.getRange("C5").setValue(takeMedicineReportsImageVO.getAge());
- worksheet.getRange("G5").setValue(takeMedicineReportsImageVO.getPhone());
- worksheet.getRange("I4").setValue(takeMedicineReportsImageVO.getDiagnosis());
- worksheet.getRange("C7").setValue(takeMedicineReportsImageVO.getMedicinalDetail());
- worksheet.getRange("C8").setValue(takeMedicineReportsImageVO.getDosis());
- worksheet.getRange("I8").setValue(takeMedicineReportsImageVO.getMedicationMethod());
- worksheet.getRange("C10").setValue(takeMedicineReportsImageVO.getDecoctingSuggestion());
- worksheet.getRange("I10").setValue(takeMedicineReportsImageVO.getMedicationTemperature());
- worksheet.getRange("J15").setValue(datestampConverter(takeMedicineReportsImageVO.getCreateTime()));
- // //保存为图片
- // FileOutputStream outputStream = new FileOutputStream(outputImagePath);
- // worksheet.toImage(outputStream, ImageType.JPG);
- // outputStream.close();
- //转化为图片base64值
- // Save the worksheet as image to a ByteArrayOutputStream
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- worksheet.toImage(outputStream, ImageType.JPG);
- // Get the byte array from the ByteArrayOutputStream
- byte[] imageBytes = outputStream.toByteArray();
- // Convert the byte array to Base64 string
- String base64Image = Base64.getEncoder().encodeToString(imageBytes);
- // Close the ByteArrayOutputStream
- outputStream.close();
- return base64Image;
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
- }
复制代码
|