POI处理Word、Excel、PowerPoint
2009-02-06 13:28
381 查看
第一:下载POI,在http://jakarta.apache.org/poi/中,下载poi-bin-3.5-beta4-20081128.zip,解压后把jar包引入项目工程。
第二:处理Word(Word.java)
第三:处理Excel(Excel.java)
第四:处理PowerPoint(PowerPoint.java)
第二:处理Word(Word.java)
import org.apache.poi.hwpf.extractor.WordExtractor; import java.io.File; import java.io.InputStream; public class Word { public static void main(String[] args) throws Exception { System.out.println(getContent("c://11.doc")); } public static String getContent(String s) throws Exception { return getContent(new java.io.FileInputStream(s)); } public static String getContent(File f) throws Exception { return getContent(new java.io.FileInputStream(f)); } public static String getContent(InputStream is) throws Exception { String bodyText = null; WordExtractor ex = new WordExtractor(is); bodyText = ex.getText(); return bodyText; } } |
import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import java.io.File; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; public class Excel { public static void main(String[] args) throws Exception { System.out.println(getContent("c://22.xls")); } public static String getContent(String s) throws Exception { return getContent(new java.io.FileInputStream(s)); } public static String getContent(File f) throws Exception { return getContent(new java.io.FileInputStream(f)); } public static String getContent(InputStream is) throws Exception { StringBuffer content = new StringBuffer(); HSSFWorkbook workbook = new HSSFWorkbook(is); for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) { HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet content.append("/n"); if (null == aSheet) { continue; } for (int rowNum = 0; rowNum <= aSheet.getLastRowNum(); rowNum++) { content.append("/n"); HSSFRow aRow = aSheet.getRow(rowNum); if (null == aRow) { continue; } for (short cellNum = 0; cellNum <= aRow.getLastCellNum(); cellNum++) { HSSFCell aCell = aRow.getCell(cellNum); if (null == aCell) { continue; } if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { content.append(aCell.getRichStringCellValue() .getString()); } else if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { boolean b = HSSFDateUtil.isCellDateFormatted(aCell); if (b) { Date date = aCell.getDateCellValue(); SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd"); content.append(df.format(date)); } } } } } return content.toString(); } } |
import java.io.File; import java.io.InputStream; import org.apache.poi.hslf.HSLFSlideShow; import org.apache.poi.hslf.model.TextRun; import org.apache.poi.hslf.model.Slide; import org.apache.poi.hslf.usermodel.SlideShow; public class PowerPoint { public static void main(String[] args) throws Exception { System.out.println(getContent("c://33.ppt")); } public static String getContent(String s) throws Exception { return getContent(new java.io.FileInputStream(s)); } public static String getContent(File f) throws Exception { return getContent(new java.io.FileInputStream(f)); } public static String getContent(InputStream is) throws Exception { StringBuffer content = new StringBuffer(""); SlideShow ss = new SlideShow(new HSLFSlideShow(is)); Slide[] slides = ss.getSlides(); for (int i = 0; i < slides.length; i++) { TextRun[] t = slides[i].getTextRuns(); for (int j = 0; j < t.length; j++) { content.append(t[j].getText()); } content.append(slides[i].getTitle()); } return content.toString(); } } |
相关文章推荐
- 使用POI来处理Excel和Word文件格式
- POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解
- POI:全面了解POI操作Microsoft Office(Word、Excel、PowerPoint)
- POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解
- 使用POI来处理Excel和Word文件格式(POI的介绍)
- POI 创建,读取excel。处理word文档
- 简介POI处理Excel和Word文件格式
- 7.3 使用POI来处理Excel和Word文件格式
- JAVA POI处理WORD
- java使用poi解析或处理excel的时候,防止数字变成科学计数法的方法
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,html 格式
- Java-POI操作excel遇到文本字符问题处理
- java实现在线预览--poi实现word、excel、ppt转html
- poi3.9之读写2010 word/excel/ppt
- 使用ASP.NET的GridView导出/儲存資料至Excel,Word,Txt檔,Powerpoint
- poi 处理excel 小数问题 整数不保留小数位 整数多.0
- 如何使用POI处理Excel中的日期数据类型
- JAVA写EXCEL多于60000条的处理(POI)
- POI处理Excel中的日期数据类型
- POI使用SAX处理大量数据的xlsx格式的Excel文件