poi 根据模板导出word
ZipFile docxFile = new ZipFile(new File("c:/3.docx")); ZipEntry documentXML = docxFile.getEntry("word/document.xml"); InputStream documentXMLIS = docxFile.getInputStream(documentXML); String s = ""; InputStreamReader reader = new InputStreamReader(documentXMLIS,"UTF-8"); BufferedReader br = new BufferedReader(reader); String str = null; while ((str = br.readLine()) != null) { s = s+str; } s = s.replaceAll("${key}", "替换内容"); System.out.println(s); reader.close(); br.close(); if(true){ //return; } //ZipEntry imgFile = docxFile.getEntry("word/media/image1.png"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); InputStream documentXMLIS1 = docxFile.getInputStream(documentXML); Document doc = dbf.newDocumentBuilder().parse(documentXMLIS1); Element docElement = doc.getDocumentElement(); //assertEquals("w:document", docElement.getTagName()); Element bodyElement = (Element) docElement.getElementsByTagName( "w:body").item(0); //assertEquals("w:body", bodyElement.getTagName()); Element pElement = (Element) bodyElement.getElementsByTagName("w:p") .item(0); //assertEquals("w:p", pElement.getTagName()); Element rElement = (Element) pElement.getElementsByTagName("w:r").item( 0); //assertEquals("w:r", rElement.getTagName()); Element tElement = (Element) rElement.getElementsByTagName("w:t").item( 0); //assertEquals("w:t", tElement.getTagName()); //assertEquals("这是第一个测试文档", tElement.getTextContent()); //tElement.setTextContent("这是第一个用Java写的测试文档"); Transformer t = TransformerFactory.newInstance().newTransformer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); t.transform(new DOMSource(doc), new StreamResult(baos)); ZipOutputStream docxOutFile = new ZipOutputStream(new FileOutputStream( "response.docx")); Enumeration entriesIter = (Enumeration) docxFile .entries(); while (entriesIter.hasMoreElements()) { ZipEntry entry = entriesIter.nextElement(); System.out.println(entry.getName()); if (entry.getName().equals("word/document.xml")) { byte[] data = baos.toByteArray(); docxOutFile.putNextEntry(new ZipEntry(entry.getName())); byte[] datas = s.getBytes("UTF-8"); docxOutFile.write(datas, 0, datas.length); //docxOutFile.write(data, 0, data.length); docxOutFile.closeEntry(); } else if(entry.getName().equals("word/media/image1.png")){ InputStream incoming = new FileInputStream("c:/aaa.jpg"); byte[] data = new byte[incoming.available()]; int readCount = incoming.read(data, 0, data.length); docxOutFile.putNextEntry(new ZipEntry(entry.getName())); docxOutFile.write(data, 0, readCount); docxOutFile.closeEntry(); }else { InputStream incoming = docxFile.getInputStream(entry); byte[] data = new byte[incoming.available()]; int readCount = incoming.read(data, 0, data.length); docxOutFile.putNextEntry(new ZipEntry(entry.getName())); docxOutFile.write(data, 0, readCount); docxOutFile.closeEntry(); } } docxOutFile.close();。
java设计系统时,系统中的打印模板想依照用户上传的word或excel模
一般来说没有这种方式的,都是采用报表控件实现
但是你这种方式也可以实现就是比较考验功底
首先必须对word或者excel进行统一的规范性处理,也就是形成约束,比如版本,比如你的实现机制(例如书签或者变量)的命名等地方
其次就是sql查询数据与你实现机制的绑定,然后在相对应的变量或书签处设值
建议先看看开源报表的原理,实现有多种实现方式,你可以用poi,也可以用一些其他的组件对word进行解析,也可以编写Dephi控件,但是都必须满足以下几点
1、可维护
2、高效
3、对于多行分页的自动处理
4、对于单页排版的处理
5、如果有可能需加入图片或者图表
java poi模板导出word后用微软的word打不开,但是wps可以,跪求大
楼主你好,首先你检测一下你的word是否正常,方法是:点开始,再点运行,输入winword /safe(注意,/前面有一个空格),看看是否能打开word,如果可以,那么word是正常的,没有问题。
那么楼主打不开word可能是通用模板的问题了,通用模板的具体路径为:C:\Documents and Settings\Administrator\Application Data\Microsoft\Templates,把这个文件夹下的内容全部彻底删除,然后再去打开word,它会自动生成一个默认的通用模板,问题应该就能解决了!。
Java POI 如何操作word 格式
1、环境支持 1.1 添加poi支持:包下载地址http://www.apache.org/dyn/closer.cgi/poi/release/ 1.2 POI对Excel文件的读取操作比较方便,POI还提供对Word的DOC格式文件的读取。
但在它的发行版本中没有发布对Word支持的模块,需要另外下载一个POI的扩展的Jar包。下载地址为http://www.ibiblio.org/maven2/org/textmining/tm-extractors/0.4/ 下载extractors-0.4_zip这个文件package com.ray.poi.util;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.textmining.text.extraction.WordExtractor;/** * 读写doc * @author wangzonghao * */public class POIWordUtil { /** * 读入doc * @param doc * @return * @throws Exception */ public static String readDoc(String doc) throws Exception { // 创建输入流读取DOC文件 FileInputStream in = new FileInputStream(new File(doc)); WordExtractor extractor = null; String text = null; // 创建WordExtractor extractor = new WordExtractor(); // 对DOC文件进行提取 text = extractor.extractText(in); return text; } /** * 写出doc * @param path * @param content * @return */ public static boolean writeDoc(String path, String content) { boolean w = false; try { // byte b[] = content.getBytes("ISO-8859-1"); byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); DocumentEntry de = directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(path); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (IOException e) { e.printStackTrace(); } return w; } }测试package com.ray.poi.util;import junit.framework.TestCase;public class POIUtilTest extends TestCase { public void testReadDoc() { try{ String text = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc"); System.out.println(text); }catch(Exception e){ e.printStackTrace(); } } public void testWriteDoc() { String wr; try { wr = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc"); boolean b = POIWordUtil.writeDoc("c:\\demo.doc",wr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}。
Java POI 如何操作word 格式
1、环境支持 1.1 添加poi支持:包下载地址http://www.apache.org/dyn/closer.cgi/poi/release/ 1.2 POI对Excel文件的读取操作比较方便,POI还提供对Word的DOC格式文件的读取。
但在它的发行版本中没有发布对Word支持的模块,需要另外下载一个POI的扩展的Jar包。下载地址为http://www.ibiblio.org/maven2/org/textmining/tm-extractors/0.4/ 下载extractors-0.4_zip这个文件package com.ray.poi.util;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.textmining.text.extraction.WordExtractor;/** * 读写doc * @author wangzonghao * */public class POIWordUtil { /** * 读入doc * @param doc * @return * @throws Exception */ public static String readDoc(String doc) throws Exception { // 创建输入流读取DOC文件 FileInputStream in = new FileInputStream(new File(doc)); WordExtractor extractor = null; String text = null; // 创建WordExtractor extractor = new WordExtractor(); // 对DOC文件进行提取 text = extractor.extractText(in); return text; } /** * 写出doc * @param path * @param content * @return */ public static boolean writeDoc(String path, String content) { boolean w = false; try { // byte b[] = content.getBytes("ISO-8859-1"); byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); DocumentEntry de = directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(path); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (IOException e) { e.printStackTrace(); } return w; } }测试package com.ray.poi.util;import junit.framework.TestCase;public class POIUtilTest extends TestCase { public void testReadDoc() { try{ String text = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc"); System.out.println(text); }catch(Exception e){ e.printStackTrace(); } } public void testWriteDoc() { String wr; try { wr = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc"); boolean b = POIWordUtil.writeDoc("c:\\demo.doc",wr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}。
poi如何通过模板导出excel
共分为六部完成根据模板导出excel操作:
第一步、设置excel模板路径(setSrcPath)
第二步、设置要生成excel文件路径(setDesPath)
第三步、设置模板中哪个Sheet列(setSheetName)
第四步、获取所读取excel模板的对象(getSheet)
第五步、设置数据(分为6种类型数据:setCellStrValue、setCellDateValue、 setCellDoubleValue、setCellBoolValue、setCellCalendarValue、setCellRichTextStrValue)
第六步、完成导出 (exportToNewFile)
转载请注明出处51数据库 » poi模板打印word模板
容嬷嬷快扎她呀