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.math.RoundingMode; import java.text.DecimalFormat; import com.aspose.cells.Cells; import com.aspose.cells.HtmlSaveOptions; 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.Document; import com.aspose.words.ImportFormatMode; import com.aspose.words.NodeCollection; import com.aspose.words.NodeType; import com.aspose.words.Paragraph; import com.aspose.words.SaveFormat; import com.aspose.words.Section; import com.aspose.words.SectionStart; import com.product.core.exception.BaseException; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.file.config.FileCode; public class AsposeUtil { //是否初始了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 { is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); com.aspose.cells.License excel = new com.aspose.cells.License(); excel.setLicense(is); is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); com.aspose.words.License word = new com.aspose.words.License(); word.setLicense(is); is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); com.aspose.slides.License ppt = new com.aspose.slides.License(); ppt.setLicense(is); isInitLicense=true; } catch (Exception e) { 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) nodes) { String text=para.getText(); if("\r".equals(text)) { System.out.println("rrrrrrrrrrrr"); }else if("\f".equals(text)) { System.out.println("ffffffffffff"); }else { System.out.println(text); } if ("\r".equals(text) //|| "\f".equals(text) ) { para.remove(); } } } public static void docProcess(Document targetDoc) { NodeCollection nodes=targetDoc.getChildNodes(); for(Paragraph para :(Iterable) nodes) { String text=para.getText(); if("\r".equals(text)) { System.out.println("rrrrrrrrrrrr"); }else if("\f".equals(text)) { System.out.println("ffffffffffff"); }else { System.out.println(text); } if ("\r".equals(text) //|| "\f".equals(text) ) { para.remove(); } } } 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"); } }