我的需求是這樣的:
word文檔類型是xml,用文本編輯器打開(kāi)看到以下代碼(我只拷貝了頭部部分代碼)
java如何能把它轉(zhuǎn)換為word類型的doc文檔,Apache的poi好像只能把word文檔轉(zhuǎn)換成xml、html等格式的,但不能反過(guò)來(lái)轉(zhuǎn)。請(qǐng)問(wèn)有沒(méi)有解決過(guò)這種需求的朋友,先謝謝了!
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" Target="docProps/custom.xml"/>
</Relationships>
</pkg:xmlData>
</pkg:part>
@smilesnake 首先感謝分享代碼,可能是我的問(wèn)題,沒(méi)有把問(wèn)題描述清楚,我目前就是按照你的思路來(lái)做的,但問(wèn)題是最后給用戶生成的這個(gè)doc文檔是xml類型的(另存為的時(shí)候能夠看到)并且用戶打開(kāi)編輯后再去另存的時(shí)候就變成了xml為后綴的文檔了,導(dǎo)致后面打不開(kāi),所以我的問(wèn)題是如何能生成word類型的文檔
大致的思路是先用office2003或者2007編輯好word的樣式,然后另存為xml,將xml翻譯為FreeMarker模板,最后用java來(lái)解析FreeMarker模板并輸出Doc。經(jīng)測(cè)試這樣方式生成的word文檔完全符合office標(biāo)準(zhǔn),樣式、內(nèi)容控制非常便利,打印也不會(huì)變形,生成的文檔和office中編輯文檔完全一樣。
用xml做導(dǎo)出方案。
先創(chuàng)建一個(gè)word文檔,按照需求在word中填好一個(gè)模板,然后把對(duì)應(yīng)的數(shù)據(jù)換成變量${},然后將文檔保存為xml文檔格式,使用文檔編輯器打開(kāi)這個(gè)xml格式的文檔,去掉多余的xml符號(hào),使用Freemarker讀取這個(gè)文檔然后替換掉變量,輸出word文檔即可
需要freemarker jar包
/**
*/
package com.wt.service.download;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import net.paoding.rose.web.Invocation;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
/**
*/
@Service
public class DownloadService {
private Logger logger = Logger.getLogger(this.getClass());
/**
* downLoad:(文件下載). <br/>
*
* @author wpengfei
* @param inv
* @param fileName
* @param path
* @throws IOException
* @since JDK 1.6
*/
public void downLoad(Invocation inv, String fileName, String path) throws IOException {
File file = new File(path);// 構(gòu)造要下載的文件
if (file.exists()) {
InputStream ins = null;
BufferedInputStream bins = null;
OutputStream outs = null;
BufferedOutputStream bouts = null;
try {
ins = new FileInputStream(path);// 構(gòu)造一個(gè)讀取文件的IO流對(duì)象
bins = new BufferedInputStream(ins);// 放到緩沖流里面
outs = inv.getResponse().getOutputStream();// 獲取文件輸出IO流
bouts = new BufferedOutputStream(outs);
String path1 = inv.getRequest().getSession().
getServletContext().getRealPath("/WEB-INF/downloads");
logger.info(path1);
inv.getResponse().setContentType("application/x-download");// 設(shè)置response內(nèi)容的類型
inv.getResponse().setHeader("Content-disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));// 設(shè)置頭部信息
// inv.getResponse().setContentLength((int)file.length());
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 開(kāi)始向網(wǎng)絡(luò)傳輸文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);
}
bouts.flush();// 這里一定要調(diào)用flush()方法
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bouts != null) {
bouts.close();
}
if (outs != null) {
outs.close();
}
if (bins != null) {
bins.close();
}
if (ins != null) {
ins.close();
}
}
} else {
logger.info("導(dǎo)出的文件不存在");
}
}
}
package com.wt.common.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
*/
public class WordUtil {
@SuppressWarnings("rawtypes")
public static String createWord2(Map dataMap, String templateName, String filePath, String fileName) {
try {
// 創(chuàng)建配置實(shí)例
Configuration configuration = new Configuration();
// 設(shè)置編碼
configuration.setDefaultEncoding("UTF-8");
// ftl模板文件統(tǒng)一放至 com.lun.template 包下面
configuration.setClassForTemplateLoading(WordUtil.class, "\\com\\wt\\common\\util\\");
// 獲取模板
Template template = configuration.getTemplate(templateName);
// 輸出文件
File outFile = new File(filePath + File.separator + fileName);
// 如果輸出目標(biāo)文件夾不存在,則創(chuàng)建
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
// 將模板和數(shù)據(jù)模型合并生成文件
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
// 生成文件
template.process(dataMap, out);
// 關(guān)閉流
out.flush();
out.close();
return filePath + File.separator + fileName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package com.wt.controllers.test1;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.wt.common.util.CommonsUtil;
import com.wt.common.util.Constants;
import com.wt.common.util.ResponseObject;
import com.wt.common.util.WordUtil;
import com.wt.service.download.DownloadService;
/**
*/
@Path("/word")
public class WordController {
@Autowired
private DownloadService downloadService;
private String filePath; //文件路徑
// private String fileName; //文件名稱
private String fileOnlyName; //文件唯一名稱
/**
* createWord2:(這里用一句話描述這個(gè)方法的作用). <br/>
* localhost:8080/test1/word/createWord2
*
* @author wpengfei
* @param inv
* @return
* @throws IOException
* @since JDK 1.6
*/
@Get("/createWord2")
public String createWord2(Invocation inv) throws IOException {
/** 用于組裝word頁(yè)面需要的數(shù)據(jù) */
Map<String, Object> dataMap = new HashMap<String, Object>();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
dataMap.put("startTime", sdf.format(new Date()));
dataMap.put("endTime", sdf.format(new Date()));
dataMap.put("count", 1);
dataMap.put("username", "Tom");
dataMap.put("courseName", "物理");
dataMap.put("className", "1班");
dataMap.put("materialName", "體育學(xué)");
dataMap.put("materialVer", 1.0);
dataMap.put("teachAim", "諾克爾12421價(jià)是否可驕傲了空間阿凡達(dá)撿垃圾覅文件附件安防奇偶萬(wàn)佛諾克爾12421價(jià)是否可驕傲了空間阿凡達(dá)撿垃圾覅文件附件安防奇偶萬(wàn)佛諾克爾12421價(jià)是否可驕傲了空間阿凡達(dá)撿垃圾覅文件附件安防奇偶萬(wàn)佛諾克爾12421價(jià)是否可驕傲了空間阿凡達(dá)撿垃圾覅文件附件安防奇偶萬(wàn)佛諾克爾12421價(jià)是否可驕傲了空間阿凡達(dá)撿垃圾覅文件附件安防奇偶萬(wàn)佛");
//文件導(dǎo)出的目標(biāo)路徑
filePath=Constants.UPLOAD_BASE_FOLD;
StringBuffer sb=new StringBuffer();
sb.append(sdf.format(new Date()));
sb.append("_");
Random r=new Random();
sb.append(r.nextInt(100));
//文件唯一名稱
fileOnlyName = "testDoc11_"+sb+".doc";
/** 生成word */
String result = WordUtil.createWord2(dataMap, "testDoc11.ftl", filePath, fileOnlyName);
if(StringUtils.isNotBlank(result)){
downloadService.downLoad(inv, fileOnlyName, result);
}
return "@";
}
}
這是freemarker生成word文檔的通病,它本質(zhì)上還是一個(gè)xml文本,可以看看:http://www.xdocin.com/office....,它的結(jié)果是真正的docx格式
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。