如何使用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();//提取批注内容 }
如何用Apache POI读取Excel
首先POI是开源组织Apache出品的一个开源jar包,提供了方便解析Excel的API,我们可以非常方便的使用它来读取Excel。
这里介绍3.5Final版本。
所需用到的jar包如下: 说到Excel,有2003和2007,格式是不一样的,用POI解析的方法也就不一样,Excel2003主要是使用org.apache.poi.hssf.usermodel包中的类来解析,而Excel2007就是使用org.apache.poi.xssf.usermodel来解析。
解析Excel2003源码说到Excel,有2003和2007,格式是不一样的,用POI解析的方法也就不一样,Excel2003主要是使用org.apache.poi.hssf.usermodel包中的类来解析,而Excel2007就是使用org.apache.poi.xssf.usermodel来解析。
解析Excel2003源码StringBuffer content = new StringBuffer(); HSSFWorkbook workbook = new HSSFWorkbook(is); // 创建对Excel工作簿文件的引用 for (int numSheets = 0; numSheets = firstC) { if(cell.getRowIndex() = firstR) { return true; } } } return false; } public static String getMergedRegionValue(HSSFSheet sheet, HSSFCell cell) { //获得一个 sheet 中合并单元格的数量 int sheetmergerCount = sheet.getNumMergedRegions(); //便利合并单元格 for(int i = 0; i = firstC) { if(cell.getRowIndex() = firstR) { HSSFRow fRow = sheet.getRow(firstR); HSSFCell fCell = fRow.getCell(firstC); //除了合并单元格首单元格的值, 其余的用(*)来区分 if (fCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC && hasBorder(cell)) { return String.valueOf(fCell.getNumericCellValue()); } else if (fCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN && hasBorder(cell)) { return String.valueOf(fCell.getBooleanCellValue()); } else if (fCell.getCellType() == HSSFCell.CELL_TYPE_STRING && hasBorder(cell)) { return fCell.getStringCellValue(); } else if (fCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA && hasBorder(cell)){ return String.valueOf(fCell.getCellFormula()); } } } } return ""; }
如何用Apache POI操作Excel文件
写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(); }
怎么用java合并多个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();//提取批注内容 }
java poi 要导哪些包
翻译结果:"对“一. 基本概念Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
二. 基本功能结构:HSSF - 提供读写Microsoft Excel格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
HWPF - 提供读写Microsoft Word格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读写Microsoft Visio格式档案的功能。
三. 范文演示创建Excel 文档示例1将演示如何利用Jakarta POI API 创建Excel 文档。
示例1程序如下:读取Excel文档中的数据示例2将演示如何读取Excel文档中的数据。
假定在D盘JTest目录下有一个文件名为test1.xls的Excel文件。
示例2程序如下:设置单元格格式在这里,我们将只介绍一些和格式设置有关的语句,我们假定workbook就是对一个工作簿的引用。
在Java中,第一步要做的就是创建和设置字体和单元格的格式,然后再应用这些格式:1、创建字体,设置其为红色、粗体:2、创建格式3、应用格式处理WORD文档
Apache的POI,可以利用POI(java)生成word中柱状图、饼图之类的图表...
展开全部 “poi”是(Program of Instruction )教学大纲的意思。
“poi”释义①POI是“Point of Interest”的缩写,可以翻译成“兴趣点”,也有些叫做“Point of Information”,即“信息点”。
电子地图上一般用气泡图标来表示POI,像电子地图上的景点、政府机构、公司、商场、饭馆等,都是POI。
②POI是基于位置服务的最核心数据,在电子地图上运用场景广泛, 如导航前选择的目的地、查看周边的餐馆等。
例句:①The application and importance of POI in logistics monitoring system are analyzed. 本文分析了POI数据在物流监控系统中的应用特点和重要性。
②Support for persistence of Java objects to Excel via JDO/ JPA APIs, utilizing Apache POI. 支持通过JDO/JPA APIs将Java对象持久化到Excel中,这利用了Apache POI。
③This article demonstrates how to use Java technology and the Apache POI to read from Employee_List.xls. 本文演示如何使用Java技术和Apache POI来读取Employee List.xls。
④This article uses the Apache POI because of its support community and rich functionality. 由于Apache POI的支持社区和丰富功能,本文使用Apache POI。
⑤Apache POI is a set of Java APIs for working with both older and newer Microsoft standard documents. Apache POI是一组用于处理旧版和新版Microsoft标准文档的Java API。
...
Java poi读取doc文档出错
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()); }
转载请注明出处51数据库 » apache poi读写word
静夜_思