乔山办公网我们一直在努力
您的位置:乔山办公网 > word文档 > java <em>poi</em> 生成<em>word</em>表格怎

java <em>poi</em> 生成<em>word</em>表格怎

作者:乔山办公网日期:

返回目录:word文档


rt java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class XwpfTUtil {

/*String filePath = "/sta.docx";
InputStream is;
XWPFDocument doc;
Map<String, Object> params = new HashMap<String, Object>();

{
params.put("${name}", "xxx");
params.put("${sex}", "男");
params.put("${political}", "共青团员");
params.put("${place}", "sssss");
params.put("${classes}", "3102");
params.put("${id}", "213123123");
params.put("${qq}", "213123");
params.put("${tel}", "312313213");
params.put("${oldJob}", "sadasd");
params.put("${swap}", "是");
params.put("${first}", "asdasd");
params.put("${second}", "综合7a686964616fe4b893e5b19e332事务部");
params.put("${award}", "asda");
params.put("${achievement}", "完成科协网站的开发");
params.put("${advice}", "没有建议");

有个最简单,不用使用任何 poi 或其他第三方类库的方法生成 excel ,或 word ,

你把一个生成好的 word 例子用文件另存为 xml , docx 之类,然后你再直接用记事本去打开这个 docx ,你就会发现是 xml 格式,

这个时候,你在程序里面就直接用普通代码生成这个 xml 就可以了。
首先我用的技术是 poi

这是代码,一个工具类得调用
public class WordUtil {
/**
* 基于模板文件导出 word 文档,此方法主要是用来处理文档中需要替换的文本内容,对图片和表格无效
*
* @param templatePath
* 模板文件的路径,要求路径中要包含全名,并且模板文件只能是 07 及以上格式,即 docx 的文件
* @param destFilePath
* 导出文件的存放路径,包含文件名,例如,E:/test/小区7a686964616fe4b893e5b19e361公告.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 新生成的地址。

关键代码7a686964616fe4b893e5b19e366如下:
FileInputStream fileInputStream = new FileInputStream( soureFile);
POIFSFileSystem pfs = new POIFSFileSystem( fileInputStream );
HWPFDocument hwpf = new HWPFDocument(pfs);// make a HWPFDocument object

OutputStream output = new FileOutputStream( targetFile );
hwpf.write(output);// write to the target file
output.close();

(2)再word中插入表格。HWPF的情况:
Table tcDataTable = range.insertTableBefore( (short)column , row);//column and row列数和行数
tcDataTable.getRow(i).getCell(j).getParagraph(0).getCharacterRun(0).insertBefore("插入i行j列的内容" );

XWPF的情况:
String outputFile = "D:\\test.doc";

XWPFDocument document = new XWPFDocument();

XWPFTable tableOne = document.createTable();

XWPFTableRow tableOneRowOne = tableOne.getRow(0);
tableOneRowOne.getCell(0).setText("11");
XWPFTableCell cell12 = tableOneRowOne.createCell();
cell12.setText("12");
// tableOneRowOne.addNewTableCell().setText("第1行第2列");
// tableOneRowOne.addNewTableCell().setText("第1行第3列");
// tableOneRowOne.addNewTableCell().setText("第1行第4列");

XWPFTableRow tableOneRowTwo = tableOne.createRow();
tableOneRowTwo.getCell(0).setText("21");
tableOneRowTwo.getCell(1).setText("22");
// tableOneRowTwo.getCell(2).setText("第2行第3列");

XWPFTableRow tableOneRow3 = tableOne.createRow();
tableOneRow3.addNewTableCell().setText("31");
tableOneRow3.addNewTableCell().setText("32");

FileOutputStream fOut;

try {
fOut = new FileOutputStream(outputFile);

document.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}

相关阅读

  • 如何用纯java代码实现<em>word</em>转<em>pdf</em&g

  • 乔山办公网word文档
  • 如果只是文字的话,直接读出来,然后用07的存回去,poi能实现。但是有目录什么的就麻烦了,03的读出来目录是一行代码,就容易出问题我用的poi正好在纠结,最后没办法,用按键精灵
  • <em>java</em> <em>poi</em> XWPFTable

  • 乔山办公网word文档
  • 首先需加载你的SQLCUTE是不能错(指明点就是数据库的连接)创建的方法查询所有的数据:publicListAllObject();得到所有的数据,我们就可以开始了(最好是写在你的Service中)创建表格输出的方
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网