package com.product.file.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.aspose.cells.Cells; import com.aspose.cells.PdfSaveOptions; import com.aspose.cells.Workbook; import com.aspose.cells.Worksheet; import com.aspose.cells.WorksheetCollection; import com.aspose.slides.Presentation; import com.aspose.words.*; import com.product.core.exception.BaseException; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.file.config.FileCode; public class AsposeUtil { private static int OFFICE_XLS=1; private static int OFFICE_WORD=2; private static int OFFICE_PPT=3; private static boolean OFFICE_XLS_=false; private static boolean OFFICE_WORD_=false; private static boolean OFFICE_PPT_=false; //是否初始了license private static boolean isInitLicense=false; /** * 获取license * @return */ public static boolean getLicense(int type)throws BaseException { //已经初始过,不再初始 if(isInitLicense) { return true; } boolean result = true; InputStream is = null; try { if(type==OFFICE_XLS && !OFFICE_XLS_ ) { is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); com.aspose.cells.License excel = new com.aspose.cells.License(); excel.setLicense(is); OFFICE_XLS_=true; }else if(type==OFFICE_WORD && !OFFICE_WORD_ ){ is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); com.aspose.words.License word = new com.aspose.words.License(); word.setLicense(is); OFFICE_WORD_=true; }else if(type==OFFICE_PPT && ! OFFICE_PPT_) { is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); com.aspose.slides.License ppt = new com.aspose.slides.License(); ppt.setLicense(is); OFFICE_PPT_=true; } } catch (Exception e) { e.printStackTrace(); result=false; throw new BaseException(FileCode.DOC_CONVERT_FALL,e); }finally{ if(is!=null) { try { is.close(); }catch(IOException e) { throw new BaseException(e); } } } return result; } /** * * @param officePath * @param OutPutPath * @throws BaseException */ public static void Excel2Pdf(String officePath,String OutPutPath)throws BaseException { // 验证License if (!getLicense(1)) { return; } FileOutputStream fileOS=null; try { File file = new File(OutPutPath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } Workbook wb = new Workbook(officePath);// 原始excel路径 fileOS = new FileOutputStream(OutPutPath); //wb.save(fileOS, com.aspose.cells.SaveFormat.PDF); PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); pdfSaveOptions.setAllColumnsInOnePagePerSheet(true); wb.save(fileOS, pdfSaveOptions); } catch (Exception e) { throw new BaseException(FileCode.DOC_CONVERT_FALL,e); }finally{ if(fileOS!=null) { try { fileOS.flush(); fileOS.close(); }catch(IOException e) { throw new BaseException(e); } } } } /** * * @param officePath * @param OutPutPath * @throws BaseException */ public static void xls2xlsx(String officePath,String OutPutPath,String officeType)throws BaseException { // 验证License if (!getLicense(1)) { return; } if("xlsx".equals(officeType)) { FileUtil.copyFile(new File(officePath), OutPutPath); return ; } FileOutputStream fileOS=null; try { File file = new File(OutPutPath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } Workbook wb = new Workbook(officePath);// 原始excel路径 fileOS = new FileOutputStream(OutPutPath); wb.save(fileOS, com.aspose.cells.SaveFormat.XLSX); } catch (Exception e) { throw new BaseException(FileCode.DOC_CONVERT_FALL,e); }finally{ if(fileOS!=null) { try { fileOS.flush(); fileOS.close(); }catch(IOException e) { throw new BaseException(e); } } } } public static void Excel2Html(String officePath,String OutPutPath)throws BaseException { // 验证License if (!getLicense(1)) { return; } FileOutputStream fileOS=null; try { File file = new File(OutPutPath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } Workbook wb = new Workbook(officePath);// 原始excel路径 WorksheetCollection sheets=wb.getWorksheets(); System.out.println("sheet个数:"+sheets.getCount()); for(int i=0;i ss=new ArrayList<>(); for(Paragraph para :(Iterable) nodes) { System.out.println("==========================="); System.out.println(para.isEndOfSection()); System.out.println(para.hasChildNodes()); String text=para.getText(); if("\r".equals(text))System.out.println("\\\\r"); else if("\f".equals(text))System.out.println("\\\\f"); else if("".equals(text.trim())) System.out.println("空格"); else System.out.println(text); NodeCollection pnodes =para.getChildNodes(); if(pnodes.getCount()==0)para.appendChild(new Run(targetDoc,"a")); if(para.isEndOfSection())para.appendChild(new Run(targetDoc,"B")); System.out.println("sub nodes:"+pnodes.getCount()); for(int i=0;i var3 = srcDoc.getSections().iterator(); while (var3.hasNext()) { Section srcSection = (Section) var3.next(); Node dstNode = dstDoc.importNode(srcSection, true, 0); dstDoc.appendChild(dstNode); } } else { Node node = dstDoc.getLastSection().getBody().getLastParagraph(); if (node == null) { node = new Paragraph(srcDoc); dstDoc.getLastSection().getBody().appendChild(node); } if (node.getNodeType() != 8 & node.getNodeType() != 5) { throw new Exception("Use appendDoc(dstDoc, srcDoc, true) instead of appendDoc(dstDoc, srcDoc, false)"); } insertDocumentAfterNode(node, dstDoc, srcDoc); } File f=new File(sohuyepath); dstDoc.save(f.getParent()+"/merge/"+f.getName()); return f.getParent()+"/merge/"+f.getName(); } private static void insertDocumentAfterNode(Node insertAfterNode, Document mainDoc, Document srcDoc) throws Exception { if (insertAfterNode.getNodeType() != 8 & insertAfterNode.getNodeType() != 5) { throw new Exception("The destination node should be either a paragraph or table."); } else { CompositeNode dstStory = insertAfterNode.getParentNode(); while (null != srcDoc.getLastSection().getBody().getLastParagraph() && !srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes()) { srcDoc.getLastSection().getBody().getLastParagraph().remove(); } NodeImporter importer = new NodeImporter(srcDoc, mainDoc, 1); int sectCount = srcDoc.getSections().getCount(); for (int sectIndex = 0; sectIndex < sectCount; ++sectIndex) { Section srcSection = srcDoc.getSections().get(sectIndex); int nodeCount = srcSection.getBody().getChildNodes().getCount(); for (int nodeIndex = 0; nodeIndex < nodeCount; ++nodeIndex) { Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex); Node newNode = importer.importNode(srcNode, true); dstStory.insertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } } } /** * 把源文档内容放到最后一页的页末 * @param sohuyepath * @return * @throws Exception */ private static void appendDocToPageTail(String sohuyepath) throws Exception { Document dstDoc = new Document(sohuyepath); int destPages=dstDoc.getPageCount(); System.out.println("合并合的最后总页数:"+destPages); Node lastNode=dstDoc.getLastSection().getBody().getLastChild(); Paragraph lp=null; if(lastNode.getNodeType()==NodeType.PARAGRAPH){ lp=(Paragraph)lastNode; Node ld=lp.getFirstChild(); while(ld!=null && ld.getNodeType()!=NodeType.SHAPE && ld.getNodeType()!=NodeType.RUN && ld.getNodeType()!=NodeType.TABLE){ System.out.println("type name "+NodeType.getName(ld.getNodeType())); ld=ld.getPreviousSibling(); } } System.out.println("最后一节点:"+NodeType.getName(lastNode.getNodeType())); for(int i=0;i<40;i++) { Run t = new Run(dstDoc, "======\r"); if(lp!=null){ lp.insertBefore(t,lp.getLastChild()); System.out.println("子元素数:"+lp.getChildNodes().getCount()); } System.out.println("合并合的最后总页数:"+dstDoc.getPageCount()); if(destPages < dstDoc.getPageCount()){ System.out.println("================分页================="); } } File ff=new File(sohuyepath); dstDoc.save(ff.getParent()+"/merge/"+ff.getName(),SaveFormat.DOCX); } public static void main(String args[]) { // String file="E:/工作内容.xlsx"; // String file="E:/爆破母线.xls"; // String file="E:/采集提取日志表结构.xlsx"; String file="E:/FE信息系统国产化解决方案V1.0.docx"; // AsposeUtil.OfficeToPdf(file); // AsposeUtil.mergeWord("E:\\QQ\\住房担保置换公司.docx","E:\\QQ\\住房担保置换公司2.docx"); // try { // processDocPage(); // }catch(Exception e) { // e.printStackTrace(); // } try { // String sohuyepath="E:/QQ/住房担保置换公司.docx"; // String xuyepath="E:/QQ/FE信息系统国产化解决方案V1.0.docx"; // String npath=appendDoc(sohuyepath, xuyepath, false); String tail="E:/QQ/住房担保置换公司2.docx"; // appendDoc(npath, tail, false); appendDocToPageTail(tail); }catch(Exception e) { e.printStackTrace(); } } }