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
- python EXCEL處理PythonExcel
- Apache POI 建立 ExcelApacheExcel
- Excel 資料處理Excel
- POI生成EXCEL檔案Excel
- poi解析Excel內容Excel
- poi的excel匯出Excel
- java使用poi生成excelJavaExcel
- [PY] Word 處理, 技術選型, Word 轉 PDF
- Python資料處理(二):處理 Excel 資料PythonExcel
- Java架構-Apache POI ExcelJava架構ApacheExcel
- springboot poi匯出excel表格Spring BootExcel
- Java操作Excel:POI和EasyExcelJavaExcel
- 使用POI讀寫word docx檔案
- Python中用OpenPyXL處理Excel表格PythonExcel
- 使用Excel高效處理資料Excel
- Excel縮排層級處理Excel
- poi 匯出Excel java程式碼ExcelJava
- Java POI匯入Excel檔案JavaExcel
- POI匯入Excel中文API文件ExcelAPI
- POI 分批讀取Excel資料Excel
- POI操作Excel文件-中級篇Excel
- POI設定excel單元格Excel
- 記一次vba+word+excel+powerbi處理問卷調研結果的經歷Excel
- 使用Apache POI 處理Miscrosoft Office各種格式檔案ApacheROS
- python-docx處理Word必備工具Python
- maatwebsite/excel3.1 匯入excel 公式怎麼處理WebExcel公式
- Python Excel處理庫openpyxl詳解PythonExcel
- Excel VBA 利用FileSystemObject處理檔案ExcelObject
- Python使用xlrd處理excel資料PythonExcel
- EXCEL,POI,EASYEXCEL的使用和比較Excel
- POI匯出excel檔案加水印Excel
- Springboot操作Poi進行Excel匯入Spring BootExcel
- word放不下excel表格怎麼辦 word放不下excel表格的方法Excel
- 深入解析C#中的第三方庫NPOI:Excel和Word檔案處理的利器C#Excel
- 【Python自動化Excel】pandas處理Excel的“分分合合”PythonExcel
- Python自動化處理Excel資料PythonExcel
- Spring Batch + JPA 處理 Excel 檔案教程SpringBATExcel
- 利用poi將Html中table轉為ExcelHTMLExcel