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文件中出现乱码,您有好的方法
保存文件的时候,将格式设置为UTF-8。
给个保存其他文件的类似的例子你:
//file 为你要保存的文件
OutputStream out = new FileOutputStream(file);
//关键的就是这个utf-8
OutputStreamWriter isr=new OutputStreamWriter(out,"UTF-8");
BufferedWriter br=new BufferedWriter(isr);
3.word文档打开是乱码怎么办
1
文档一打开,汉语部分并没有乱码,而编程代码(java代码)乱码了,这是两个使用了不同的编码方式或格式照成的原因,或者是已经损坏了的文档。如图
2
虽然,本人这次遇到的是汉语部分没有乱码,编写的代码部分,字母乱码了,但是原理和出问题的原因都是一样的,汉语没有乱码证明他的编码方式和word相一致而已,下面介绍的word乱码解决办法合适所以乱码情况!
END
方法一(删除内容格式法)
1
首先,乱码的原因就是因为内容的编码格式和word规定的不一样!这一点是清楚的,万变不离其宗。所以,不管字母乱码还是汉字乱码,采用“删除内容格式法”,都可以解决乱码的现象!
第一步:打开【文件】——【选项】
2
在选项卡设置里边,找到【高级】——取消对“【使用智能段落范围选择】”复选框的勾选,然后单击【确定】按钮,目的是为了修复文件,下面修复文件!
3
第二步:全选乱码文件——复制(将所有的乱码内容复制到新的文档里边,为了粘贴的时候去除格式)
4
第三步:新建一个文档 【文件】——【新建】——【空白文档】
5
第四步:粘贴复制内容到新建好的空白文档!这里必须注意:粘贴后,选择【仅保留文本】,这样所有文字的格式将会被移除,如图
6
选择“仅保留文本”后,保存文档,文档格式已经被删除了,乱码就被解决了,清除文档格式后排序会有些变化,不过也是很整洁的啦!结果如图
方法二(转换文档格式法)
1另一种解决方法是转换文档格式法,也很快捷方便,解决中心思想:将乱码文档或者是已经损坏了的文档转换为txt格式后,再转为word的doc或docx专用格式!
2步骤:【文件】——【另存为】将乱码文件转存为txt格式。
保存txt时的设置windows默认,按下图设置后选择保存即可!最终我们即可得到想要的文档内容了。
3文档的乱码已经解决了!这归根揭底也是删除格式的方法,也很快的解决!要想获得doc文件,复制到word里边,或者保存为doc格式或者docx格式即可!一步即可解决了!
4.如何解决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(); } }。
5.java写文件出现乱码,怎么解决
--------下面是其中一种编码格式转换工具--------//把以iso编码转换为字符串内码 public static String ISOToInnerCode(String str) { if (str == null) return null; try { str = (new String(str.getBytes("ISO-8859-1"), "UTF-8")).trim(); } catch (Exception ex) { } return str; }---------------------------------------------/charlesyy。
6.如何解决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(); } }。
转载请注明出处51数据库 » java解析word乱码
M哒M