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)文章
如何使用SpringBootCondition更自由地定義條件化配置
這篇文章主要介紹了如何使用SpringBootCondition更自由地定義條件化配置,幫助大家更好的理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下2021-04-04SpringBoot初始化接口CommandLineRunner示例詳解
這篇文章主要介紹了SpringBoot初始化接口CommandLineRunner,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07使用c3p0連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪改查
這篇文章主要為大家詳細(xì)介紹了使用c3p0連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪改查,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08詳解java為什么不允許類(lèi)多重繼承卻允許接口多重繼承
這篇文章主要介紹了java為什么不允許類(lèi)多重繼承卻允許接口多重繼承,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05java 數(shù)據(jù)庫(kù)連接與增刪改查操作實(shí)例詳解
這篇文章主要介紹了java 數(shù)據(jù)庫(kù)連接與增刪改查操作,結(jié)合實(shí)例形式詳細(xì)分析了java使用jdbc進(jìn)行數(shù)據(jù)庫(kù)連接及增刪改查等相關(guān)操作實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2019-11-11SSH框架網(wǎng)上商城項(xiàng)目第14戰(zhàn)之商城首頁(yè)UI的設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第14戰(zhàn)之商城首頁(yè)UI的設(shè)計(jì),感興趣的小伙伴們可以參考一下2016-06-06基于SpringBoot bootstrap.yml配置未生效的解決
這篇文章主要介紹了基于SpringBoot bootstrap.yml配置未生效的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10