最近項目中有個這樣的需求:要把word 按章節(大綱)拆分多個word,而後再將這些word能合併成一個總體。看到這個須要,其實也是頭疼,100個程序員中應該100位都沒有作過相似的開發,最多的就是poi 讀取、生成word,那麼接下來,我會將最近研究的word切割、轉換、合併以筆記的形式記錄,也算作爲積累吧!java
word 分割 :程序員
word 分割,使用的 poi,這個還真有點難,若是不是同事給國幫助,分割這塊,仍是很難搞定,不過合併用的第三方插件,很快就搞定了,上代碼吧:數據庫
package com.sysware.soft603.util.backUp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; import org.apache.poi.xwpf.usermodel.BodyElementType; import org.apache.poi.xwpf.usermodel.IBodyElement; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFStyles; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import com.sysware.fai.entity.BookMarkInfo; public class POItest1 { public static void main(String[] args) throws IOException { splitWord(); } private static void splitWord() { String path = "d:\\GJB 1362A-2007 軍工產品定型程序和要求.docx"; InputStream is = null; List<BookMarkInfo> bookMarkInfos = new ArrayList<BookMarkInfo>(); try { is = new FileInputStream(path); } catch (FileNotFoundException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } XWPFDocument doc = null; try { doc = new XWPFDocument(is); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } // 獲取段落 List<XWPFParagraph> paras = doc.getParagraphs(); // 獲取bodyElements List<IBodyElement> bodyElements = doc.getBodyElements(); // 獲取doc樣式 XWPFStyles styles = doc.getStyles(); int j = 0; // /切割成的word 文件存儲位置 String patha = "d:\\poi\\"; // 根據大綱定義分割成的段落 ArrayList<Integer> al_duanLuo = new ArrayList<Integer>(); // 大綱名稱 ArrayList al2_name = new ArrayList<>(); // 大綱級別 ArrayList<Integer> al3_jiBie = new ArrayList<Integer>(); ArrayList<Integer> al5 = new ArrayList<Integer>(); // 存放生成wordId ArrayList<String> al6_wordId = new ArrayList<String>(); for (int i = 0; i < bodyElements.size(); i++) { IBodyElement bodyElement = bodyElements.get(i); try { if (j == 0) { al_duanLuo.add(i); j++; al2_name.add("首頁"); al3_jiBie.add(1); // al5.add(1); al6_wordId.add(java.util.UUID.randomUUID().toString()); } if (bodyElement.getElementType() == BodyElementType.PARAGRAPH) { XWPFParagraph para = (XWPFParagraph) bodyElement; if (styles.getStyle(para.getStyle()).getCTStyle().getName() .getVal().contains("heading")) { // 以標題建立第一個文件 al_duanLuo.add(i); j++; al2_name.add(para.getParagraphText()); // System.out.println(al2); String temps = styles.getStyle(para.getStyle()) .getCTStyle().getName().getVal() .split("heading")[1].trim(); al3_jiBie.add(Integer.parseInt(temps)); // al5.add(Integer.parseInt(para.getStyleID())); al6_wordId.add(java.util.UUID.randomUUID().toString()); // System.out.println("a2======" + al2_name); // System.out.println("a3======" + al3_jiBie); // System.out.println("al======" + al_duanLuo); // System.out.println("a5======" + al5); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 定義存放父新id ArrayList al4_parentId = new ArrayList<>(); for (int i = 0; i < al_duanLuo.size(); i++) { if (al3_jiBie.get(i) > 1) { int k; for (k = i; k >= 0; k--) { if ((int) al3_jiBie.get(k) < (int) al3_jiBie.get(i)) { break; } } al4_parentId.add(al6_wordId.get(k)); } else { al4_parentId.add(" "); } } // for (int i = 0; i < al_duanLuo.size(); i++) { // System.out.println("==================="); // System.out.println("al2=" + al2_name.get(i)); // System.out.println("al3=" + al3_jiBie.get(i)); // System.out.println("all4=" + al4_parentId.get(i)); // System.out.println("==================="); // // 根據關係建立拼接組裝xml文件 // } System.out.println("al_duanLuo" + al_duanLuo); System.out.println("al2_name=" + al2_name); System.out.println("al3_jiBie=" + al3_jiBie); // System.out.println(al6_wordId); System.out.println("al4_parentId=" + al4_parentId); XWPFDocument newDoc = doc; int max = 0; max = bodyElements.size() - 1; al_duanLuo.add(max); try { for (int k = 0; k < al_duanLuo.size() - 1; k++) { path = "d:\\GJB 1362A-2007 軍工產品定型程序和要求.docx"; is = null; try { is = new FileInputStream(path); } catch (FileNotFoundException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } doc = null; try { doc = new XWPFDocument(is); // 移除多級列表,移除前面的編號,這裏分割後是有編號的,不過這裏若是你不移除的話,直接把代碼註釋掉便可 if (k != 0) { XWPFParagraph para1 = (XWPFParagraph) doc .getBodyElements().get(al_duanLuo.get(k)); String str1 = para1.getStyleID(); doc.getStyles().getStyle(str1).getCTStyle().getPPr() .unsetNumPr(); } } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } // 移除前0--14, int temp = al_duanLuo.get(k); int tempCount = al_duanLuo.get(k + 1); for (int u = max; u > tempCount - 1; u--) { doc.removeBodyElement(u); } // 進行移除以前 for (int l = temp - 1; l >= 0; l--) { doc.removeBodyElement(l); } // XWPFParagraph tempDuan = paras.get(temp); // paras.get(7).setStyle(paras.get(1).getStyle()); // XWPFRun run = paras.get(7).insertNewRun(0); // run.setText("10086"); OutputStream out = null; out = new FileOutputStream("d:\\poi\\" + al6_wordId.get(k) + ".docx"); BookMarkInfo bookMarkInfo = new BookMarkInfo(); bookMarkInfo.setId(al6_wordId.get(k)); bookMarkInfo.setPid(al4_parentId.get(k).toString()); bookMarkInfo.setName(al2_name.get(k).toString()); bookMarkInfo.setLevel(al3_jiBie.get(k)); bookMarkInfos.add(bookMarkInfo); try { doc.write(out); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("over"); CreateXml(bookMarkInfos); //遍歷xml文件 } public static void CreateXml(List<BookMarkInfo> bookMarkInfos) { String target = "d:/MyXml.xml"; SAXReader reader = new SAXReader(); try { org.dom4j.Document document = reader.read(new File(target)); Element root = document.getRootElement(); List<Element> list = root.elements(); for (Element element : list) { root.remove(element); } //給xml加上標號, int j=0; for (int i=0;i< bookMarkInfos.size();i++) { BookMarkInfo bookMarkInfo =bookMarkInfos.get(i); //判斷是否爲第一級 if(bookMarkInfo.getLevel()==1){ //爲了是第一個不用加編號 if(i!=0){ j++ ; } } if (bookMarkInfo.getLevel() == 1) { Element element = root.addElement("item"); element.addAttribute("id", bookMarkInfo.getId()); //加上編號 if(j==0){ element.addAttribute("name", bookMarkInfo.getName()); }else{ element.addAttribute("name", j+bookMarkInfo.getName()); } element.addAttribute("filename", bookMarkInfo.getId() + ".docx"); GetXElement(element, bookMarkInfo, bookMarkInfos,j+"",0); } } saveXml(target, document); //遍歷xml } catch (org.dom4j.DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void saveXml(String target, org.dom4j.Document document) { try { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setLineSeparator("\r\n"); OutputStreamWriter outputStreamWriter = new OutputStreamWriter( new FileOutputStream(target), "UTF-8"); XMLWriter xmlWriter = new XMLWriter(outputStreamWriter, outputFormat); xmlWriter.write(document); xmlWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void GetXElement(Element parent, BookMarkInfo item, List<BookMarkInfo> bookMarkInfos,String j,int k) { k=0; for (BookMarkInfo bookMarkInfo : bookMarkInfos) { if (bookMarkInfo.getLevel() == item.getLevel() + 1 && bookMarkInfo.getPid() == item.getId()) { k++ ; Element element = parent.addElement("item"); element.addAttribute("id", bookMarkInfo.getId()); element.addAttribute("name", j+"."+k+bookMarkInfo.getName()); element.addAttribute("filename", bookMarkInfo.getId() + ".docx"); GetXElement(element, bookMarkInfo, bookMarkInfos,j+"."+k,k); } } } }
<?xml version="1.0" encoding="UTF-8"?> <item id="1" name="1" filename="a.docx" author="楊穩" publishtime="2017-02-03" keywords="關鍵詞" abstract="摘要" journal="" issn="" Implementation="2017-04-01" groupname="國標" phase="研製階段" content="內容分類"> <!-- id=編號(標準規範),name=名稱,filename=文件名稱,author=做者,publishtime="發表時間",keywords="關鍵詞",abstract="摘要",journal="所屬期刊",issn=ISSN,Implementation="實施日期",groupname="國標",phase="研製階段",content="內容分類"--> <item id="1.1" name="1.1" filename="a.docx"/> <item id="1.2" name="1.2" filename="a.docx"> <item id="1.2.1" name="1.2.1" filename="a.docx"/> <item id="1.2.2" name="1.2.2" filename="a.docx"/> </item> <item id="1.3" name="1.3" filename="a.docx"> <item id="1.3.1" name="1.3.1" filename="a.docx"/> </item> <item id="1.4" name="1.4" filename="a.docx"/> </item>
最終生成的 xml 主是要爲了後續往數據庫保存爲了:最終生成的xml是:apache
<?xml version="1.0" encoding="UTF-8"?> <item id="1" name="1" filename="a.docx" author="楊穩" publishtime="2017-02-03" keywords="關鍵詞" abstract="摘要" journal="" issn="" Implementation="2017-04-01" groupname="國標" phase="研製階段" content="內容分類"> <!-- id=編號(標準規範),name=名稱,filename=文件名稱,author=做者,publishtime="發表時間",keywords="關鍵詞",abstract="摘要",journal="所屬期刊",issn=ISSN,Implementation="實施日期",groupname="國標",phase="研製階段",content="內容分類"--> <item id="cdc88f18-a024-4e88-9ba1-3d5c5a7a0c58" name="首頁" filename="cdc88f18-a024-4e88-9ba1-3d5c5a7a0c58.docx"/> <item id="d6cd51f4-b16f-4d25-860f-d102d01f4d79" name="1範圍" filename="d6cd51f4-b16f-4d25-860f-d102d01f4d79.docx"/> <item id="250b91c9-986e-4ad8-ad21-2008331d07ec" name="2引用文件" filename="250b91c9-986e-4ad8-ad21-2008331d07ec.docx"/> <item id="7e54f054-3880-4087-bde6-ec423bc0a3ad" name="3術語和定義" filename="7e54f054-3880-4087-bde6-ec423bc0a3ad.docx"> <item id="0b9dfec5-a484-4092-a8a3-0f1c0c4caeae" name="3.1一級定委 first-class committee for finalization of military products" filename="0b9dfec5-a484-4092-a8a3-0f1c0c4caeae.docx"/> <item id="0192d8d5-fdc7-4f8d-8a6e-bb88a53f5395" name="3.2二級定委 second-class committee for finalization of military products" filename="0192d8d5-fdc7-4f8d-8a6e-bb88a53f5395.docx"/> <item id="1e172285-5b97-4da0-98be-60a90bd245b8" name="3.3軍工產品 military products" filename="1e172285-5b97-4da0-98be-60a90bd245b8.docx"/> <item id="a255bca2-9380-485c-8c43-43bfb6c19a62" name="3.4軍工產品定型 finalization of military products" filename="a255bca2-9380-485c-8c43-43bfb6c19a62.docx"/> <item id="d1df3277-4895-4282-b1b1-52cf9fe48a3c" name="3.5設計定型 design finalization of military products" filename="d1df3277-4895-4282-b1b1-52cf9fe48a3c.docx"/> <item id="70709984-ba90-40cc-b4ad-e29a025cb9ab" name="3.6生產定型 manufacture finalization of military products" filename="70709984-ba90-40cc-b4ad-e29a025cb9ab.docx"/> <item id="f27b646b-bdf6-4fa9-aa78-c9dab72450ce" name="3.7一級軍工產品 first-class military products" filename="f27b646b-bdf6-4fa9-aa78-c9dab72450ce.docx"/> <item id="d80e9425-6502-46db-9251-43a06fa8a164" name="3.8二級軍工產品 second-class military products" filename="d80e9425-6502-46db-9251-43a06fa8a164.docx"/> <item id="6d217bdf-a7c6-4751-8a64-0a16026d6e52" name="3.9軍工產品鑑定 assessment of military products" filename="6d217bdf-a7c6-4751-8a64-0a16026d6e52.docx"/> </item> <item id="bd43a101-a948-4efc-ab33-cdb1bf324a6b" name="4總則" filename="bd43a101-a948-4efc-ab33-cdb1bf324a6b.docx"> <item id="43463950-f7f8-459c-9a62-65ef8021027a" name="4.1定型工做的依據" filename="43463950-f7f8-459c-9a62-65ef8021027a.docx"/> <item id="fd4614c6-c00e-4244-ae25-0d9fdb3c5e51" name="4.2定型分類" filename="fd4614c6-c00e-4244-ae25-0d9fdb3c5e51.docx"/> <item id="2ccfe33a-c864-41ac-89af-91a05075eda1" name="4.3定型原則" filename="2ccfe33a-c864-41ac-89af-91a05075eda1.docx"/> <item id="b58bb7c0-cebf-45f6-8c86-30658ec89ca4" name="4.4定型分級" filename="b58bb7c0-cebf-45f6-8c86-30658ec89ca4.docx"/> </item> <item id="afc9af0e-9074-4dc6-be3b-6632f20135b8" name="5設計定型" filename="afc9af0e-9074-4dc6-be3b-6632f20135b8.docx"> <item id="0ed6ad9a-34f1-427a-bf7e-70e1ec8717e0" name="5.1設計定型程序" filename="0ed6ad9a-34f1-427a-bf7e-70e1ec8717e0.docx"/> <item id="8634212f-0ec7-45b8-9596-69e7e6b3db78" name="5.2申請設計定型試驗的條件" filename="8634212f-0ec7-45b8-9596-69e7e6b3db78.docx"/> <item id="f1ab75a0-def4-4058-ab36-df14ee4081d6" name="5.3申請設計定型試驗" filename="f1ab75a0-def4-4058-ab36-df14ee4081d6.docx"/> <item id="d205e3ac-aa79-414f-8817-0bf693cb950c" name="5.4制定設計定型試驗大綱" filename="d205e3ac-aa79-414f-8817-0bf693cb950c.docx"> <item id="7659923a-d274-4ae2-b882-dbce090457cc" name="5.4.1試驗大綱的制定" filename="7659923a-d274-4ae2-b882-dbce090457cc.docx"/> <item id="7d3ca588-771d-4b93-b3d9-fbaa7b60066a" name="5.4.2試驗大綱內容和要求" filename="7d3ca588-771d-4b93-b3d9-fbaa7b60066a.docx"/> <item id="3fb7579f-23b0-47e5-95fb-02115726d4cb" name="5.4.3試驗大綱編制說明" filename="3fb7579f-23b0-47e5-95fb-02115726d4cb.docx"/> <item id="a864a195-3fcc-40c8-8854-8521603144a3" name="5.4.4試驗大綱變動" filename="a864a195-3fcc-40c8-8854-8521603144a3.docx"/> </item> <item id="67b5e4c2-16ac-4023-a215-d650f86d2d06" name="5.5組織設計定型試驗" filename="67b5e4c2-16ac-4023-a215-d650f86d2d06.docx"> <item id="fb17fdd6-def8-4219-b780-bdcb8fcf34f5" name="5.5.1試驗要求" filename="fb17fdd6-def8-4219-b780-bdcb8fcf34f5.docx"/> <item id="fff01927-27c5-4e27-bfd9-8356846c64a9" name="5.5.2試驗組織實施" filename="fff01927-27c5-4e27-bfd9-8356846c64a9.docx"/> <item id="e02e40c4-c1a9-42da-bf91-923e46f49ca3" name="5.5.3試驗順序" filename="e02e40c4-c1a9-42da-bf91-923e46f49ca3.docx"/> <item id="8428ba02-bda0-43f3-898a-cb61a0bffebb" name="5.5.4試驗中斷處理" filename="8428ba02-bda0-43f3-898a-cb61a0bffebb.docx"/> <item id="0f8ee823-1b3b-4357-9821-5b6c23bb1c9e" name="5.5.5試驗恢復處理" filename="0f8ee823-1b3b-4357-9821-5b6c23bb1c9e.docx"/> <item id="008c1546-45d5-4bfd-96c5-d4150054d547" name="5.5.6承研承製單位的責任" filename="008c1546-45d5-4bfd-96c5-d4150054d547.docx"/> <item id="f19db0de-1e2f-47dd-b82f-173ae5a7acab" name="5.5.7試驗記錄" filename="f19db0de-1e2f-47dd-b82f-173ae5a7acab.docx"/> <item id="2c6df7ba-53ec-45c5-95f7-4be37e5b7406" name="5.5.8試驗報告" filename="2c6df7ba-53ec-45c5-95f7-4be37e5b7406.docx"/> </item> <item id="8262e0da-b543-45da-b289-380cf43fab59" name="5.6申請設計定型" filename="8262e0da-b543-45da-b289-380cf43fab59.docx"> <item id="ded722e3-4c04-4fe3-a8e4-bf30b73bcda2" name="5.6.1提出申請報告" filename="ded722e3-4c04-4fe3-a8e4-bf30b73bcda2.docx"/> <item id="45d1a9ed-01ed-46c5-a81a-1a0822627833" name="5.6.2申請報告內容" filename="45d1a9ed-01ed-46c5-a81a-1a0822627833.docx"/> <item id="3038774c-ddd7-440a-b172-ab758192dd8a" name="5.6.3申請報告附件" filename="3038774c-ddd7-440a-b172-ab758192dd8a.docx"/> <item id="ec05b70d-07c6-4f21-b5b0-f61fa08db313" name="5.6.4產品模型" filename="ec05b70d-07c6-4f21-b5b0-f61fa08db313.docx"/> </item> <item id="a9c7cf2a-48be-46c5-aef4-8c93e138db00" name="5.7設計定型審查" filename="a9c7cf2a-48be-46c5-aef4-8c93e138db00.docx"> <item id="ea6c4556-a3fe-4177-a24b-d05ecc8b9054" name="5.7.1設計定型審查組和檢查組" filename="ea6c4556-a3fe-4177-a24b-d05ecc8b9054.docx"/> <item id="b448feb3-4731-4344-bf27-a29309b4dded" name="5.7.2設計定型標準和要求" filename="b448feb3-4731-4344-bf27-a29309b4dded.docx"/> <item id="72e2b383-83ad-4699-ad07-23b6c29da944" name="5.7.3審查意見書" filename="72e2b383-83ad-4699-ad07-23b6c29da944.docx"/> </item> <item id="8f480f8a-cee6-4bb0-8a82-23c01b368f7c" name="5.8審批設計定型" filename="8f480f8a-cee6-4bb0-8a82-23c01b368f7c.docx"> <item id="b5c7d912-9f6f-4660-beb3-57e19f5d4e95" name="5.8.1審批一級軍工產品設計定型" filename="b5c7d912-9f6f-4660-beb3-57e19f5d4e95.docx"> <item id="89fc14e1-09af-4537-915a-3fca40f7aa1d" name="5.8.1.1二級定委根據產品設計定型審查意見,審議一級軍工產品設計定型有關事宜,符合設計定型標準和要求的,向一級定委呈報批准設計定型的請示;不符合設計定型標準和要求的,提出處理意見,連同原提交的軍工產品設計定型申請文件一併退回申請單位。" filename="89fc14e1-09af-4537-915a-3fca40f7aa1d.docx"/> <item id="b47935da-32fd-4ff7-b1a2-8ffa74d69dba" name="5.8.1.2一級定委專家諮詢委員會對二級定委報送的軍工產品設計定型請示進行審覈,審覈後向一級定委提出定型諮詢意見。專家諮詢一委員會可參加二級定委組織的試驗試用大綱評審、試驗試用、定型審查等活動。" filename="b47935da-32fd-4ff7-b1a2-8ffa74d69dba.docx"/> <item id="1d7edc11-e6f2-4513-8268-ccf2de55e1ba" name="5.8.1.3一級定委對符合規定標準和要求的產品,按照規定的權限批准設計定型或報國務院、中央軍委審批,下發批覆;對不符合規定標準和要求的產品,提出處理意見,連同原報送的有關文件一併退回報送的二級定委。" filename="1d7edc11-e6f2-4513-8268-ccf2de55e1ba.docx"/> <item id="dd662740-de4a-4dee-9b05-afe92b3a020a" name="5.8.1.4產品批准設計定型後,由一級定委頒發產品設計定型證書,由二級定委對有關設計定型文件加蓋設計定型專用章。" filename="dd662740-de4a-4dee-9b05-afe92b3a020a.docx"/> </item> <item id="e3da11b1-6637-4e06-adfd-191614132859" name="5.8.2審批二級軍工產品設計定型" filename="e3da11b1-6637-4e06-adfd-191614132859.docx"/> </item> </item> <item id="248e1e89-f829-4e89-b4d7-9420dacc13b8" name="6生產定型" filename="248e1e89-f829-4e89-b4d7-9420dacc13b8.docx"> <item id="90f82375-bf17-4d44-86b5-581bcce5ec2d" name="6.1生產定型條件和時間" filename="90f82375-bf17-4d44-86b5-581bcce5ec2d.docx"/> <item id="7a197dae-20e4-4ba7-abce-86d6ce6ced72" name="6.2生產定型程序" filename="7a197dae-20e4-4ba7-abce-86d6ce6ced72.docx"/> <item id="21da9766-d333-41cf-abc9-933006b59799" name="6.3部隊試用產品與生產定型試驗產品" filename="21da9766-d333-41cf-abc9-933006b59799.docx"> <item id="2c96287b-3f41-4fb5-9ee5-71a4b3a763ee" name="6.3.1部隊試用產品" filename="2c96287b-3f41-4fb5-9ee5-71a4b3a763ee.docx"/> <item id="f96c66e4-6279-4bd5-a52a-2948e7ec580e" name="6.3.2生產定型試驗產品" filename="f96c66e4-6279-4bd5-a52a-2948e7ec580e.docx"/> </item> <item id="5f252b0e-7785-4d08-9d74-a85081c9dc5f" name="6.4組織工藝和生產條件考覈" filename="5f252b0e-7785-4d08-9d74-a85081c9dc5f.docx"/> <item id="6ff6d59f-1711-42a1-beb9-cf8c96208049" name="6.5申請部隊試用" filename="6ff6d59f-1711-42a1-beb9-cf8c96208049.docx"/> <item id="5fb9ca65-9ed9-426c-adb0-82c3c8a42d9f" name="6.6制定部隊試用大綱" filename="5fb9ca65-9ed9-426c-adb0-82c3c8a42d9f.docx"/> <item id="7017ddc5-456f-470c-a73b-284704415106" name="6.7組織部隊試用" filename="7017ddc5-456f-470c-a73b-284704415106.docx"> <item id="d2b30828-a09a-4534-bdcd-4eda8aba8e90" name="6.7.1部隊試用的實施" filename="d2b30828-a09a-4534-bdcd-4eda8aba8e90.docx"/> <item id="2d83ebef-af67-4ff7-8b2c-d61bcface34e" name="6.7.2部隊試用報告" filename="2d83ebef-af67-4ff7-8b2c-d61bcface34e.docx"/> </item> <item id="eedd399f-0d47-42c0-b954-20b532a05340" name="6.8申請生產定型試驗" filename="eedd399f-0d47-42c0-b954-20b532a05340.docx"/> <item id="a2f3b8ab-5fdf-44cd-8694-a020dbed6fe2" name="6.9制定生產定型試驗大綱" filename="a2f3b8ab-5fdf-44cd-8694-a020dbed6fe2.docx"/> <item id="c35fa5ab-ba11-451a-bbf5-043add4496c1" name="6.10組織生產定型試驗" filename="c35fa5ab-ba11-451a-bbf5-043add4496c1.docx"/> <item id="f5ad80a9-fdc2-4bab-a460-2da497dfca68" name="6.11申請生產定型" filename="f5ad80a9-fdc2-4bab-a460-2da497dfca68.docx"> <item id="25783469-78ff-4400-b39c-795e72ed33b0" name="6.11.1提出申請報告" filename="25783469-78ff-4400-b39c-795e72ed33b0.docx"/> <item id="0344cbf0-1595-4124-b030-20a9ace0941c" name="6.11.2申請報告內容" filename="0344cbf0-1595-4124-b030-20a9ace0941c.docx"/> <item id="6862ca2d-b646-4eba-af5c-150de9055301" name="6.11.3申請報告附件" filename="6862ca2d-b646-4eba-af5c-150de9055301.docx"/> </item> <item id="a12aaeb2-2615-437a-aeb4-2ce7fc172657" name="6.12組織生產定型審查" filename="a12aaeb2-2615-437a-aeb4-2ce7fc172657.docx"> <item id="e5987e7d-5a37-4906-961e-6ee839dfc743" name="6.12.1生產定型審查組 " filename="e5987e7d-5a37-4906-961e-6ee839dfc743.docx"/> <item id="b9c74941-650a-4f8f-a92c-8578e60c072b" name="6.12.2生產定型標準和要求" filename="b9c74941-650a-4f8f-a92c-8578e60c072b.docx"/> <item id="fa286bb9-27e0-4d50-ac57-78773dfbded3" name="6.12.3審查意見書" filename="fa286bb9-27e0-4d50-ac57-78773dfbded3.docx"/> </item> <item id="0346afa0-ca37-46e8-89c5-eaaec86a8898" name="6.13審批生產定型" filename="0346afa0-ca37-46e8-89c5-eaaec86a8898.docx"/> </item> <item id="6472b4ba-fbf8-4b8b-bf3a-7d0412edade2" name="7定型文件" filename="6472b4ba-fbf8-4b8b-bf3a-7d0412edade2.docx"> <item id="f79e1d11-fb56-45cb-8529-fba7eac33185" name="7.1定型文件種類" filename="f79e1d11-fb56-45cb-8529-fba7eac33185.docx"/> <item id="07fce1f4-d186-42f1-ab89-1f9db5d65e0b" name="7.2定型文件清單" filename="07fce1f4-d186-42f1-ab89-1f9db5d65e0b.docx"> <item id="cece5585-cf2f-4891-a4df-e1e5c2e57508" name="7.2.1設計定型文件" filename="cece5585-cf2f-4891-a4df-e1e5c2e57508.docx"/> <item id="45350173-17a8-4357-bb2c-540e5184c6b5" name="7.2.2生產定型文件" filename="45350173-17a8-4357-bb2c-540e5184c6b5.docx"/> </item> <item id="9d8ebba1-513a-4eea-ade1-8629e3d5ea60" name="7.3定型文件製做要求" filename="9d8ebba1-513a-4eea-ade1-8629e3d5ea60.docx"> <item id="2ea2bd5a-fb02-4ffa-b219-4261f8232b42" name="7.3.1技術文件格式" filename="2ea2bd5a-fb02-4ffa-b219-4261f8232b42.docx"/> <item id="a6d486b7-c3ae-4109-adf7-54270879bdb8" name="7.3.2產品圖樣繪製" filename="a6d486b7-c3ae-4109-adf7-54270879bdb8.docx"/> <item id="1a9b9795-7469-4fd6-92be-ff43c9cf454e" name="7.3.3產品照片和錄像片" filename="1a9b9795-7469-4fd6-92be-ff43c9cf454e.docx"/> <item id="ba1e384c-e16e-4b3a-a125-0575f38d3a1e" name="7.3.4文件整理" filename="ba1e384c-e16e-4b3a-a125-0575f38d3a1e.docx"/> </item> <item id="50c13504-32c3-4a6d-ae96-f9a35cfd6c9e" name="7.4定型文件彙總上報" filename="50c13504-32c3-4a6d-ae96-f9a35cfd6c9e.docx"/> <item id="3cca6d6b-13a2-495b-ba9d-bd21b8584917" name="7.5定型文件蓋章" filename="3cca6d6b-13a2-495b-ba9d-bd21b8584917.docx"/> <item id="a7381eb0-fc6a-48f0-8fe1-236d63401a41" name="7.6定型文件的使用" filename="a7381eb0-fc6a-48f0-8fe1-236d63401a41.docx"/> <item id="a77fbb37-dc8d-489e-a29f-0fe414ee4c43" name="7.7定型文件的修改" filename="a77fbb37-dc8d-489e-a29f-0fe414ee4c43.docx"/> </item> </item>
不過poi 分割.docx沒問題,可是分割.doc 文件就會有問題dom
因此中間若是是.doc 文件的話,因此中間還得進行轉換,這裏我使用的技術是aspose for java,這個特別簡單隻要把jar 包引入便可兩步,不過這個玩藝是收費的,因此有點扯,不過,最後在csdn花了10分,下載了一個license.xml(破解):spa
代碼以下:插件
package com.sysware.soft603.util.backUp;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import com.aspose.words.Document;
import com.aspose.words.License;
public class WordToPDF {
public static boolean getLicense() {
boolean result = false;
try {
// InputStream is =
// Test.class.getClassLoader().getResourceAsStream("\\license.xml");
InputStream is = Test.class.getClassLoader().getResourceAsStream(
"com\\sysware\\soft603\\util\\backUp\\license.xml");
// InputStream is =
// Test.class.getClassLoader().getResourceAsStream("testpackage/license2.xml");
// InputStream is =
// Test.class.getClassLoader().getResourceAsStream("subpackage/license3.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
// 驗證License
if (!getLicense()) {
return;
}
String path = "d:\\GJB 1362A-2007 軍工產品定型程序和要求.doc";
InputStream is = null;
try {
is = new FileInputStream(path);
Document doc = new Document(is);
doc.save("d:\\1028.pdf");//轉pdf、docx均可以
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如下是合併的代碼:使用的是docx4j設計
package com.sysware.soft603.util.backUp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.commons.io.IOUtils; import org.apache.poi.xwpf.usermodel.BodyElementType; import org.apache.poi.xwpf.usermodel.IBodyElement; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFStyles; import org.docx4j.jaxb.Context; import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.PartName; import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart; import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; import org.docx4j.relationships.Relationship; import org.docx4j.wml.CTAltChunk; public class MargeDoc { public void mergeDocx(List<String> list, String path) { List<InputStream> inList = new ArrayList<InputStream>(); for (int i = 0; i < list.size(); i++) try { inList.add(new FileInputStream(list.get(i))); } catch (FileNotFoundException e) { e.printStackTrace(); } try { InputStream inputStream = mergeDocx(inList); saveTemplate(inputStream, path); } catch (Docx4JException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public InputStream mergeDocx(final List<InputStream> streams) throws Docx4JException, IOException { WordprocessingMLPackage target = null; final File generated = File.createTempFile("generated", ".docx"); int chunkId = 0; Iterator<InputStream> it = streams.iterator(); while (it.hasNext()) { InputStream is = it.next(); if (is != null) { if (target == null) { // Copy first (master) document OutputStream os = new FileOutputStream(generated); os.write(IOUtils.toByteArray(is)); os.close(); target = WordprocessingMLPackage.load(generated); } else { // Attach the others (Alternative input parts) insertDocx(target.getMainDocumentPart(), IOUtils.toByteArray(is), chunkId++); } } } if (target != null) { target.save(generated); return new FileInputStream(generated); } else { return null; } } // 鎻掑靉鏂囨。 private void insertDocx(MainDocumentPart main, byte[] bytes, int chunkId) { try { AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart( new PartName("/part" + chunkId + ".docx")); // afiPart.setContentType(new ContentType(CONTENT_TYPE)); afiPart.setBinaryData(bytes); Relationship altChunkRel = main.addTargetPart(afiPart); CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk(); chunk.setId(altChunkRel.getId()); main.addObject(chunk); } catch (Exception e) { e.printStackTrace(); } } public void saveTemplate(InputStream fis, String toDocPath) { FileOutputStream fos; int bytesum = 0; int byteread = 0; try { fos = new FileOutputStream(toDocPath); byte[] buffer = new byte[1444]; while ((byteread = fis.read(buffer)) != -1) { bytesum += byteread; // 瀛楄妭鏁� 鏂囦歡澶у皬 fos.write(buffer, 0, byteread); } fis.close(); fos.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws Docx4JException, IOException { MargeDoc wordUtil = new MargeDoc(); String template = "D:/poinew"; List<String> list = new ArrayList<String>(); list.add(template + "/0.docx"); list.add(template + "/1.docx"); list.add(template + "/2.docx"); list.add(template + "/3.docx"); list.add(template + "/3.1.docx"); list.add(template + "/3.2.docx"); list.add(template + "/3.3.docx"); list.add(template + "/3.4.docx"); list.add(template + "/3.5.docx"); list.add(template + "/3.6.docx"); list.add(template + "/3.7.docx"); list.add(template + "/3.8.docx"); list.add(template + "/3.9.docx"); list.add(template + "/4.docx"); list.add(template + "/4.1.docx"); list.add(template + "/4.2.docx"); list.add(template + "/4.3.docx"); list.add(template + "/4.4.docx"); list.add(template + "/5.docx"); list.add(template + "/5.1.docx"); list.add(template + "/5.2.docx"); list.add(template + "/5.3.docx"); list.add(template + "/5.4.docx"); list.add(template + "/5.4.1.docx"); list.add(template + "/5.4.2.docx"); list.add(template + "/5.4.3.docx"); list.add(template + "/5.4.4.docx"); list.add(template + "/5.5.docx"); list.add(template + "/5.5.1.docx"); list.add(template + "/5.5.2.docx"); list.add(template + "/5.5.3.docx"); list.add(template + "/5.5.4.docx"); list.add(template + "/5.5.5.docx"); list.add(template + "/5.5.6.docx"); list.add(template + "/5.5.7.docx"); list.add(template + "/5.5.8.docx"); list.add(template + "/5.6.docx"); list.add(template + "/5.6.1.docx"); list.add(template + "/5.6.2.docx"); list.add(template + "/5.6.3.docx"); list.add(template + "/5.6.4.docx"); list.add(template + "/5.7.docx"); list.add(template + "/5.7.1.docx"); list.add(template + "/5.7.2.docx"); list.add(template + "/5.7.3.docx"); list.add(template + "/5.8.docx"); list.add(template + "/5.8.1.docx"); list.add(template + "/5.8.1.1.docx"); list.add(template + "/5.8.1.2.docx"); list.add(template + "/5.8.1.3.docx"); list.add(template + "/5.8.1.4.docx"); list.add(template + "/5.8.2.docx"); list.add(template + "/6.docx"); list.add(template + "/6.1.docx"); list.add(template + "/6.2.docx"); list.add(template + "/6.3.docx"); list.add(template + "/6.3.1.docx"); list.add(template + "/6.3.2.docx"); list.add(template + "/6.4.docx"); list.add(template + "/6.5.docx"); list.add(template + "/6.6.docx"); list.add(template + "/6.7.docx"); list.add(template + "/6.7.1.docx"); list.add(template + "/6.7.2.docx"); list.add(template + "/6.8.docx"); list.add(template + "/6.9.docx"); list.add(template + "/6.10.docx"); list.add(template + "/6.11.docx"); list.add(template + "/6.11.1.docx"); list.add(template + "/6.11.2.docx"); list.add(template + "/6.11.3.docx"); list.add(template + "/6.12.docx"); list.add(template + "/6.12.1.docx"); list.add(template + "/6.12.2.docx"); list.add(template + "/6.12.3.docx"); list.add(template + "/6.13.docx"); list.add(template + "/7.docx"); list.add(template + "/7.1.docx"); list.add(template + "/7.2.docx"); list.add(template + "/7.2.1.docx"); list.add(template + "/7.2.2.docx"); list.add(template + "/7.3.docx"); list.add(template + "/7.3.1.docx"); list.add(template + "/7.3.2.docx"); list.add(template + "/7.3.3.docx"); list.add(template + "/7.3.4.docx"); list.add(template + "/7.4.docx"); list.add(template + "/7.5.docx"); list.add(template + "/7.6.docx"); list.add(template + "/7.7.docx"); wordUtil.mergeDocx(list, template + "/out.docx"); //splitWord(); } private static void splitWord() { String path = "d:\\poinew\\out.docx"; InputStream is = null; try { is = new FileInputStream(path); } catch (FileNotFoundException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } XWPFDocument doc = null; try { doc = new XWPFDocument(is); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } // 獲取bodyElements List<IBodyElement> bodyElements = doc.getBodyElements(); // 獲取doc樣式 XWPFStyles styles = doc.getStyles(); System.out.println(doc.getParagraphs().size()); for (int i = 0; i < bodyElements.size(); i++) { IBodyElement bodyElement = bodyElements.get(i); try { if (bodyElement.getElementType() == BodyElementType.PARAGRAPH) { XWPFParagraph para = (XWPFParagraph) bodyElement; System.out.println(styles.getStyle(para.getStyle()).getCTStyle().getName().getVal()); System.out.println(para.getText()); if (styles.getStyle(para.getStyle()).getCTStyle().getName().getVal().contains("heading")) { XWPFParagraph para1 = (XWPFParagraph) doc.getBodyElements().get(i); String str1 = para1.getStyleID(); doc.getStyles().getStyle(str1).getCTStyle().getPPr().unsetNumPr(); System.out.println("test"); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } XWPFDocument newDoc = doc; int max = 0; path = "d:\\poinew\\out.docx"; OutputStream out = null; try { out = new FileOutputStream("d:\\poinew\\newOut.docx"); try { doc.write(out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("over"); } }