/src/main/java/com/eastrobot/doc/watermark/ExcelProcessor.java

https://github.com/ekoz/kbase-doc · Java · 78 lines · 48 code · 8 blank · 22 comment · 1 complexity · f5e6e96ddbeb0815b821cca841ad1d40 MD5 · raw file

  1. /*
  2. * Power by www.xiaoi.com
  3. */
  4. package com.eastrobot.doc.watermark;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import org.apache.commons.io.FileUtils;
  8. import org.docx4j.openpackaging.exceptions.Docx4JException;
  9. import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
  10. import org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart;
  11. import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
  12. import org.docx4j.relationships.Relationship;
  13. import org.xlsx4j.exceptions.Xlsx4jException;
  14. import org.xlsx4j.sml.CTSheetBackgroundPicture;
  15. /**
  16. * @author <a href="mailto:eko.z@outlook.com">eko.zhan</a>
  17. * @date 2018年9月17日 下午1:48:10
  18. * @version 1.0
  19. */
  20. public class ExcelProcessor extends AbstractProcessor {
  21. private SpreadsheetMLPackage excelMLPackage;
  22. public ExcelProcessor(File file, File imageFile) {
  23. super(file, imageFile);
  24. }
  25. /**
  26. * 传入一个 xlsx 文件,和一个水印文件,给这个 xlsx 文件加水印
  27. * @author eko.zhan at 2018年9月17日 下午2:10:50
  28. * @throws Docx4JException
  29. * @throws Xlsx4jException
  30. */
  31. @Override
  32. public void process() throws WatermarkException {
  33. try {
  34. SpreadsheetMLPackage excelMLPackage = SpreadsheetMLPackage.load(file);
  35. this.excelMLPackage = excelMLPackage;
  36. int size = excelMLPackage.getWorkbookPart().getContents().getSheets().getSheet().size();
  37. for (int i=0;i<size;i++) {
  38. WorksheetPart worksheet = excelMLPackage.getWorkbookPart().getWorksheet(i);
  39. createBgPic(worksheet);
  40. }
  41. excelMLPackage.save(file);
  42. } catch (Docx4JException e) {
  43. throw new WatermarkException("Docx4JException", e);
  44. } catch (Xlsx4jException e) {
  45. throw new WatermarkException("Xlsx4jException", e);
  46. }
  47. }
  48. /**
  49. * 使用水印图片作为excel背景,达到水印效果<但打印时不会生效>
  50. * @param pkg
  51. * @param worksheet
  52. * @throws Docx4JException
  53. * @throws Exception
  54. * @throws IOException
  55. */
  56. private void createBgPic(WorksheetPart worksheet) throws Docx4JException {
  57. CTSheetBackgroundPicture ctSheetBackgroundPicture = org.xlsx4j.jaxb.Context.getsmlObjectFactory().createCTSheetBackgroundPicture();
  58. worksheet.getContents().setPicture(ctSheetBackgroundPicture);
  59. try {
  60. BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(excelMLPackage, worksheet, FileUtils.readFileToByteArray(imageFile));
  61. Relationship sourceRelationship = imagePart.getSourceRelationships().get(0);
  62. String imageRelId = sourceRelationship.getId();
  63. ctSheetBackgroundPicture.setId(imageRelId);
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }