当前位置:知识问问>百科问答>java poi 操作word文档,怎么写入带上下标的文字?

java poi 操作word文档,怎么写入带上下标的文字?

2023-08-20 06:50:52 编辑:join 浏览量:567

java poi 操作word文档,怎么写入带上下标的文字?

1.1 添加poi支持:包下载地址

1.2 POI对Excel文件的读取操作比较方便,POI还提供对Word的DOC格式文件的读取。但在它的发行版本中没有发布对Word支持的模块,需要另外下载一个POI的扩展的Jar包。下载地址为

下载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();

}

}

}

步骤

第一步,使用输入流打开文件,并获得文档的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();

修改后的文本框

标签:java,poi,word

版权声明:文章由 知识问问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.zhshwenwen.com/answer/277479.html
热门文章