1.JAVA使用POI读写word 乱码
如何使用java、poi读写word文档??能不能将一个word的内容完全读过来,放到一个新生成的word文件中去,要求能将word中的表格、图片等保留,格式不变。
最好能给个例子?网上多是很早以前的那个解决方法如下:,只能读文本内容,且新生成的word文件打开时总是要提示选择编码,不太好用,希望能有新的解决方案??!!poi操作word1.1 添加poi支持:包下载地址1.2 poi对excel文件的读取操作比较方便,poi还提供对word的doc格式文件的读取。但在它的发行版本中没有发布对word支持的模块,需要另外下载一个poi的扩展的jar包。
下载地址为;下载extractors-0.4_zip这个文件2、提取doc文件内容 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; } public static void main(string[] args) { try{ string text = wordreader.readdoc("c:/test.doc"); system.out.println(text); }catch(exception e){ e.printstacktrace(); } }3、写入doc文档 import java.io.bytearrayinputstream; 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; public class wordwriter { 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; } public static void main(string[] args) throws exception{ string wr=wordreader.readdoc("d:\\test.doc"); boolean b = writedoc("d:\\result.doc",wr);。
2.Java操作word里的表格问题,高分在线等
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ExportDocImpl
{
public void testWord(){
try{
FileInputStream in = new FileInputStream("D:\\sinye.doc");//载入文档
POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(pfs);
Range range = hwpf.getRange();//得到文档的读取范围
TableIterator it = new TableIterator(range);
//迭代文档中的表格
while (it.hasNext()) {
Table tb = (Table) it.next();
//迭代行,默认从0开始
for (int i = 0; i
3.如何解决POI生成WORD中文乱码问题
[C:\\doc.doc]内容:我是$[name];Word.java-writeWord方法: Java代码 public void writeWord(HttpServletResponse response) { String URL = "C:\\doc.doc"; File file = new File(URL); try { FileInputStream in = new FileInputStream(file); POIFSFileSystem pfs = new POIFSFileSystem(in); HWPFDocument hwpf = new HWPFDocument(pfs); Range range = hwpf.getRange(); String str = range.text(); str = str.replace("$[name]", "黄锋"); System.out.println(str); response.setCharacterEncoding("GB2312"); response.setContentType("application/ms-word"); response.setHeader("Content-disposition", "attachment; filename=1.doc"); PrintWriter out = response.getWriter(); out.println(str); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void writeWord(HttpServletResponse response) { String URL = "C:\\doc.doc"; File file = new File(URL); try { FileInputStream in = new FileInputStream(file); POIFSFileSystem pfs = new POIFSFileSystem(in); HWPFDocument hwpf = new HWPFDocument(pfs); Range range = hwpf.getRange(); String str = range.text(); str = str.replace("$[name]", "黄锋"); System.out.println(str); response.setCharacterEncoding("GB2312"); response.setContentType("application/ms-word"); response.setHeader("Content-disposition", "attachment; filename=1.doc"); PrintWriter out = response.getWriter(); out.println(str); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }。
4.我想用POI在Word里插入一张图片,这是我的测试代码
public void addPicture(string filepath, string oldfileName, uint id) { System.IO.FileStream gfs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read); XWPFParagraph para = doc.CreateParagraph(); XWPFRun r = para.CreateRun(); XWPFPicture pic = r.AddPicture(gfs, (int)NPOI.XWPF.UserModel.PictureType.JPEG, oldfileName, 1000000, 1000000); NPOI.OpenXmlFormats.Dml.WordProcessing.CT_Inline inline = r.GetCTR().GetDrawingList()[0].inline[0]; inline.docPr.id = id;//默认插入少了这个ID号,它就是图片的序号,从 1开始编号的。
这个是我折腾了一整天弄才弄好的。 gfs.Close(); }。
5.如何样能让poi读取的word按原来的格式显示在页面
怎么样能让poi读取的word按原来的格式显示在页面
因为poi读取word 没法读取到空格和回车.这个问题要如何解决呢
poi java
------解决方案--------------------
public static void main(String[] args) {
File file = new File("D:/test.doc");
try {
FileInputStream fis = new FileInputStream(file);
HWPFDocument hwpfd = new HWPFDocument(fis);
WordExtractor wordExtractor = new WordExtractor(hwpfd);
String[] paragraph = wordExtractor.getParagraphText();
for (int i = 0; i
6.poi的word转html,怎么显示修订内容的最终状态
实现代码如下:public class Word2Html { public static void main(String argv[]) { try { //word 路径 html输出路径 convert2Html("D:/doctohtml/1.doc","D:/doctohtml/1.html"); } catch (Exception e) { e.printStackTrace(); } } public static void writeFile(String content, String path) { FileOutputStream fos = null; BufferedWriter bw = null; try { File file = new File(path); fos = new FileOutputStream(file); bw = new BufferedWriter(new OutputStreamWriter(fos,"utf-8")); bw.write(content); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (bw != null) bw.close(); if (fos != null) fos.close(); } catch (IOException ie) { } } } public static void convert2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException { HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile)); WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( DocumentBuilderFactory.newInstance().newDocumentBuilder() .newDocument()); wordToHtmlConverter.setPicturesManager( new PicturesManager() { public String savePicture( byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches ) { //html 中 图片标签中 显示的图片路路径 return "d:/doctohtml/"+suggestedName; } } ); wordToHtmlConverter.processDocument(wordDocument); //save pictures List pics=wordDocument.getPicturesTable().getAllPictures(); if(pics!=null){ for(int i=0;iword中图片的存储路径 pic.writeImageContent(new FileOutputStream("D:/doctohtml/" + pic.suggestFullFileName())); } catch (FileNotFoundException e) { e.printStackTrace(); } } } Document htmlDocument = wordToHtmlConverter.getDocument(); ByteArrayOutputStream out = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(htmlDocument); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "html"); serializer.transform(domSource, streamResult); out.close(); writeFile(new String(out.toByteArray()), outPutFile); }}。
转载请注明出处51数据库 » poiwordnewpage事件