POI读取Word内容及格式,看这一篇就够了! |
您所在的位置:网站首页 › java读取word内容并进行修改 › POI读取Word内容及格式,看这一篇就够了! |
ps:获取实例代码请直接拉倒最后 效果 pom依赖 代码语言:javascript复制 org.apache.poi poi 4.1.2 cn.hutool hutool-all 5.5.7 org.apache.poi poi-ooxml 4.1.2 org.apache.poi poi-ooxml-schemas 4.1.2 org.apache.poi ooxml-schemas 1.1 org.apache.poi poi-scratchpad 4.1.2加载文档 代码语言:javascript复制 InputStream is = new FileInputStream("C:\\Users\\10386\\Desktop\\word-正确文档2.docx"); XWPFDocument doc = new XWPFDocument(is);页眉页脚 代码语言:javascript复制 XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy(); //获取页眉 String header = headerFooterPolicy.getDefaultHeader().getText(); System.out.println("***页眉 ***"+header); //获取页脚 String footer = headerFooterPolicy.getDefaultFooter().getText(); System.out.println("***页脚 ***"+header);页眉边距 代码语言:javascript复制 CTDocument1 ctdoc = doc.getDocument(); int top = ctdoc.getBody().getSectPr().getPgMar().getTop().intValue(); int bottom = ctdoc.getBody().getSectPr().getPgMar().getBottom().intValue(); int left = ctdoc.getBody().getSectPr().getPgMar().getLeft().intValue(); int right = ctdoc.getBody().getSectPr().getPgMar().getRight().intValue();获取标题 代码语言:javascript复制 List paras = doc.getParagraphs(); //将得到包含段落列表 //获取标题 List list = getParagraph(paras.get(0)); System.out.println("标题信息==="+list);获取表格 代码语言:javascript复制 int row_count =0; XWPFTable table = (XWPFTable) element; List xwpfTableRows = table.getRows(); row_count = xwpfTableRows.size(); ArrayList cell_count=new ArrayList(); int row_index = 1; for (XWPFTableRow xwpfTableRow : xwpfTableRows) { List xwpfTableCells = xwpfTableRow.getTableCells(); cell_count.add(xwpfTableCells.size()); System.out.println("第"+row_index+"行"); int cell_index =1; for (XWPFTableCell xwpfTableCell : xwpfTableCells) { //单元格是否被合并,合并了几个 CTDecimalNumber cellspan = xwpfTableCell.getCTTc().getTcPr().getGridSpan(); boolean gridspan = cellspan != null; String gridspan_num = cellspan != null?cellspan.getVal().toString():"0"; List xwpfParagraphs = xwpfTableCell.getParagraphs(); XWPFParagraph paragraph = xwpfParagraphs.get(0); System.out.println("第" +cell_index+"个单元格,合并标志:"+gridspan+",合并个数:"+gridspan_num +"文字:"+getParagraph(paragraph)); cell_index++; } row_index++; } System.out.println("表格为:row_count==="+row_count+"行"+Collections.max(cell_count)+"列");获取图片 代码语言:javascript复制List pictures = run.getEmbeddedPictures(); if (pictures.size() > 0) { XWPFPicture picture = pictures.get(0); XWPFPictureData pictureData = picture.getPictureData(); System.out.println(Base64.encode(pictureData.getData())); }缩进方式计算 代码语言:javascript复制//先判断缩进方式再进行数值计算 double ind = -1, ind_left = -1, ind_right = -1, ind_hang = -1; String ind_type = ""; if (para.getIndentationHanging() != -1) {//悬挂缩进 ind_type = "hang"; if (para.getIndentationHanging() % 567 == 0) {//悬挂单位为厘米 ind = para.getIndentationHanging() / 567.0; ind_left = (para.getIndentationLeft() - 567.0 * ind) / 210; } else {//悬挂单位为字符 ind = para.getIndentationHanging() / 240; ind_left = (para.getIndentationLeft() - para.getIndentationHanging()) / 210; } ind_right = para.getIndentationRight() / 210.0; } else {//首行缩进或者无 ind_type = "first"; if (para.getFirstLineIndent() == -1) { ind_type = "none"; ind = 0; } else { ind = para.getFirstLineIndent() % 567.0 == 0 ? para.getFirstLineIndent() / 567.0 : para.getFirstLineIndent() / 240.0; } ind_left = para.getIndentationLeft() / 210; ind_right = para.getIndentationRight() / 210.0; }段落格式 代码语言:javascript复制List paras = doc.getParagraphs(); //将得到包含段落列表 XWPFParagraph para = paras.get(1); List runsLists = para.getRuns(); List list = new ArrayList(); Map titile = new HashMap(); titile.put("Text", para.getText());//本段全部内容 titile.put("Alignment", para.getAlignment().toString()); titile.put("SpacingBetween", para.getSpacingBetween() + "");//行距 titile.put("SpacingBeforeLines", para.getSpacingBeforeLines() + "");//段前 titile.put("SpacingAfterLines", para.getSpacingAfterLines() + "");//段后 titile.put("NumLevelText", para.getNumLevelText() + "");//自动编号格式 文字属性 代码语言:javascript复制 List runsLists = para.getRuns() for (XWPFRun run : runsLists ) { List pictures = run.getEmbeddedPictures(); if (pictures.size() > 0) { XWPFPicture picture = pictures.get(0); XWPFPictureData pictureData = picture.getPictureData(); System.out.println(Base64.encode(pictureData.getData())); } Map titile_map = new HashMap(); titile_map.put("content", run.getText(0)); String Bold = Boolean.toString(run.isBold());//加粗 titile_map.put("Bold", Bold); String color = run.getColor();//字体颜色 titile_map.put("Color", color); String FontFamily = run.getFontFamily(XWPFRun.FontCharRange.hAnsi);//字体 titile_map.put("FontFamily", FontFamily); String FontName = run.getFontName();//字体 titile_map.put("FontName", FontName); String FontSize = run.getFontSize() + "";//字体大小 titile_map.put("FontSize", FontSize); String Underline = run.getUnderline().name();//字下加线 titile_map.put("Underline", Underline); String UnderlineColor = run.getUnderlineColor();//字下加线颜色 titile_map.put("UnderlineColor", UnderlineColor); String Italic = Boolean.toString(run.isItalic());//字体倾斜 titile_map.put("Italic", Italic); list.add(titile_map);示例代码: 链接:https://pan.baidu.com/s/1mjVQ3x22MZJi1OZw0H6fuQ 提取码:6lu6 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |