POI處理Word、Excel、PowerPoint
第一:下載POI,在http://jakarta.apache.org/poi/中,下載poi-bin-3.5-beta4-20081128.zip,解壓後把jar包引入專案工程。
第二:處理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;
}
}
第三:處理Excel(Excel.java)
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();
}
}
第四:處理PowerPoint(PowerPoint.java)
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();
}
}
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/tim_zhang8888/archive/2009/02/07/3865980.aspx
相關文章
- Apache POI處理Excel文件ApacheExcel
- php生成匯出Word、Excel、PowerPoint外掛PHPExcel
- POI 匯出ExcelExcel
- POI 操作Excel 整理Excel
- Apache POI 操作ExcelApacheExcel
- Apache POI 建立 ExcelApacheExcel
- poi解析Excel內容Excel
- java使用poi生成excelJavaExcel
- POI生成EXCEL檔案Excel
- poi的excel匯出Excel
- python EXCEL處理PythonExcel
- Excel 資料處理Excel
- python處理ExcelPythonExcel
- [PY] Word 處理, 技術選型, Word 轉 PDF
- lucene開發中有關讀取pdf,html,word,rtf,txt,powerpoint,excel等文件的操作HTMLExcel
- 巧將PowerPoint文字轉換Word文件
- Java操作Excel:POI和EasyExcelJavaExcel
- Java架構-Apache POI ExcelJava架構ApacheExcel
- java poi 匯出excel加密JavaExcel加密
- 使用POI讀寫word docx檔案
- 如何批量處理word中的表格
- 如何把Word裡的公式放到PowerPoint裡公式
- 如何將Powerpoint文件轉換為Word文件
- Python資料處理(二):處理 Excel 資料PythonExcel
- Java POI匯入Excel檔案JavaExcel
- POI 分批讀取Excel資料Excel
- POI操作Excel文件-中級篇Excel
- poi 匯出Excel java程式碼ExcelJava
- POI設定excel單元格Excel
- POI匯入Excel中文API文件ExcelAPI
- poi--excel --匯出例項Excel
- POI操作Excel文件-基礎篇Excel
- Java解析OFFICE(word,excel,powerpoint)以及PDF的實現方案及開發中的點滴分享JavaExcel
- Excel縮排層級處理Excel
- 使用Excel高效處理資料Excel
- python處理Excel 之 xlrdPythonExcel
- 使用Java poi編輯word.docx文件Java
- 使用Apache POI 處理Miscrosoft Office各種格式檔案ApacheROS