java 谁有poi读取本地word然后在替换word的内容如table、图片....的...
The supplied data appears to be a raw XML file. Formats such as Office 2003 XML are not supported换对象来处理~~~~~~~~~~~~~~~~~~~~~~~~~~/** * 仅支持2003 * * @param file * @throws IOException */ private static void readExcel2003(File file) throws IOException { InputStream is = new FileInputStream(file); Workbook rwb = new HSSFWorkbook(is); Sheet sheet = rwb.getSheetAt(0); Row row = sheet.getRow(3); Cell cell = row.getCell(0); System.out.println(cell.getStringCellValue()); } /** * 仅支持2007 * * @param file * @throws IOException */ private static void readExcel2007(File file) throws IOException { InputStream is = new FileInputStream(file); Workbook rwb = new XSSFWorkbook(is); Sheet sheet = rwb.getSheetAt(0); Row row = sheet.getRow(3); Cell cell = row.getCell(0); System.out.println(cell.getStringCellValue()); } /** * 支持2003/2007 * * @param file * @throws Exception */ private static void readExcel(File file) throws Exception { InputStream is = new FileInputStream(file); Workbook rwb = WorkbookFactory.create(is); Sheet sheet = rwb.getSheetAt(0); Row row = sheet.getRow(3); Cell cell = row.getCell(0); System.out.println(cell.getStringCellValue()); }
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();修改后的文本框
java poi XWPFTable操作word表格的问题?
1.下载下载3.8beta4版本,请记得一定要下载该版本,其他版本读取word模板并改写内容生成新的文件后,打开新文件时会提示“word无法读取文档,文档可能损坏。
”2.集成到项目这一步很简单,只要把下载后解压得到的poi-3.8-beta4-20110826.jar和poi-scratchpad-3.8-beta4-20110826.jar两个文件复制到java web项目的lib目录下就行了3.制作word模板把需要变动的值全部用代码来代替,例如你需要改变名称的值,则可以在模板中用name来表示。
详细见附件中的doc文件。
4.调用接口方法实现对word的读写操作整个过程就是先读取模板,然后修改内容,再重新生成新的文档保存到本地或者输出文件流提供下载,下面分别是生成新文档和输出文件流两种方式的代码片断,详细的代码请见下列代码中的readwriteWord()两个重载方法。
java poi 读取excel文件 用wps编辑之后不能读取,只能读取office的
1. 通过流来读取,例如:TextFileForm fileForm = (TextFileForm) form; FormFile formFile = fileForm.getTxtFile(); if (formFile.getFileData().length == 0) { response.setCharacterEncoding("gb2312"); response.getWriter().write(""); } InputStream in = formFile.getInputStream(); WordExtractor extractor = new WordExtractor(); String str = extractor.extractText(in);这段代码就是负责读取word的
java中poi如何将word文档转换成pdf
有个最简单,不用使用任何 poi 或其他第三方类库的方法生成 excel ,或 word ,你把一个生成好的 word 例子用文件另存为 xml , docx 之类,然后你再直接用记事本去打开这个 docx ,你就会发现是 xml 格式,这个时候,你在程序里面就直接用普通代码生成这个 xml 就可以了。
java poi怎么向word文档中插入图片
Java可以使用这个开源框架,对word进行读取合并等操作,Apache POI是一个开源的利用Java读写Excel、WORD等微软OLE2组件文档的项目。
最新的3.5版本有很多改进,加入了对采用OOXML格式的Office 2007支持,如xlsx、docx、pptx文档。
示例如下:import org.apache.poi.POITextExtractor; import org.apache.poi.hwpf.extractor.WordExtractor; //得到.doc文件提取器 org.apache.poi.hwpf.extractor.WordExtractor doc = new WordExtractor(new FileInputStream(filePath)); //提取.doc正文文本 String text = doc.getText(); //提取.doc批注 String[] comments = doc. getCommentsText(); 2007 import org.apache.poi.POITextExtractor; import org.apache.poi.xwpf.extractor.XWPFWordExtractor; import org.apache.poi.xwpf.usermodel.XWPFComment; import org.apache.poi.xwpf.usermodel.XWPFDocument; //得到.docx文件提取器 org.apache.poi.xwpf.extractor.XWPFWordExtractor docx = new XWPFWordExtractor(POIXMLDocument.openPackage(filePath)); //提取.docx正文文本 String text = docx.getText(); //提取.docx批注 org.apache.poi.xwpf.usermodel.XWPFComment[] comments = docx.getDocument()).getComments(); for(XWPFComment comment:comments){ comment.getId();//提取批注Id comment.getAuthor();//提取批注修改人 comment.getText();//提取批注内容 }
转载请注明出处51数据库 » java中poi读取word