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 data) { FileOutputStream out = null; XWPFDocument doc = null; try { doc = new XWPFDocument(POIXMLDocument.openPackage(templatePath)); List listRun; List 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 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 maps = new HashMap(); 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如何向Word文档中添加表格
import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;// 在当前路径(默认)创建3个非空.doc(当然也可以是.txt,.java…)文件public class FileTest{//遇到异常抛给Java虚拟机处理public static void main(String[] args)throws IOException{//i>-1,恒为true,创建无限文件,就成病毒了for (int i = 0; i < 3; i++){//指定要要输入内容的文件名nameString name = "a" + i + ".doc";//定义一个节点输出流FilOutputStream//通俗点:定义一个水管,水管通向name文件FileOutputStream out = new FileOutputStream(name);//使用PrintStream包装该节点流,使用PrintStream来输出字符串//通俗点:给水管加个水龙头(PrintStream),这个水龙头具有放水功能(ps.print())PrintStream ps = new PrintStream(out);ps.print("我我我窝窝窝窝窝窝窝窝哦我");ps.append("你");}}}一切尽在代码里!
java poi 操作word文档,怎么写入带上下标的文字?
步骤第一步,使用输入流打开文件,并获得文档的XWPFDocument对象。
然后获得文档的所有段落,进而获得要操作的文本框所在的段落,具体使用时候,可以通过判断或者print操作得知要操作的文本框到底是哪一段。
FileInputStream fis = newFileInputStream("e:/file.docx");XWPFDocument doc = new XWPFDocument(fis);List paragraphList =doc.getParagraphs();XWPFParagraph paragraph = paragraphList.get(10);文本框在Word中显示第二步,获取XWPFParagraph的XmlObject,然后获得XmlObject对象的游标。
可以通过打印XmlObject来得知当前XML的内容,也可以使用XmlCursor的getName方法和getTextValue方法来查看当前游标所在位置的Node及Node的值。
XmlObject object =paragraph.getCTP().getRArray(1);XmlCursor cursor = object.newCursor();第四步,通过移动游标,找到要修改的文本所在位置,然后使用游标的setTextValue来设置其值。
//修改第一处文本:cursor.toChild(1); cursor.toChild(0);cursor.toChild(3); cursor.toChild(0); cursor.toChild(0); cursor.toChild(3);cursor.toChild(1); cursor.setTextValue("First");// 修改第二处文本cursor.toParent(); cursor.toParent();cursor.toChild(1);cursor.toChild(3); cursor.toChild(1);cursor.setTextValue("Second");第四步,保存文件、关闭输入输出流。
FileOutputStream fos = newFileOutputStream("e:/export.docx");doc.write(fos);fos.flush();fos.close();fis.close();修改后的文本框
转载请注明出处51数据库 » java 写入word表格
段子渝北小哥