java簡(jiǎn)單操作word實(shí)例
更新時(shí)間:2016年03月06日 15:13:39 作者:ngulc
這篇文章主要為大家詳細(xì)介紹了java簡(jiǎn)單操作word實(shí)例,感興趣的朋友可以參考一下
本文為大家分享了java簡(jiǎn)單操作word例子,供大家參考,具體內(nèi)容如下
package apache.poi;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ExportDocTest {
public static void main(String[] args) {
String destFile="D:\\11.doc";
//#####################根據(jù)自定義內(nèi)容導(dǎo)出Word文檔#################################################
StringBuffer fileCon=new StringBuffer();
fileCon.append(" 張大炮 男 317258963215223\n" +
"2011 09 2013 07 3\n" +
" 二炮研究 成人\n" +
"2013000001 2013 07 08");
fileCon.append("\n\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
new ExportDocTest().exportDoc(destFile, fileCon.toString());
//##################根據(jù)Word模板導(dǎo)出單個(gè)Word文檔###################################################
Map<String, String> map=new HashMap<String, String>();
map.put("name", "Zues");
map.put("sex", "男");
map.put("idCard", "200010");
map.put("year1", "2000");
map.put("month1", "07");
map.put("year2", "2008");
map.put("month2", "07");
map.put("gap", "2");
map.put("zhuanye", "計(jì)算機(jī)科學(xué)與技術(shù)");
map.put("type", "研究生");
map.put("bianhao", "2011020301");
map.put("nowy", "2011");
map.put("nowm", "01");
map.put("nowd", "20220301");
//注意biyezheng_moban.doc文檔位置,此例中為應(yīng)用根目錄
HWPFDocument document=new ExportDocTest().replaceDoc("biyezheng_moban.doc", map);
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
try {
document.write(ostream);
//輸出word文件
OutputStream outs=new FileOutputStream(destFile);
outs.write(ostream.toByteArray());
outs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param destFile
* @param fileCon
*/
public void exportDoc(String destFile,String fileCon){
try {
//doc content
ByteArrayInputStream bais = new ByteArrayInputStream(fileCon.getBytes());
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();
directory.createDocument("WordDocument", bais);
FileOutputStream ostream = new FileOutputStream(destFile);
fs.writeFilesystem(ostream);
bais.close();
ostream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 讀取word模板并替換變量
* @param srcPath
* @param map
* @return
*/
public HWPFDocument replaceDoc(String srcPath, Map<String, String> map) {
try {
// 讀取word模板
FileInputStream fis = new FileInputStream(new File(srcPath));
HWPFDocument doc = new HWPFDocument(fis);
// 讀取word文本內(nèi)容
Range bodyRange = doc.getRange();
// 替換文本內(nèi)容
for (Map.Entry<String, String> entry : map.entrySet()) {
bodyRange.replaceText("${" + entry.getKey() + "}", entry
.getValue());
}
return doc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
您可能感興趣的文章:
- java使用POI實(shí)現(xiàn)html和word相互轉(zhuǎn)換
- Java使用poi將word轉(zhuǎn)換為html
- java實(shí)現(xiàn)word文件轉(zhuǎn)html文件
- java導(dǎo)出生成word的簡(jiǎn)單方法
- java使用poi讀取ppt文件和poi讀取excel、word示例
- 實(shí)例講解Java讀取一般文本文件和word文檔的方法
- 使用Java讀取Word文件的簡(jiǎn)單例子分享
- java使用Jsoup組件生成word文檔
- java/word+fusionchart生成圖表深入分析
- Java用freemarker導(dǎo)出word實(shí)用示例
- Java實(shí)現(xiàn)將word轉(zhuǎn)換為html的方法示例【doc與docx格式】
相關(guān)文章
Spring集成JPA配置懶加載報(bào)錯(cuò)解決方案
這篇文章主要介紹了Spring集成JPA配置懶加載報(bào)錯(cuò)解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
線程局部變量的實(shí)現(xiàn)?ThreadLocal使用及場(chǎng)景介紹
這篇文章主要為大家介紹了線程局部變量的實(shí)現(xiàn)?ThreadLocal使用及場(chǎng)景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Java8實(shí)戰(zhàn)之Stream的延遲計(jì)算
JDK中Stream的中間函數(shù)如 filter(Predicate super T>)是惰性求值的,filter并非對(duì)流中所有元素調(diào)用傳遞給它的Predicate,下面這篇文章主要給大家介紹了關(guān)于Java8實(shí)戰(zhàn)之Stream延遲計(jì)算的相關(guān)資料,需要的朋友可以參考下2021-09-09
SpringBoot整合之SpringBoot整合MongoDB的詳細(xì)步驟
這篇文章主要介紹了SpringBoot整合之SpringBoot整合MongoDB的詳細(xì)步驟,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
Java使用DFA算法實(shí)現(xiàn)敏感詞過濾的示例代碼
很多項(xiàng)目中都會(huì)有一個(gè)敏感詞管理模塊,本文主要介紹了Java使用DFA算法實(shí)現(xiàn)敏感詞過濾的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

