java產生word文檔

來源:互聯網
上載者:User

標籤:pack   .com   inf   close   檔案名稱   擷取值   ext   obj   contex   

這是工具類代碼

package common.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import sun.misc.BASE64Encoder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

 

public class DocUtil {

private Configuration configure=null;

public DocUtil(){
configure=new Configuration();
configure.setDefaultEncoding("utf-8");
}

/**
* 根據Doc模板產生word檔案
* @param dataMap 需要填入模板的資料
* @param downloadType 檔案名稱
* @param savePath 儲存路徑
*/
public void createDoc(Map<String,Object> dataMap,String downloadType,String savePath){
try {
//載入需要裝填的模板
Template template=null;
//設定模板裝置方法和路徑,FreeMarker支援多種模板裝載方法。可以重servlet,classpath,資料庫裝載。
//載入模板檔案,放在testDoc下
configure.setClassForTemplateLoading(this.getClass(), "testDoc");
//設定對象封裝器
// configure.setObjectWrapper(new DefaultObjectWrapper());
//設定異常處理器
configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
//定義Template對象,注意模板類型名字與downloadType要一致
template=configure.getTemplate(downloadType+".xml");
File outFile=new File(savePath);
Writer out=null;
out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
template.process(dataMap, out);
out.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}

這裡模板路徑為

 



public String getImageStr(String imgFile){
InputStream in=null;
byte[] data=null;
try {
in=new FileInputStream(imgFile);
data=new byte[in.available()];
in.read(data);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder=new BASE64Encoder();
return encoder.encode(data);
}

}

方法調用

 

@Override
public String exportWord(TeachMaterialDealInfo teachMaterialDealInfo,
String titleName) {
String s = UUID.randomUUID().toString();
String filepath = ServletActionContext.getServletContext().getRealPath("upload/notice/"+s+"/");
String zippath = ServletActionContext.getServletContext().getRealPath("upload/notice/"+s+"/week-job.zip");
String notice = ServletActionContext.getServletContext().getRealPath("upload/notice/");
createfile(notice);
createfile(filepath);
List<TeachMaterialDealInfo> teachMaterial = teachMaterialDealInfoDao.findDealInfo(teachMaterialDealInfo);
TeachMaterial teachMaterial1 = applicationDao.getMaterInfoByMaterId(teachMaterialDealInfo.getMaterid());
DocUtil docUtil=null;
Map<String, Object> dataMap=null;
....給dataMap賦值如dataMap.put("writername", “呵呵呵”);調用xml檔案是就是通過這個來擷取值得這裡我用的是freemarker方法


docUtil.createDoc(dataMap, "basedoc", filepath+"/"+teachMaterial.get(i).getWritername()+".doc");


}

 xml檔案為先寫好word檔案後轉存為xml

 

 

 

 

java產生word文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.