Spire操作word⽂档Spire操作word⽂档
1、基本⽂档⽅⾯
// 测试⽤例
PersonalCreditRecord credit =getPersonalCreditRecord();
//Create word document.
Document document =new Document();
//Add a new section.
Section section =createSectionTitle(document);
// 信息汇总
setSummaryCredit(section, credit);
创建word,并使⽤信息填充
(这⾥不仅仅是信息的填充,还有⽂档格式的设置等)
private static void setSummaryCredit(Section section, PersonalCreditRecord r){
Paragraph paragraph1 = section.addParagraph();
TextRange tr1 = paragraph1.appendText("\r\n信息汇总\r\n");
Paragraph paragraph2 = section.addParagraph();
TextRange tr2 = paragraph2.appendText("个⼈姓名:"+ r.getName()+"\r\n"
+"\r\n申请时间:"+ r.getApplyTime()+"\r\n"
+"\r\n⽣效时间:"+ r.getValidTime()+"\r\n");
}
2、表格⽅⾯
// 基本信息
Document document1 =new Document();
Section section1 =createSection(document1);
crateSecodTitle(section1,"基本信息");
createBaseInfoPart(section1, BaseCredit().getBaseInfoSubTypeList());
addLineBreak(section1);
创建表格,并填充内容
包括格式的设置
private static void createBaseInfoPart(Section section,
List<PersonalBaseInfoSubTypeCredit> baseInfoSubTypeList) {
if(GcmsListUtils.isEmptyList(baseInfoSubTypeList)){
return ;
}
// 实现表格的循环
for (PersonalBaseInfoSubTypeCredit subType : baseInfoSubTypeList) {
// ⼦类型标题
setBaseInfoSubTypeTitle(section, subType);
List<PersonalBaseInfoSubTypeCreditChild> subTypeChild = SubTypeChild();            for (PersonalBaseInfoSubTypeCreditChild stc : subTypeChild) {
PersonalBaseTablePO po1 = new PersonalBaseTablePO();
po1.setTitle("评分");
crateBaseInfoTable(section,po1);
PersonalBaseTablePO po11 = new PersonalBaseTablePO();
po11.setTitle("评分信息");
// 实现表格⾏的循环
crateBaseInfoTable(section,po11);
}
}
}
private static void crateBaseInfoTable(Section section, PersonalBaseTablePO po1) {
int rowNum = List().size() + 1;
// 创建表
Table table = createTable(section, rowNum);
// Title
Title(), table);
// 给表格复制
for (int i = 0; i &List().size() ;i++) {
TableRow row201202 = Rows().get(i+1);
/
/ 给两列赋值
setPramsCellValue(row201202,List().get(i).getName());
setPramsCellValue(row201202,List().get(i).getValue());
}
}
3、对多个⽂档进⾏合并
// 合并⽂档
XWPFDocument outDocument =Doc(document,document1);
/**
* 合并docx⽂件
*  srcDocxs 需要合并的⽬标docx⽂件
*  destDocx 合并后的docx输出⽂件
*/
public XWPFDocument mergeDoc(Document ... ds)throws Exception{
if(ds == null || ds.length ==0){
if(ds == null || ds.length ==0){
return null;
}
OutputStream dest = null;
List<OPCPackage> opcpList =new ArrayList<OPCPackage>();
List<File> tempFileList =new ArrayList<File>();
/**
* 循环获取每个docx⽂件的OPCPackage对象
*/
for(int i=0;i<ds.length;i++){
File tempFile1 =getTempFile(ds[i]);
tempFileList.add(tempFile1);
OPCPackage srcPackage =  null;
try{
srcPackage = OPCPackage.open(tempFile1);
}catch(Exception e){
e.printStackTrace();
}
if(null != srcPackage){
opcpList.add(srcPackage);
}
}
int opcpSize = opcpList.size();
//获取的OPCPackage对象⼤于0时,执⾏合并操作
if(opcpSize >0){
try{
XWPFDocument src1Document =new (0));
CTBody src1Body = Document().getBody();
//OPCPackage⼤于1的部分执⾏合并操作
if(opcpSize >1){
for(int i =1; i < opcpSize; i++){
OPCPackage src2Package = (i);
@SuppressWarnings("resource")
CTBody src2Body =new XWPFDocument(src2Package).getDocument().getBody();
appendBody(src1Body, src2Body);
}
}
//将合并的⽂档写⼊⽬标⽂件中
return src1Document;
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
//注释掉以下部分,去除影响⽬标⽂件srcDocxs。
/*for (OPCPackage opcPackage : opcpList) {
if(null != opcPackage){
try {
opcPackage.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/getsavefilename
//关闭流
IOUtils.closeQuietly(dest);
for(File file : tempFileList){
try{
file.deleteOnExit();
}catch(Exception e2){
e2.printStackTrace();
e2.printStackTrace();
}
}
}
}
return null;
}
/**
* 合并⽂档内容
* @param src ⽬标⽂档
* @param append 要合并的⽂档
* @throws Exception
*/
private void appendBody(CTBody src, CTBody append)throws Exception {
XmlOptions optionsOuter =new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = lText(optionsOuter);
String srcString = lText();
String prefix = srcString.substring(0, srcString.indexOf(">")+1);
String mainPart = srcString.substring(srcString.indexOf(">")+1,
srcString.lastIndexOf("<"));
String sufix = srcString.substring(srcString.lastIndexOf("<"));
String addPart = appendString.substring(appendString.indexOf(">")+1,
appendString.lastIndexOf("<"));
CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart
+ sufix);
src.set(makeBody);
}
将两个⽂档可以合并在⼀起,这样就可以有超多的花样来玩⼉
附加(⽂件下载)
//保存⽂档
try(ServletOutputStream out = OutputStream()){
String fileName = Name()+"信⽤⽂档"+GcmsDateUtil.currentTimeMillis()+".docx", StandardCharsets.String( ));
response.setContentType(MediaType.APPLICATION_String());
//特殊处理,解决 Safari 浏览器下载⽂件名中⽂乱码问题。
response.setHeader("Content-Disposition","attachment; filename=\""+ fileName +"\"; filename*=utf-8''"+ fileName);
//解决前端跨域提⽰问题。
response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
response.setHeader("Access-Control-Allow-Origin","*");
response.setHeader("Cache-Control","no-cache");
outDocument.write(out);
out.flush();
}catch(IOException e){
throw e;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。