aspose家族的包很丰富 有操作word pdf excell 等等 今天主要说的是操作word
先引入坐标 提一嘴 aspose的坐标直接从maven仓库引入会报找不到 所以要先配置aspose自己的仓库坐标
<dependencies>
<dependency>
<groupId>com.asposegroupId>
<artifactId>aspose-wordsartifactId>
<version>18.2version>
<classifier>jdk16classifier>
dependency>
<dependency>
<groupId>com.asposegroupId>
<artifactId>aspose-pdfartifactId>
<version>18.2version>
dependency>
<dependency>
<groupId>com.asposegroupId>
<artifactId>aspose-cellsartifactId>
<version>18.2version>
dependency>
dependencies>
<repositories>
<repository>
<id>AsposeJavaAPIid>
<name>Aspose Java APIname>
<url>https://repository.aspose.com/repo/url>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>AsposeJavaAPIid>
<url>https://repository.aspose.com/repo/url>
pluginRepository>
pluginRepositories>
如果maven坐标还是无法下载 可以试试如下的操作
除了用eclipse添加到maven仓库,我见到还有一种处理方式
内置属性:主要有两个常用内置属性—— b a s e d i r 表 示 项 目 根 目 录 , 即 包 含 p o m . x m l 文 件 的 目 录 ; {basedir}表示项目根目录,即包含pom.xml文件的目录; basedir表示项目根目录,即包含pom.xml文件的目录;{version}表示项目版本。
初次使用aspose导出word会带水印 如下
由于版权信息 所以有水印 去水印 很方便 加载License即可 封装了一个工具类
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.words.BorderType;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.License;
import com.aspose.words.LineStyle;
import com.aspose.words.ParagraphAlignment;
import com.aspose.words.SaveFormat;
import com.aspose.words.Section;
/**
* word处理
*/
public class AsposeWordUtil {
// 公司购买的license
private static final String LIC = "";
private static boolean haveLoad = false;
private static AsposeWordUtil wordStyleUtil = new AsposeWordUtil();
public static AsposeWordUtil getInstance() {
return wordStyleUtil;
}
/**
* 获取文档
* @param template 导出word文档模板
* @return
* @throws Exception
*/
public Document getDocument(String template) throws Exception {
// 加载License
registeLicense();
Document doc = new Document();
if (null != template) {
try {
doc = new Document(new FileInputStream(template));
} catch (Exception e) {
throw new Exception("没有找到导出模板!");
}
}
return doc;
}
/**
* 获取文档
* @param template 导出word文档模板
* @return
* @throws Exception
*/
public Document getDocument(InputStream in) throws Exception {
// 加载License
registeLicense();
Document doc = new Document();
if (null != in) {
try {
doc = new Document(in);
} catch (Exception e) {
throw new Exception("没有找到文件!");
}
}
return doc;
}
/**
* 清除格式
* @param builder
*/
private static void cleanFormatting(DocumentBuilder builder) throws Exception {
// 清除字体格式
builder.getFont().clearFormatting();
builder.getParagraphFormat().clearFormatting();
}
/**
* 文档保存
* @param expWordFile
* @param doc
* @throws Exception
*/
private void saveDoc(File expWordFile, Document doc) throws Exception {
doc.updateFields();
doc.updatePageLayout();
doc.save(expWordFile.getPath() + File.separatorChar + expWordFile.getName() + ".docx");
}
/**
* 创建另外一个章节
* @param doc
* @throws Exception
*/
private void addSection(Document doc) throws Exception {
Section section = new Section(doc);
doc.getSections().add(section);
}
/**
* 插入目录
* @param builder
* @param doc
* @throws Exception
*/
private void insertTableOfContent(DocumentBuilder builder, Document doc) throws Exception {
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getFont().setSize(32);
// 设置加粗
builder.getFont().setBold(true);
builder.writeln("目 录");
builder.getParagraphFormat().clearFormatting();
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
addSection(doc);
}
/**
* 插入页码
* @param builder
*/
private void insertPageNum(DocumentBuilder builder) throws Exception {
// 将builder移动至页脚
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
// 设置起始页码
builder.getPageSetup().setPageStartingNumber(1);
builder.getParagraphFormat().getBorders().getByBorderType(BorderType.TOP).setLineStyle(LineStyle.SINGLE);
// 页码对齐方式(居中,居左,居右)
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write("第");
// 插入页数
builder.insertField("PAGE", "PAGE");
builder.write("页 共");
// 插入总共的页数
builder.insertField("NUMPAGES", "NUMPAGES");
builder.write("页");
}
/**
* 加载license流避免导出word出现侵权提示
*/
private void registeLicense() {
if (!haveLoad) {
InputStream licenseStream = new ByteArrayInputStream(LIC.getBytes());
License license = new License();
try {
license.setLicense(licenseStream);
} catch (Exception e) {
e.printStackTrace();
haveLoad = false;
}
haveLoad = true;
}
}
public static InputStream doc2pdf(InputStream in, String name) {
FileOutputStream os = null;
FileInputStream inputStream = null;
try {
File file = new File("E:/demo11.pdf"); // 新建一个空白pdf文档
os = new FileOutputStream(file);
Document doc = AsposeWordUtil.getInstance().getDocument(in); // in是将要被转化的word文档流
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML,
// OpenDocument, PDF, EPUB, XPS, SWF
// 相互转换
os.close();
if (!file.exists()) {
return null;
} else {
inputStream = new FileInputStream(file);
return inputStream;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return inputStream;
}
}
简单demo
import java.io.File;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.SaveFormat;
public class TestAsposeWords {
public static void main(String[] args) {
try {// 载入模板
String fileName = "D:\\Desktop\\123.docx";
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
file = new File(fileName);
file.createNewFile();
Document doc = AsposeWordUtil.getInstance().getDocument(fileName);
DocumentBuilder builder = new DocumentBuilder(doc);
// doc.setNodeChangingCallback(new HandleNodeChangingFontChanger());
builder.write("create Table");// 填入内容,
// We call this method to start building the table.
builder.startTable();
builder.insertCell();
builder.write("Row 1, Cell 1 Content.");
// Build the second cell
builder.insertCell();
builder.write("Row 1, Cell 2 Content.");
// Call the following method to end the row and start a new row.
builder.endRow();
// Build the first cell of the second row.
builder.insertCell();
builder.write("Row 2, Cell 1 Content");
// Build the second cell.
builder.insertCell();
builder.write("Row 2, Cell 2 Content.");
builder.endRow();
// Signal that we have finished building the table.
builder.endTable();
doc.save(fileName, SaveFormat.DOCX);// 保存文件
System.err.println("ok");
} catch (Exception e) {
e.printStackTrace();
}
}
}