java 간단한 조작word 실례
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";
//##################### Word #################################################
StringBuffer fileCon=new StringBuffer();
fileCon.append(" 317258963215223
" +
"2011 09 2013 07 3
" +
"
" +
"2013000001 2013 07 08");
fileCon.append("
\r
");
new ExportDocTest().exportDoc(destFile, fileCon.toString());
//################## Word 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", " ");
map.put("type", " ");
map.put("bianhao", "2011020301");
map.put("nowy", "2011");
map.put("nowm", "01");
map.put("nowd", "20220301");
// biyezheng_moban.doc ,
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
Range bodyRange = doc.getRange();
//
for (Map.Entry<String, String> entry : map.entrySet()) {
bodyRange.replaceText("${" + entry.getKey() + "}", entry
.getValue());
}
return doc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되기를 바랍니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.