src/main/java/com/product/file/service/MergeDoc.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/product/file/service/OnlineDocumentEditService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/product/file/util/AsposeUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/product/file/util/FileUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/product/file/service/MergeDoc.java
@@ -1,16 +1,10 @@ package com.product.file.service; import cn.hutool.core.io.FileUtil; import com.product.file.util.AsposeUtil; import com.product.file.util.FileUtils; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.apache.xmlbeans.XmlOptions; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody; import java.io.*; import java.util.ArrayList; import java.util.List; /** @@ -24,16 +18,19 @@ * @param srcDocxs 需要合并的目标docx文件 * @param response 合并后的docx输出文件 */ public static File mergeDoc(List<File> srcDocxs, String tempFilePath) { public static File mergeDoc(List<File> srcDocxs, String tempFilePath) throws Exception { File file = null; if (srcDocxs.size() > 1) { File file = srcDocxs.get(0); file = srcDocxs.get(0); for (int i = 1; i < srcDocxs.size(); i++) { file = AsposeUtil.mergeWord(file, srcDocxs.get(i)); file = com.product.file.util.FileUtil.mergeFile(file, srcDocxs.get(i)); } FileUtil.copyFile(file, new File(tempFilePath)); File file1 = new File(tempFilePath); FileUtil.copyFile(file, file1); FileUtil.del(file); return file1; } return null; return file; } /** src/main/java/com/product/file/service/OnlineDocumentEditService.java
@@ -75,7 +75,15 @@ } List<File> fileList = new ArrayList<>(3); if (!StringUtils.isEmpty(document_template)) { fileList.add(fileManagerService.getFile(document_template)); File file1 = fileManagerService.getFile(document_template); //判断file1的文件类型 if (file1.getName().substring(file1.getName().lastIndexOf(".") + 1).equals("doc")) { //转换为docx File docx = com.product.file.util.FileUtil.toDocx(file1); FileUtil.del(file1); file1 = docx; } fileList.add(file1); fileList.add(file); } else { fileList.add(file); @@ -104,6 +112,7 @@ } catch (BaseException e) { throw e; } catch (Exception e) { e.printStackTrace(); throw new BaseException(FileCode.NEST_RED_DOCUMENT_FAIL); } } src/main/java/com/product/file/util/AsposeUtil.java
@@ -29,8 +29,10 @@ private static boolean OFFICE_PPT_=false; //是否初始了license private static boolean isInitLicense=false; /** * 获取license * * @return */ public static boolean getLicense(int type)throws BaseException { @@ -47,6 +49,8 @@ excel.setLicense(is); OFFICE_XLS_=true; }else if(type==OFFICE_WORD && !OFFICE_WORD_ ){ //判断根路径是否为web模块 is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); com.aspose.words.License word = new com.aspose.words.License(); word.setLicense(is); @@ -72,8 +76,8 @@ } return result; } /** * * @param officePath * @param OutPutPath * @throws BaseException @@ -108,8 +112,8 @@ } } } /** * * @param officePath * @param OutPutPath * @throws BaseException @@ -145,6 +149,7 @@ } } } public static void Excel2Html(String officePath,String OutPutPath)throws BaseException { // 验证License if (!getLicense(1)) { @@ -179,28 +184,27 @@ } } } /** * 设置单元格的自动适宽、高 * * @param sheet * @throws Exception */ public static void setAutoWithHeight(Worksheet sheet)throws Exception { public static void setAutoWithHeight(Worksheet sheet) throws Exception { Cells cells =sheet.getCells(); int columnCount = cells.getMaxColumn(); //获取表页的最大列数 int rowCount = cells.getMaxRow(); //获取表页的最大行数 if(columnCount<0 ||rowCount<0 )return ; for (int col = 0; col < columnCount; col++) { for (int col = 0; col < columnCount; col++) { sheet.autoFitColumn(col, 0, rowCount); } for (int row = 0; row < rowCount; row++) { for (int row = 0; row < rowCount; row++) { sheet.autoFitRow(row, 0, columnCount); } } /** * * @param officePath * @param OutPutPath * @throws BaseException @@ -234,8 +238,8 @@ } } } /** * * @param officePath * @param OutPutPath * @throws BaseException @@ -268,8 +272,10 @@ } } } /** * 文档转换 * * @param officePath * @param outFile * @return @@ -300,8 +306,10 @@ } return outFile; } /** * 文档转换 * * @param officePath * @return */ @@ -339,8 +347,10 @@ } return OutPutPath+officeName; } /** * word文档的合并 * * @param targetWordFile * @param sourceWordFile * @return @@ -351,8 +361,10 @@ File source=new File(sourceWordFile); return mergeWord(target,source); } /** * word文档的合并 * * @param * @param * @return @@ -381,7 +393,7 @@ sin=new FileInputStream(source); Document targetDoc = new Document(tin); Document sourceDoc = new Document(sin); // docBlankProcess(targetDoc); docBlankProcess(targetDoc); targetDoc.appendDocument(sourceDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); // NodeCollection t =sourceDoc.getChildNodes(); // for(int c=0;c<t.getCount();c++) { @@ -401,8 +413,7 @@ }catch(Exception e) { e.printStackTrace(); throw new BaseException(FileCode.DOC_MERGE_FALL,e); } finally{ } finally { try { if(tin!=null) { tin.close(); @@ -463,7 +474,11 @@ } private static String appendDoc(String sohuyepath, String xuyepath, boolean includeSection) throws Exception { public static File appendDoc(File source, File target) throws Exception { return new File(appendDoc(source.getPath(), target.getPath(), false)); } public static String appendDoc(String sohuyepath, String xuyepath, boolean includeSection) throws Exception { // 验证License if (!getLicense(OFFICE_WORD)) { return null ; @@ -528,6 +543,7 @@ /** * 把源文档内容放到最后一页的页末 * * @param sohuyepath * @return * @throws Exception src/main/java/com/product/file/util/FileUtil.java
@@ -33,6 +33,43 @@ private FileUtil() { } public static String checkDocType(File file) { try (FileInputStream fis = new FileInputStream(file)) { byte[] bytes = new byte[8]; fis.read(bytes, 0, 8); String hex = bytesToHex(bytes); if (hex.contains("504B0304") && (file.getName().endsWith(".doc") || file.getName().endsWith(".docx"))) { return "docx"; } else if (hex.contains("D0CF11E0") && (file.getName().endsWith(".doc") || file.getName().endsWith(".docx"))) { //因为doc文件的头部也是D0CF11E0,所以需要判断文件后缀 return "doc"; } //增加xls 和 xlsx的判断 else if (hex.contains("504B0304") && (file.getName().endsWith(".xls") || file.getName().endsWith(".xlsx"))) { return "xlsx"; } else if (hex.contains("D0CF11E0") && (file.getName().endsWith(".xls") || file.getName().endsWith(".xlsx"))) { //因为xls文件的头部也是D0CF11E0,所以需要判断文件后缀 return "xls"; } else { return "unknown"; } } catch ( IOException e) { e.printStackTrace(); return "unknown"; } } private static String bytesToHex(byte[] bytes) { StringBuilder hex = new StringBuilder(); for (byte b : bytes) { hex.append(String.format("%02X", b)); } return hex.toString(); } /** * 合并word文档 * @@ -41,16 +78,13 @@ * @return */ public static File mergeFile(File source, File target) throws Exception { //判断文件是否为 docx或doc格式 if (!source.getName().endsWith(".docx") && !source.getName().endsWith(".doc")) { throw new BaseException(FileCode.ADD_FILE_NOT_ALLOWED); } //如果是doc格式则转换为docx格式 if (source.getName().endsWith(".doc")) { if ("doc".equals(FileUtil.checkDocType(source))) { source = toDocx(source); } if (target.getName().endsWith(".doc")) { boolean delTarget = false; if ("doc".equals(FileUtil.checkDocType(target))) { target = toDocx(target); delTarget = true; } XWPFTemplate template = XWPFTemplate.compile(source); NiceXWPFDocument xwpfDocument = template.getXWPFDocument(); @@ -65,6 +99,9 @@ } catch (IOException e) { throw e; } finally { if (delTarget) { target.delete(); } newDoc.close(); sub.close(); } @@ -83,8 +120,9 @@ File docxFile = new File(file.getParent() + File.separator + docxName); String tempDir = Global.getSystemConfig("temp.dir", "./attachment/temp"); File tempFile = new File(tempDir + File.separator + docxName); try { Document document = new Document(new FileInputStream(file)); try (FileInputStream is = new FileInputStream(file);) { Document document = new Document(is); document.save(tempFile.getAbsolutePath(), com.aspose.words.SaveFormat.DOCX); return tempFile; } catch (Exception e) {