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();修改后的文本框
如何使用POI操作Word文本框中的内容
mport org.apache.poi.POITextExtractor;import org.apache.poi.hwpf.extractor.WordExtractor;org.apache.poi.hwpf.extractor.WordExtractor doc = new WordExtractor(new FileInputStream(filePath));String text = doc.getText();
如何使用apache poi将word转化为html
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();//提取批注内容 }
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()两个重载方法。
poi 操作word 2007 (如何删除word中的某一个表格)
关键代码如下:FileInputStream fileInputStream = new FileInputStream( soureFile);POIFSFileSystem pfs = new POIFSFileSystem( fileInputStream );HWPFDocument hwpf = new HWPFDocument(pfs);// make a HWPFDocument objectOutputStream output = new FileOutputStream( targetFile );hwpf.write(output);// write to the target fileoutput.close();(2)再word中插入表格。
HWPF的情况:Table tcDataTable = range.insertTableBefore( (short)column , row);//column and row列数和行数tcDataTable.getRow(i).getCell(j).getParagraph(0).getCharacterRun(0).insertBefore("插入i行j列的内容" );XWPF的情况:String outputFile = "D:\\test.doc";XWPFDocument document = new XWPFDocument();XWPFTable tableOne = document.createTable();XWPFTableRow tableOneRowOne = tableOne.getRow(0);tableOneRowOne.getCell(0).setText("11");XWPFTableCell cell12 = tableOneRowOne.createCell();cell12.setText("12");// tableOneRowOne.addNewTableCell().setText("第1行第2列");// tableOneRowOne.addNewTableCell().setText("第1行第3列");// tableOneRowOne.addNewTableCell().setText("第1行第4列");XWPFTableRow tableOneRowTwo = tableOne.createRow();tableOneRowTwo.getCell(0).setText("21");tableOneRowTwo.getCell(1).setText("22");// tableOneRowTwo.getCell(2).setText("第2行第3列");XWPFTableRow tableOneRow3 = tableOne.createRow();tableOneRow3.addNewTableCell().setText("31");tableOneRow3.addNewTableCell().setText("32");FileOutputStream fOut;try {fOut = new FileOutputStream(outputFile);document.write(fOut); fOut.flush();// 操作结束,关闭文件fOut.close();} catch (Exception e) {e.printStackTrace();}
POI操作word,怎么设置换行
写public static void main(String args[]) throws Exception { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p1 = doc.createParagraph(); p1.setAlignment(ParagraphAlignment.CENTER); p1.setBorderBottom(Borders.DOUBLE); p1.setBorderTop(Borders.DOUBLE); p1.setBorderRight(Borders.DOUBLE); p1.setBorderLeft(Borders.DOUBLE); p1.setBorderBetween(Borders.SINGLE); p1.setVerticalAlignment(TextAlignment.TOP); XWPFRun r1 = p1.createRun(); r1.setBold(true); r1.setText("The quick brown fox"); r1.setBold(true); r1.setFontFamily("Courier"); r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH); r1.setTextPosition(100); XWPFParagraph p2 = doc.createParagraph(); p2.setAlignment(ParagraphAlignment.RIGHT); p2.setBorderBottom(Borders.DOUBLE); p2.setBorderTop(Borders.DOUBLE); p2.setBorderRight(Borders.DOUBLE); p2.setBorderLeft(Borders.DOUBLE); p2.setBorderBetween(Borders.SINGLE); XWPFRun r2 = p2.createRun(); r2.setText("jumped over the lazy dog"); r2.setStrike(true); r2.setFontSize(20); XWPFRun r3 = p2.createRun(); r3.setText("and went away"); r3.setStrike(true); r3.setFontSize(20); r3.setSubscript(VerticalAlign.SUPERSCRIPT); XWPFParagraph p3 = doc.createParagraph(); p3.setWordWrap(true); p3.setPageBreak(true); p3.setAlignment(ParagraphAlignment.BOTH); p3.setSpacingLineRule(LineSpacingRule.EXACT); p3.setIndentationFirstLine(600); XWPFRun r4 = p3.createRun(); r4.setTextPosition(20); r4.setText("To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? To die: to sleep; "); r4.addBreak(BreakType.PAGE); r4.setText("No more; and by a sleep to say we end The heart-ache and the thousand natural shocks That flesh is heir to, 'tis a consummation Devoutly to be wish'd. To die, to sleep; To sleep: perchance to dream: ay, there's the rub; ......."); r4.setItalic(true); XWPFRun r5 = p3.createRun(); r5.setTextPosition(-10); r5.setText("For in that sleep of death what dreams may come"); r5.addCarriageReturn(); r5.setText("When we have shuffled off this mortal coil,Must give us pause: there's the respectThat makes calamity of so long life;"); r5.addBreak(); r5.setText("For who would bear the whips and scorns of time,The oppressor's wrong, the proud man's contumely,"); r5.addBreak(BreakClear.ALL); r5.setText("The pangs of despised love, the law's delay,The insolence of office and the spurns......."); FileOutputStream out = new FileOutputStream("simple.docx"); doc.write(out); out.close(); }
如何用Apache POI操作Excel文件
展开全部 POI是Apache下的一个项目,是用Java编写的开源框架,提供API供开发者直接操作Microsoft Office(Excel,Word,PowerPoint...) POI为我们带来了什么? 在很多的企业当中,储蓄数据是使用Excel文档的,因为Excel文档的格式方便,也能套用公式,而企业程序是存储在数据库当中,这样就需要一种两者之间互相转换的方法,当企业刚开始使用信息化的管理系统时,也需要将Excel的数据录入到程序当中,这种需求是非常普遍的. POI使用: 首先增加Maven的依赖 org.apache.poi poi 3.8 org.apache.poi poi-ooxml 3.8 org.apache.poi poi-ooxml-schemas 3.8 org.apache.poi poi-scratchpad 3.8以下为操作Excel的测试类 package com.accentrix.ray;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.usermodel.WorkbookFactory;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.junit.Before;import org.junit.Test;public class TestExcel { private Workbook workbook;/* * 由于Excel当中的单元格Cell存在类型,若获取类型错误 就会产生错误, * 所以通过此方法将Cell内容全部转换为String类型 */ private String getCellValue(Cell cell) { String str = null; switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK: str = ""; break; case Cell.CELL_TYPE_BOOLEAN: str = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: str = String.valueOf(cell.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: str = String.valueOf(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: str = String.valueOf(cell.getStringCellValue()); break; default: str = null; break; } return str; }@Before public void setUp() throws InvalidFormatException, IOException { // 加载excel文件,自动判断是HSSF还是XSSF workbook = WorkbookFactory.create(new File("E:/aaa.xls")); } /* * 读取一个已存在的Excel */ @Test public void testReadExcel() throws InvalidFormatException, IOException { // 获取第一个工作目录,下标从0开始 Sheet sheet = workbook.getSheetAt(0); // 获取该工作目录最后一行的行数 int lastRowNum = sheet.getLastRowNum(); for (int i = 0; i < lastRowNum; i++) { // 获取下标为i的行 Row row = sheet.getRow(i); // 获取该行单元格个数 int lastCellNum = row.getLastCellNum(); for (int j = 0; j < lastCellNum; j++) { // 获取下标为j的单元格 Cell cell = row.getCell(j); // 调用获取方法 String cellValue = this.getCellValue(cell); } } } /* * 使用Foreach方式读取Excel */ @Test public void testForeachReadExcel() { // 根据sheet的名字获取 Sheet sheet = workbook.getSheet("test"); // 处了上面testReadExcel的方式读取以外,还支持foreach的方式读取 for (Row row : sheet) { for (Cell cell : row) { String cellValue = this.getCellValue(cell); System.out.println(cellValue); } } } /* * 创建简单的Excel */ @Test public void testWriteExcel() throws IOException { // 创建一个XSSF的Excel文件 workbook = new XSSFWorkbook(); FileOutputStream fos = new FileOutputStream("E:/test.xlsx"); // 创建名称为test的工作目录 Sheet sheet = workbook.createSheet("test"); /* * 创建1个10行x10列的工作目录 */ for (int i = 0; i < 10; i++) { // 创建一行 Row row = sheet.createRow(i); for (int j = 0; j < 10; j++) { // 创建一个单元格 Cell cell = row.createCell(j); // 设置单元格value cell.setCellValue("test"); // 此处为设置Excel的样式,设置单元格内容居中, // 但这样设置方式并不常用,请留意下面的方法 CellStyle cs = workbook.createCellStyle(); cs.setAlignment(CellStyle.ALIGN_CENTER); cell.setCellStyle(cs); } } // 将Excel写出到文件流 workbook.write(fos); }
转载请注明出处51数据库 » poi word写操作