aspose.words for java操作文件doc,設定一級二級三級標題以及段落表格等詳情

小哥哥呀發表於2019-01-16

實現將aspose.words的相關元件jar包

以下是我自己編輯整理的工具類,歡迎交流

package com;

import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aspose.words.CellMerge;
import com.aspose.words.CellVerticalAlignment;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.FontSettings;
import com.aspose.words.HeightRule;
import com.aspose.words.License;
import com.aspose.words.LineStyle;
import com.aspose.words.ParagraphAlignment;
import com.aspose.words.SaveFormat;
import com.aspose.words.Table;

/**
 * 
 * @ClassName:AsposeUtils
 * @Description: aspose.words操作文件工具類
 * @Date:2019年1月11日

 *
 */
public class AsposeUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(AsposeUtils.class);
    private static boolean AsposeLicense = false;
    static{
        try {
            //license.xml 
            InputStream is = AsposeUtils.class.getClassLoader().getResourceAsStream("license.xml"); 
            new License().setLicense(is); 
            AsposeLicense = true;
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
    /**
     * 驗證License
     * @return boolean
     */ 
    private static void getLicense() { 
        if (!AsposeLicense) { // 驗證License 若不驗證則轉化出的pdf文件會有水印產生
            LOGGER.info("**********驗證失敗,會產生水印***********");
        } 
        LOGGER.info("************驗證成功,已去除預設水印***********");
    }
    /**
     * 儲存pdf
     * @param path儲存目錄
     * @param doc原文件
     */
    public static void savePdf(String path,Document doc){
        String format = "pdf";
        save(path,doc,SaveFormat.PDF,format);
    }
    /**
     * 儲存doc
     * @param path儲存目錄
     * @param doc原文件
     */
    public static void saveDoc(String path,Document doc){
        String format = "doc";
        save(path,doc,SaveFormat.DOC,format);
    }
    public static void saveDocx(String path,Document doc){
        String format = "docx";
        save(path,doc,SaveFormat.DOCX,format);
    }
    /**
     * 儲存各種格式的文件
     * @param path儲存地址
     * @param doc儲存檔案
     * @param saveFormat儲存格式
     */
    private static void save(String path,Document doc,int saveFormat,String format){
        getLicense();
        try {
            String os = System.getProperty("os.name");
            if(os.toLowerCase().indexOf("linux") >= 0){
                LOGGER.info("************當前使用*****linux**"+ os +"*********");
                //設定一個字型目錄
                FontSettings.getDefaultInstance().setFontsFolder("/usr/share/fonts/", false);
            }
            doc.save(path, saveFormat);
        } catch (Exception e) {
            LOGGER.info("************儲存文件(格式為"+format+")出現異常***********");
            e.printStackTrace();
        }
    }
    /**
     * 製作報表總標題
     * @param builder
     * @param title
     */
    public static void setTitle(DocumentBuilder builder,String title){
        try {
            //設定字型格式
            builder.insertHtml("<p style='text-align:center'><font face='宋體' size='30' color='black'>"+ title +"</font></p>");
        } catch (Exception e) {
            LOGGER.info("************製作標題"+title+"出現異常***********");
            e.printStackTrace();
        }
    }
    /**
     * 製作一級標題
     * @param builder
     * @param title
     */
    private static void setTitle1(DocumentBuilder builder,String title1){
        try {
            builder.insertHtml("<h1 style='text-align:left;font-family:Simsun;'>"+ title1 +"</h1>");
        } catch (Exception e) {
            LOGGER.info("************製作一級標題"+title1+"出現異常***********");
            e.printStackTrace();
        }
    }
    /**
     * 製作二級標題
     * @param builder
     * @param title
     */
    private static void setTitle2(DocumentBuilder builder,String title2){
        try {
            builder.insertHtml("<h2 style='text-align:left;font-family:Simsun;'>"+ title2 +"</h2>");
        } catch (Exception e) {
            LOGGER.info("************製作二級標題"+title2+"出現異常***********");
            e.printStackTrace();
        }
    }
    /**
     * 製作三級標題
     * @param builder
     * @param title
     */
    private static void setTitle3(DocumentBuilder builder,String title3){
        try {
            builder.insertHtml("<h3 style='text-align:left;font-family:Simsun;'>"+ title3 +"</h3>");
        } catch (Exception e) {
            LOGGER.info("************製作三級標題"+title3+"出現異常***********");
            e.printStackTrace();
        }
    }
    /**
     * 區別各級標題
     * @param builder
     * @param title
     * @param level
     */
    public static void setTitleS(DocumentBuilder builder,String title,String level){
        switch (level) {
        case "1":
            setTitle1(builder, title);
            break;
        case "2":
            setTitle2(builder, title);
            break;
        case "3":
            setTitle3(builder, title);
            break;
        default:
            break;
        }
    }
    /**
     * 製作報表段落
     * @param builder
     * @param pragraph
     */
    public static void setParagraph(DocumentBuilder builder,String pragraph){
        try {
            //設定字型格式
            builder.insertHtml("<p><font face='宋體'>&nbsp;&nbsp; "+ pragraph +"</font></p>");
        } catch (Exception e) {
            LOGGER.info("************製作段落"+pragraph+"出現異常***********");
            e.printStackTrace();
        }
    }
    /**
     * 製作一個單元格並追加資料,單元格不合並
     * @param builder
     * @param width 設定單元格寬度
     * @param text
     */
    public static void setCell(DocumentBuilder builder,Double width,String text){
        builder.insertCell();
        if(width==null) width = 3d;
        builder.getCellFormat().setWidth(width);
        builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
        builder.write(text);
    }
    /**
     * 插入單元格,不設定寬度,單元格不合並
     * @param builder
     * @param text
     */
    public static void setCell(DocumentBuilder builder,String text){
        builder.insertCell();
        builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.write(text);
    }
    public static void setCell(DocumentBuilder builder,Long text){
        builder.insertCell();
        builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        if(text == null){
            builder.write("");
        }else{
            builder.write(text.toString());
        }
    }
    public static void setCell(DocumentBuilder builder,Double text){
        builder.insertCell();
        builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        if(text == null){
            builder.write("");
        }else{
            builder.write(text.toString());
        }
    }
    /**
     * 垂直合併單元格的第一格
     * @param builder
     * @param text
     */
    public static void setStartVerticalMerge(DocumentBuilder builder,String text){
        builder.insertCell();
        builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.write(text);
    }
    /**
     * 垂直合併單元格的後面格
     * @param builder
     * @param text
     */
    public static void setThenVerticalMerge(DocumentBuilder builder){
        builder.insertCell();
        // This cell is vertically merged to the cell above and should be empty.
        builder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
    }
    /**
     * 水平合併單元格的第一格
     * @param builder
     * @param text
     */
    public static void setStartHorizontalMerge(DocumentBuilder builder,String text){
        builder.insertCell();
        builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
        builder.write(text);
    }
    /**
     * 水平合併單元格的後面格
     * @param builder
     * @param text
     */
    public static void setThenHorizontalMerge(DocumentBuilder builder){
        builder.insertCell();
        // This cell is vertically merged to the cell above and should be empty.
        builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
    }
    /**
     * 製作表格資料行
     * @param builder
     */
    public static void setRow(DocumentBuilder builder){
        try {
            builder.getRowFormat().setHeadingFormat(true);
            builder.getRowFormat().getBorders().setLineStyle(LineStyle.SINGLE);
            builder.getRowFormat().setHeightRule(HeightRule.AUTO);
            builder.getRowFormat().setAllowBreakAcrossPages(true);
        } catch (Exception e) {
            LOGGER.info("************製作表格資料行出現異常***********");
            e.printStackTrace();
        }
    }
    /**
     * 製作表格
     * @param builder
     * @throws Exception 
     */
    public static Table setTable(DocumentBuilder builder){
        builder.moveToDocumentEnd();
        Table table = builder.startTable();
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        return table;
    }
}

 

相關文章