packagecom;importjava.io.InputStream;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.aspose.words.CellMerge;importcom.aspose.words.CellVerticalAlignment;importcom.aspose.words.Document;importcom.aspose.words.DocumentBuilder;importcom.aspose.words.FontSettings;importcom.aspose.words.HeightRule;importcom.aspose.words.License;importcom.aspose.words.LineStyle;importcom.aspose.words.ParagraphAlignment;importcom.aspose.words.SaveFormat;importcom.aspose.words.Table;/***
* @ClassName:AsposeUtils
* @Description: aspose.words操作文档工具类
* @Date:2019年1月11日
**/
public classAsposeUtils {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");newLicense().setLicense(is);
AsposeLicense= true;
}catch(Exception e) {
e.printStackTrace();
}
}/*** 验证License
*@returnboolean*/
private static voidgetLicense() {if (!AsposeLicense) { //验证License 若不验证则转化出的pdf文档会有水印产生
LOGGER.info("**********验证失败,会产生水印***********");
}
LOGGER.info("************验证成功,已去除默认水印***********");
}/*** 保存pdf
*@parampath保存目录
*@paramdoc原文档*/
public static voidsavePdf(String path,Document doc){
String format= "pdf";
save(path,doc,SaveFormat.PDF,format);
}/*** 保存doc
*@parampath保存目录
*@paramdoc原文档*/
public static voidsaveDoc(String path,Document doc){
String format= "doc";
save(path,doc,SaveFormat.DOC,format);
}public static voidsaveDocx(String path,Document doc){
String format= "docx";
save(path,doc,SaveFormat.DOCX,format);
}/*** 保存各种格式的文档
*@parampath保存地址
*@paramdoc保存文件
*@paramsaveFormat保存格式*/
private static void save(String path,Document doc,intsaveFormat,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();
}
}/*** 制作报表总标题
*@parambuilder
*@paramtitle*/
public static voidsetTitle(DocumentBuilder builder,String title){try{//设置字体格式
builder.insertHtml("
"+ title +"
");}catch(Exception e) {
LOGGER.info("************制作标题"+title+"出现异常***********");
e.printStackTrace();
}
}/*** 制作一级标题
*@parambuilder
*@paramtitle*/
private static voidsetTitle1(DocumentBuilder builder,String title1){try{
builder.insertHtml("
"+ title1 +"
");}catch(Exception e) {
LOGGER.info("************制作一级标题"+title1+"出现异常***********");
e.printStackTrace();
}
}/*** 制作二级标题
*@parambuilder
*@paramtitle*/
private static voidsetTitle2(DocumentBuilder builder,String title2){try{
builder.insertHtml("
"+ title2 +"
");}catch(Exception e) {
LOGGER.info("************制作二级标题"+title2+"出现异常***********");
e.printStackTrace();
}
}/*** 制作三级标题
*@parambuilder
*@paramtitle*/
private static voidsetTitle3(DocumentBuilder builder,String title3){try{
builder.insertHtml("
"+ title3 +"
");}catch(Exception e) {
LOGGER.info("************制作三级标题"+title3+"出现异常***********");
e.printStackTrace();
}
}/*** 区别各级标题
*@parambuilder
*@paramtitle
*@paramlevel*/
public static voidsetTitleS(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;
}
}/*** 制作报表段落
*@parambuilder
*@parampragraph*/
public static voidsetParagraph(DocumentBuilder builder,String pragraph){try{//设置字体格式
builder.insertHtml("
"+ pragraph +"
");}catch(Exception e) {
LOGGER.info("************制作段落"+pragraph+"出现异常***********");
e.printStackTrace();
}
}/*** 制作一个单元格并追加数据,单元格不合并
*@parambuilder
*@paramwidth 设置单元格宽度
*@paramtext*/
public static voidsetCell(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);
}/*** 插入单元格,不设置宽度,单元格不合并
*@parambuilder
*@paramtext*/
public static voidsetCell(DocumentBuilder builder,String text){
builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.write(text);
}public static voidsetCell(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 voidsetCell(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());
}
}/*** 垂直合并单元格的第一格
*@parambuilder
*@paramtext*/
public static voidsetStartVerticalMerge(DocumentBuilder builder,String text){
builder.insertCell();
builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.write(text);
}/*** 垂直合并单元格的后面格
*@parambuilder
*@paramtext*/
public static voidsetThenVerticalMerge(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);
}/*** 水平合并单元格的第一格
*@parambuilder
*@paramtext*/
public static voidsetStartHorizontalMerge(DocumentBuilder builder,String text){
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.write(text);
}/*** 水平合并单元格的后面格
*@parambuilder
*@paramtext*/
public static voidsetThenHorizontalMerge(DocumentBuilder builder){
builder.insertCell();//This cell is vertically merged to the cell above and should be empty.
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}/*** 制作表格数据行
*@parambuilder*/
public static voidsetRow(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();
}
}/*** 制作表格
*@parambuilder
*@throwsException*/
public staticTable setTable(DocumentBuilder builder){
builder.moveToDocumentEnd();
Table table=builder.startTable();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);returntable;
}
}