자바 분석 워드 2007 과 엑셀 2007
package com.test;
/**
* jar :
* poi-3.0.2-FINAL-20080204.jar
* poi-contrib-3.0.2-FINAL-20080204.jar
* poi-scratchpad-3.0.2-FINAL-20080204.jar
* poi-3.5-beta6-20090622.jar
* geronimo-stax-api_1.0_spec-1.0.jar
* ooxml-schemas-1.0.jar
* openxml4j-bin-beta.jar
* poi-ooxml-3.5-beta6-20090622.jar
* xmlbeans-2.3.0.jar
* dom4j-1.6.1.jar
*/
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;
public class WordAndExcelExtractor {
public static void main(String[] args){
try{
String wordFile = "D:/ .docx";
String wordText2007 = WordAndExcelExtractor.extractTextFromDOC2007(wordFile);
System.out.println("wordText2007======="+wordText2007);
InputStream is = new FileInputStream("D:/XXX .xls");
String excelText = WordAndExcelExtractor.extractTextFromXLS(is);
System.out.println("text2003==========" + excelText);
String excelFile = "D:/Hello2007.xlsx";
String excelText2007 = WordAndExcelExtractor.extractTextFromXLS2007(excelFile);
System.out.println("excelText2007==========" + excelText2007);
}catch(Exception e ){
e.printStackTrace();
}
}
/**
* @Method: extractTextFromDOCX
* @Description: word 2003
*
* @param
* @return String
* @throws
*/
public static String extractTextFromDOC(InputStream is) throws IOException {
WordExtractor ex = new WordExtractor(is); //is WORD InputStream
return ex.getText();
}
/**
* @Method: extractTextFromDOCX
* @Description: word 2007
*
* @param
* @return String
* @throws
*/
public static String extractTextFromDOC2007(String fileName) throws IOException, OpenXML4JException, XmlException {
OPCPackage opcPackage = POIXMLDocument.openPackage(fileName);
POIXMLTextExtractor ex = new XWPFWordExtractor(opcPackage);
return ex.getText();
}
/**
* @Method: extractTextFromXLS
* @Description: excel 2003
*
* @param
* @return String
* @throws
*/
@SuppressWarnings("deprecation")
private static String extractTextFromXLS(InputStream is)
throws IOException {
StringBuffer content = new StringBuffer();
HSSFWorkbook workbook = new HSSFWorkbook(is); // Excel
for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets); // sheet
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //
for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow); //
if(aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
content.append(aCell.getNumericCellValue());
}else if(aCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
content.append(aCell.getBooleanCellValue());
}else {
content.append(aCell.getStringCellValue());
}
}
}
}
}
}
}
return content.toString();
}
/**
* @Method: extractTextFromXLS2007
* @Description: excel 2007
*
* @param
* @return String
* @throws
*/
private static String extractTextFromXLS2007(String fileName) throws Exception{
StringBuffer content = new StringBuffer();
// XSSFWorkbook ,strPath
XSSFWorkbook xwb = new XSSFWorkbook(fileName);
// Sheet
for(int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++){
XSSFSheet xSheet = xwb.getSheetAt(numSheet);
if(xSheet == null){
continue;
}
// Row
for(int rowNum = 0; rowNum <= xSheet.getLastRowNum(); rowNum++){
XSSFRow xRow = xSheet.getRow(rowNum);
if(xRow == null){
continue;
}
// Cell
for(int cellNum = 0; cellNum <= xRow.getLastCellNum(); cellNum++){
XSSFCell xCell = xRow.getCell(cellNum);
if(xCell == null){
continue;
}
if(xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN){
content.append(xCell.getBooleanCellValue());
}else if(xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC){
content.append(xCell.getNumericCellValue());
}else{
content.append(xCell.getStringCellValue());
}
}
}
}
return content.toString();
}
}
http://archive.cnblogs.com/a/1759383/
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.