当前位置:   article > 正文

java读写word表格、图片_java ,获取word文档表格中的图片

java ,获取word文档表格中的图片

读取word文本

  1. package docx;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.util.List;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.apache.poi.xwpf.usermodel.BodyElementType;
  7. import org.apache.poi.xwpf.usermodel.IBodyElement;
  8. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  9. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  10. import org.apache.poi.xwpf.usermodel.XWPFRun;
  11. /**
  12. * 读文本
  13. */
  14. public class TextRead {
  15. public static void main(String[] args) throws Exception {
  16. readDocx();
  17. }
  18. public static void readDocx() throws Exception{
  19. InputStream is;
  20. is = new FileInputStream("test.docx");
  21. XWPFDocument xwpf = new XWPFDocument(is);
  22. List<IBodyElement> ibs = xwpf.getBodyElements();
  23. for(IBodyElement ib : ibs) {
  24. BodyElementType elementType = ib.getElementType();
  25. //表格
  26. if(elementType == BodyElementType.TABLE) {
  27. System.out.println("table"+ib.getPart());
  28. }else {
  29. //段落
  30. XWPFParagraph para = (XWPFParagraph) ib;
  31. System.out.println("It is a new paragraph...The indention is "+para.getFirstLineIndent());
  32. List<XWPFRun> runs = para.getRuns();
  33. System.out.println("run");
  34. if(runs.size() <= 0) {
  35. System.out.println("empty line");
  36. }
  37. for(XWPFRun run : runs) {
  38. //如果片段没有文字,可能该片段是图片
  39. if(StringUtils.isEmpty(run.text())) {
  40. //该片段是图片时
  41. if(run.getEmbeddedPictures().size() > 0) {
  42. System.out.println("image***"+run.getEmbeddedPictures());
  43. }else {
  44. System.out.println("objects:"+run.getCTR().getObjectList());
  45. //公式
  46. if(run.getCTR().xmlText().indexOf("instrText") > 0) {
  47. System.out.println("there is an equation field");
  48. }
  49. }
  50. }else {
  51. System.out.println("==="+run.getCharacterSpacing()+run.text());
  52. }
  53. }
  54. }
  55. }
  56. is.close();
  57. }
  58. }

读取word图片

  1. package docx;
  2. import java.awt.image.BufferedImage;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import javax.imageio.ImageIO;
  8. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  9. import org.apache.poi.openxml4j.opc.OPCPackage;
  10. import org.apache.poi.xwpf.usermodel.Document;
  11. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  12. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  13. import org.apache.poi.xwpf.usermodel.XWPFPicture;
  14. import org.apache.poi.xwpf.usermodel.XWPFRun;
  15. /**
  16. * 图片读取
  17. */
  18. public class ImageRead {
  19. public static void imageRead() throws IOException, InvalidFormatException {
  20. File docFile = new File("simple.docx");
  21. XWPFDocument doc = new XWPFDocument(OPCPackage.openOrCreate(docFile));
  22. int i = 0;
  23. for (XWPFParagraph p : doc.getParagraphs()) {
  24. for (XWPFRun run : p.getRuns()) {
  25. System.out.println("a new run");
  26. for (XWPFPicture pic : run.getEmbeddedPictures()) {
  27. System.out.println(pic.getCTPicture().xmlText());
  28. System.out.println(pic.getCTPicture().getSpPr().getXfrm().getExt().getCx());
  29. System.out.println(pic.getCTPicture().getSpPr().getXfrm().getExt().getCy());
  30. // image显示大小 以厘米为单位
  31. System.out.println(pic.getCTPicture().getSpPr().getXfrm().getExt().getCx() / 360000.0);
  32. System.out.println(pic.getCTPicture().getSpPr().getXfrm().getExt().getCy() / 360000.0);
  33. int type = pic.getPictureData().getPictureType();
  34. byte[] img = pic.getPictureData().getData();
  35. BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(img));
  36. System.out.println(bufferedImage.getWidth());
  37. System.out.println(bufferedImage.getHeight());
  38. String extension = "";
  39. switch (type) {
  40. case Document.PICTURE_TYPE_EMF:
  41. extension = ".emf";
  42. break;
  43. case Document.PICTURE_TYPE_WMF:
  44. extension = ".wmf";
  45. break;
  46. case Document.PICTURE_TYPE_PICT:
  47. extension = ".pic";
  48. break;
  49. case Document.PICTURE_TYPE_PNG:
  50. extension = ".png";
  51. break;
  52. case Document.PICTURE_TYPE_DIB:
  53. extension = ".dib";
  54. break;
  55. default:
  56. extension = ".jpg";
  57. break;
  58. }
  59. FileOutputStream fos = new FileOutputStream("test"+i+extension);
  60. fos.write(img);
  61. fos.close();
  62. i++;
  63. }
  64. }
  65. }
  66. }
  67. public static void main(String[] args) {
  68. }
  69. }

将图片保存到word

  1. package docx;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  7. import org.apache.poi.util.Units;
  8. import org.apache.poi.xwpf.usermodel.BreakType;
  9. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  10. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  11. import org.apache.poi.xwpf.usermodel.XWPFRun;
  12. /**
  13. * 图片写入,图片的保存
  14. */
  15. public class ImageWriter {
  16. public static void main(String[] args) throws InvalidFormatException, FileNotFoundException, IOException {
  17. XWPFDocument doc = new XWPFDocument();
  18. XWPFParagraph paragraph = doc.createParagraph();
  19. XWPFRun run = paragraph.createRun();
  20. String[] imgFiles = { "D:\\image\\303951486484.jpg", "D:\\image\\304032384201.jpg" };
  21. for (String imgFile : imgFiles) {
  22. int format = 0;
  23. if (imgFile.endsWith(".emf"))
  24. format = XWPFDocument.PICTURE_TYPE_EMF;
  25. else if (imgFile.endsWith(".wmf"))
  26. format = XWPFDocument.PICTURE_TYPE_WMF;
  27. else if (imgFile.endsWith(".pict"))
  28. format = XWPFDocument.PICTURE_TYPE_PICT;
  29. else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
  30. format = XWPFDocument.PICTURE_TYPE_JPEG;
  31. else if (imgFile.endsWith(".png"))
  32. format = XWPFDocument.PICTURE_TYPE_PNG;
  33. else if (imgFile.endsWith(".dib"))
  34. format = XWPFDocument.PICTURE_TYPE_DIB;
  35. else if (imgFile.endsWith(".gif"))
  36. format = XWPFDocument.PICTURE_TYPE_GIF;
  37. else if (imgFile.endsWith(".tiff"))
  38. format = XWPFDocument.PICTURE_TYPE_TIFF;
  39. else if (imgFile.endsWith(".eps"))
  40. format = XWPFDocument.PICTURE_TYPE_EPS;
  41. else if (imgFile.endsWith(".bmp"))
  42. format = XWPFDocument.PICTURE_TYPE_BMP;
  43. else if (imgFile.endsWith(".wpg"))
  44. format = XWPFDocument.PICTURE_TYPE_WPG;
  45. else {
  46. System.err.println(
  47. "Unsupported picture:" + imgFile + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
  48. continue;
  49. }
  50. //文件路径
  51. run.setText(imgFile);
  52. //换行
  53. run.addBreak();
  54. //200*200
  55. run.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200));
  56. //每张图片的末尾添加一个分页符
  57. run.addBreak(BreakType.PAGE);
  58. }
  59. FileOutputStream out = new FileOutputStream("images.docx");
  60. doc.write(out);
  61. out.close();
  62. }
  63. }

读取word表格内容

  1. package docx;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.util.List;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.apache.poi.xwpf.usermodel.BodyElementType;
  7. import org.apache.poi.xwpf.usermodel.IBodyElement;
  8. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  9. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  10. import org.apache.poi.xwpf.usermodel.XWPFRun;
  11. import org.apache.poi.xwpf.usermodel.XWPFTable;
  12. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  13. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  14. /**
  15. * docx的表格内容的读取
  16. */
  17. public class TableRead {
  18. public static void main(String[] args) throws Exception {
  19. testTable();
  20. }
  21. private static void testTable() throws Exception{
  22. InputStream inputStream = new FileInputStream("simple2.docx");
  23. XWPFDocument document = new XWPFDocument(inputStream);
  24. List<XWPFParagraph> paragraphs = document.getParagraphs();
  25. List<IBodyElement> bodyElements = document.getBodyElements();
  26. for(IBodyElement bodyElement : bodyElements) {
  27. BodyElementType elementType = bodyElement.getElementType();
  28. if(elementType == BodyElementType.TABLE) {
  29. //表格
  30. System.out.println("table"+bodyElement.getPart());
  31. XWPFTable table = (XWPFTable) bodyElement;
  32. List<XWPFTableRow> rows = table.getRows();
  33. //读取每一行数据
  34. for(int i = 0; i < rows.size(); i++) {
  35. XWPFTableRow row = rows.get(i);
  36. //读取每一列数据
  37. List<XWPFTableCell> cells = row.getTableCells();
  38. for(int j = 0; j < cells.size(); j++) {
  39. XWPFTableCell cell = cells.get(j);
  40. System.out.println(cell.getText());
  41. List<XWPFParagraph> cellParagraphs = cell.getParagraphs();
  42. System.out.println(cellParagraphs.size());
  43. }
  44. }
  45. }else {
  46. //段落
  47. XWPFParagraph para = (XWPFParagraph) bodyElement;
  48. System.out.println("It is a new paragraph...The indention is "+para.getFirstLineIndent());
  49. List<XWPFRun> runs = para.getRuns();
  50. System.out.println("run");
  51. if(runs.size() <= 0) {
  52. System.out.println("empty line");
  53. }
  54. for(XWPFRun run : runs) {
  55. //如果片段没有文字,可能该片段是图片
  56. if(StringUtils.isEmpty(run.text())) {
  57. //该片段是图片时
  58. if(run.getEmbeddedPictures().size() > 0) {
  59. System.out.println("image***"+run.getEmbeddedPictures());
  60. }else {
  61. System.out.println("objects:"+run.getCTR().getObjectList());
  62. //公式
  63. if(run.getCTR().xmlText().indexOf("instrText") > 0) {
  64. System.out.println("there is an equation field");
  65. }
  66. }
  67. }else {
  68. System.out.println("==="+run.getCharacterSpacing()+run.text());
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }

在word中写入表格

  1. package docx;
  2. import java.io.FileOutputStream;
  3. import java.io.OutputStream;
  4. import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
  5. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  6. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  7. import org.apache.poi.xwpf.usermodel.XWPFRun;
  8. import org.apache.poi.xwpf.usermodel.XWPFTable;
  9. /**
  10. * 写入表格
  11. */
  12. public class TableWriter {
  13. public static void main(String[] args) throws Exception {
  14. try {
  15. createSimpleTable();
  16. }catch (Exception e) {
  17. System.out.println("Error trying to create simple table.");
  18. throw(e);
  19. }
  20. }
  21. private static void createSimpleTable() throws Exception{
  22. XWPFDocument doc = new XWPFDocument();
  23. try {
  24. XWPFTable table = doc.createTable(3,3);
  25. table.getRow(1).getCell(1).setText("表格示例");
  26. XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
  27. XWPFRun run1 = p1.createRun();
  28. run1.setBold(true);
  29. run1.setText("The quick brown fox");
  30. run1.setItalic(true);
  31. run1.setFontFamily("Courier");
  32. run1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
  33. run1.setTextPosition(100);
  34. table.getRow(2).getCell(2).setText("only text");
  35. OutputStream out = new FileOutputStream("simpleTable.docx");
  36. try {
  37. doc.write(out);
  38. }finally {
  39. out.close();
  40. }
  41. }finally {
  42. doc.close();
  43. }
  44. }
  45. }

word模板套写

  1. package docx;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  14. import org.apache.poi.util.Units;
  15. import org.apache.poi.xwpf.usermodel.BodyElementType;
  16. import org.apache.poi.xwpf.usermodel.IBodyElement;
  17. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  18. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  19. import org.apache.poi.xwpf.usermodel.XWPFRun;
  20. import org.apache.poi.xwpf.usermodel.XWPFTable;
  21. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  22. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  23. public class TemplateTest {
  24. public static void main(String[] args) throws InvalidFormatException, FileNotFoundException, Exception {
  25. // 导入模板文件
  26. XWPFDocument doc = openDocx("template.docx");
  27. // 文字类 key-value
  28. Map<String, Object> params = new HashMap<String, Object>();
  29. params.put("${name}", "Tom");
  30. params.put("${sex}", "男");
  31. // 图片类 key-url
  32. Map<String, String> picParams = new HashMap<String, String>();
  33. picParams.put("${pic}", "D:\\image\\303951486484.jpg");
  34. List<IBodyElement> bodyElements = doc.getBodyElements();
  35. for (IBodyElement bodyElement : bodyElements) {
  36. if (bodyElement.getElementType() == BodyElementType.TABLE) {
  37. replaceTable(bodyElement, params, picParams, doc);
  38. }
  39. }
  40. // 输出
  41. writeDocx(doc, new FileOutputStream("template2.docx"));
  42. }
  43. private static void writeDocx(XWPFDocument doc, OutputStream outputStream) {
  44. try {
  45. doc.write(outputStream);
  46. outputStream.flush();
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. } finally {
  50. if (outputStream != null) {
  51. try {
  52. outputStream.close();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58. }
  59. private static void replaceTable(IBodyElement bodyElement, Map<String, Object> params,
  60. Map<String, String> picParams, XWPFDocument doc) {
  61. Matcher matcher;
  62. XWPFTable table;
  63. List<XWPFTableRow> rows;
  64. List<XWPFTableCell> cells;
  65. table = (XWPFTable) bodyElement;
  66. rows = table.getRows();
  67. for (XWPFTableRow row : rows) {
  68. cells = row.getTableCells();
  69. int cellSize = cells.size();
  70. int cellCount = 0;
  71. for (cellCount = 0; cellCount < cellSize; cellCount++) {
  72. XWPFTableCell cell = cells.get(cellCount);
  73. String runText = "";
  74. List<XWPFParagraph> paragraphs = cell.getParagraphs();
  75. for (XWPFParagraph paragraph : paragraphs) {
  76. List<XWPFRun> runs = paragraph.getRuns();
  77. for (XWPFRun run : runs) {
  78. runText = run.text();
  79. matcher = matcher(runText);
  80. if (matcher.find()) {
  81. if (picParams != null) {
  82. for (String picKey : picParams.keySet()) {
  83. if (matcher.group().equals(picKey)) {
  84. run.setText("", 0);
  85. replacePic(run, picParams.get(picKey), doc);
  86. }
  87. }
  88. }
  89. if (params != null) {
  90. for (String paramKey : params.keySet()) {
  91. if (matcher.group().equals(paramKey)) {
  92. run.setText(params.get(paramKey) + "", 0);
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. private static void replacePic(XWPFRun run, String imgFile, XWPFDocument doc) {
  103. try {
  104. int format = 0;
  105. if (imgFile.endsWith(".emf"))
  106. format = XWPFDocument.PICTURE_TYPE_EMF;
  107. else if (imgFile.endsWith(".wmf"))
  108. format = XWPFDocument.PICTURE_TYPE_WMF;
  109. else if (imgFile.endsWith(".pict"))
  110. format = XWPFDocument.PICTURE_TYPE_PICT;
  111. else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
  112. format = XWPFDocument.PICTURE_TYPE_JPEG;
  113. else if (imgFile.endsWith(".png"))
  114. format = XWPFDocument.PICTURE_TYPE_PNG;
  115. else if (imgFile.endsWith(".dib"))
  116. format = XWPFDocument.PICTURE_TYPE_DIB;
  117. else if (imgFile.endsWith(".gif"))
  118. format = XWPFDocument.PICTURE_TYPE_GIF;
  119. else if (imgFile.endsWith(".tiff"))
  120. format = XWPFDocument.PICTURE_TYPE_TIFF;
  121. else if (imgFile.endsWith(".eps"))
  122. format = XWPFDocument.PICTURE_TYPE_EPS;
  123. else if (imgFile.endsWith(".bmp"))
  124. format = XWPFDocument.PICTURE_TYPE_BMP;
  125. else if (imgFile.endsWith(".wpg"))
  126. format = XWPFDocument.PICTURE_TYPE_WPG;
  127. else {
  128. System.err.println(
  129. "Unsupported picture:" + imgFile + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
  130. }
  131. // 文件路径
  132. run.setText(imgFile);
  133. // 换行
  134. run.addBreak();
  135. // 200*200
  136. run.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200));
  137. } catch (InvalidFormatException e) {
  138. // TODO Auto-generated catch block
  139. e.printStackTrace();
  140. } catch (FileNotFoundException e) {
  141. // TODO Auto-generated catch block
  142. e.printStackTrace();
  143. } catch (IOException e) {
  144. // TODO Auto-generated catch block
  145. e.printStackTrace();
  146. }
  147. }
  148. private static Matcher matcher(String str) {
  149. Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
  150. Matcher matcher = pattern.matcher(str);
  151. return matcher;
  152. }
  153. private static XWPFDocument openDocx(String fileName) throws IOException {
  154. InputStream inputStream = null;
  155. try {
  156. inputStream = new FileInputStream(fileName);
  157. XWPFDocument document = new XWPFDocument(inputStream);
  158. return document;
  159. } catch (Exception e) {
  160. e.printStackTrace();
  161. } finally {
  162. inputStream.close();
  163. }
  164. return null;
  165. }
  166. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/273100
推荐阅读
相关标签
  

闽ICP备14008679号