81,095
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws Exception{
String excelText = TestPoi.extractTextFromXLS("D:\\desk\\company\\文档\\AAAA.xls");
System.out.println(excelText);
}
private static String extractTextFromXLS(String path)
throws IOException,Exception {
StringBuffer content = new StringBuffer();
try{
InputStream is = new FileInputStream(path);
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());
}
}
}
}
}
}
}
}catch(org.apache.poi.poifs.filesystem.OfficeXmlFileException e){
content.append(extractTextFromXLS2007(path));
}catch(Exception e){
e.printStackTrace();
}
return content.toString();
}
InputStream is = new FileInputStream(path);
HSSFWorkbook workbook = new HSSFWorkbook(is); //创建对Excel工作簿文件的引用
is.close();