我有jacbo操作word的大多基本方法。你留下邮箱我发给你。更多
yu_me@163.com 谢谢~~~
已发
已收到~谢谢了~不过还是没看到有任何关于读取的操作。。。
有的啊。。。
/**
* 打开一个已存在的文档
*
* @param docPath
*/
public void openDocument(String docPath) {
closeDocument();
doc = Dispatch.call(documents, "Open", docPath).toDispatch();
selection = Dispatch.get(word, "Selection").toDispatch();
}
这个不就是么。。
全局量的doc不就是word的对象么? 根据这个doc调用方法来取得里面的数据
java 调用word模板实现循环套打生成word文档
1,模版里做循环,需要循环的地方 在模版里加入 <#list reportListas a > </#list> 编辑好。
2,代码里
Map<String,Object> resMap = new HashMap<>();
resMap.put("reportList", list);
t.process(resMap,out);
请教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!=records&&0!=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(有个word模板,填充数据)怎么做,最好有代码啊
这个你可以试试一些控件 比如iWebOffice2009 他可以根据自定义的书签 自己定义一套模板 如果加载的时候 填充模板 给模板中书签赋值 就可以 了
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, Map<String, String> data) {
FileOutputStream out = null;
XWPFDocument doc = null;
try {
doc = new XWPFDocument(POIXMLDocument.openPackage(templatePath));
List<XWPFRun> listRun;
List<XWPFParagraph> 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, Map<String, 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 {
Map<String, String> maps = new HashMap<String, 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 新生成的地址。
阿帕奇的这个支持导PDF格式的吗
转载请注明出处51数据库 » javaword按模板导出 java怎么导入word模板