本文目錄一覽:
java導出word表格
首先我用的技術是 poi
這是代碼,一個工具類得調用
public class WordUtil {
/**
* 基於模板文件導出 word 文檔,此方法主要是用來處理文檔中需要替換的文本內容,對圖片和表格無效
*
* @param templatePath
* 模板文件的路徑,要求路徑中要包含全名,並且模板文件只能是 07 及以上格式,即 docx 的文件
* @param destFilePath
* 導出文件的存放路徑,包含文件名,例如,E:/test/小區公告.docx
* @param data
* 用來替換文檔中預定義的字符串,要求預定義的字符串與 data 中的 key 值要相同
*/
public static void exportWordByTemplate(String templatePath,
String destFilePath, MapString, String data) {
FileOutputStream out = null;
XWPFDocument doc = null;
try {
doc = new XWPFDocument(POIXMLDocument.openPackage(templatePath));
ListXWPFRun listRun;
ListXWPFParagraph listParagraphs = doc.getParagraphs();
for (int i = 0; i listParagraphs.size(); i++) {
listRun = listParagraphs.get(i).getRuns();
for (int j = 0; j listRun.size(); j++) {
if (data.get(listRun.get(j).getText(0)) != null) {
String val = data.get(listRun.get(j).getText(0));
listRun.get(j).setText(val, 0);
}
}
}
File destFile = new File(destFilePath);
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
out = new FileOutputStream(destFilePath);
doc.write(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 基於模板文件導出 word 文檔,該方法支持03格式,但是此方法只能保留文檔內容,不能保留文檔中的樣式和圖片,建議將模板使用 07 的格式保存
*
* @param templatePath
* 模板文件的路徑
* @param destFilePath
* 導出文件的存放路徑,包含文件名,例如,E:/test/小區公告.doc
* @param data
* 用來替換文檔中預定義的字符串,要求預定義的字符串與 data 中的 key 值要相同
*/
public static void export03WordByTemplate(String templatePath,
String destFilePath, MapString, String data) {
try {
WordExtractor doc = new WordExtractor(new FileInputStream(
templatePath));
String content = doc.getText();
for (String key : data.keySet()) {
content = content.replaceAll(key, data.get(key));
}
byte b[] = content.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();
directory.createDocument(“WordDocument”, bais);
FileOutputStream ostream = new FileOutputStream(destFilePath);
fs.writeFilesystem(ostream);
bais.close();
ostream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
MapString, String maps = new HashMapString, String();
maps.put(“appellation”, “萬達公寓業主:”);
maps.put(
“main_body”,
“輸出的內容”);
maps.put(“date”, “2013年1月23日”);
exportWordByTemplate(“E:/sss 2.docx”, “E:/test/test.doc”, maps);
}
}
“E:/sss 2.docx 模板存放的地址。
E:/test/test.doc 新生成的地址。
請教java html導出word如何實現
java將html導出word不用忘記html/html這對標籤
//換頁
span style=’font-size:16px;line-height:150%;font-family:”Times New Roman”;
mso-fareast-font-family:宋體;mso-font-kerning:1px;mso-ansi-language:EN-US;
mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA’br clear=all style=’mso-special-character:page-break;page-break-before:always’
/span
//換行
p style=’line-height:150%’span style=’font-size:16px;line-height:150%’o:p /o:p/span/p
查看的話 打開word 視圖——頁面 就能看出看出效果
[java] view plain copy print?
ArrayList records = form.getRecords();//獲取數據庫數據
if(null!=records0!=records.size()){
//html拼接出word內容
String content=”html”;
for (int i = 0; i records.size(); i++) {
Record record =(Record) records.get(i);
//從數據庫中獲得數據,將oracle中的clob數據類型轉換成string類型
Method method = record.get(“CONTENT”).getClass().getMethod(“getVendorObj”,new Class[]{});
CLOB clob = (CLOB)method.invoke(record.get(“CONTENT”));
String cx = clob.getSubString((long) 1, (int) clob.length());
String title= (String) record.get(“TITLE”);
//html拼接出word內容
content+=”div style=\”text-align: center\”span style=\”font-size: 24px\”span style=\”font-family: 黑體\””+title+”br / br / /span/span/div”;
content+=”div style=\”text-align: left\”span “+cx+”br / br / /span/span/div”;
//插入分頁符
content+=”span lang=EN-US style=’font-size:16px;line-height:150%;mso-fareast-font-family:宋體;mso-font-kerning:1px;mso-ansi-language:EN-US;mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA’br clear=all style=’page-break-before:always’/span”;
content+=”p class=MsoNormal style=’line-height:150%’span lang=EN-US style=’font-size:16px;line-height:150%’o:p /o:p/span/p”;
}
content += “/html”;
byte b[] = content.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument(“WordDocument”, bais);
//輸出文件
String name=”導出知識”;
response.reset();
response.setHeader(“Content-Disposition”,
“attachment;filename=” +
new String( (name + “.doc”).getBytes(),
“iso-8859-1”));
response.setContentType(“application/msword”);
OutputStream ostream = response.getOutputStream();
//輸出文件的話,new一個文件流
//FileOutputStream ostream = new FileOutputStream(path+ fileName);
poifs.writeFilesystem(ostream);
ostream.flush();
ostream.close();
bais.close();
java導出word,默認打開時doc或者docx格式
到控制面板的功能和程序,點擊2003,點擊更改,修復,或者直接點擊2003的安裝程序進行修復也可以,這樣默認就是2003,而docx只有2010才能打開。
java用itext導出word修改後文件變大?
用free spire.doc for java試試,不會出現文件變大這麼多的問題,讀、寫、修改編輯都可以的
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/254223.html