使用poi解析Excel

解析XLS

package xls;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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; public class XlsMain { public static void main( String[] args) throws IOException {
XlsMain xlsMain = new XlsMain(); xlsMain.readXls();
} private void readXls() throws IOException{
InputStream is = new FileInputStream( "D:\\excel\\xls_test2.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook( is); // 循环工作表Sheet
for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt( numSheet);
if(hssfSheet == null){
continue;
} // 循环行Row
for(int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++){
HSSFRow hssfRow = hssfSheet.getRow( rowNum);
if(hssfRow == null){
continue;
} // 循环列Cell
for(int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++){
HSSFCell hssfCell = hssfRow.getCell( cellNum);
if(hssfCell == null){
continue;
} System.out.print(" " + getValue( hssfCell));
}
System.out.println();
}
}
} @SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell){
if(hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN){
return String.valueOf( hssfCell.getBooleanCellValue());
}else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC){
return String.valueOf( hssfCell.getNumericCellValue());
}else{
return String.valueOf( hssfCell.getStringCellValue());
}
} }

解析XLSX

package xlsx;

import java.io.IOException;
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; public class XlsxMain { public static void main( String[] args) throws IOException {
XlsxMain xlsxMain = new XlsxMain(); xlsxMain.readXlsx();
} private void readXlsx() throws IOException{
String fileName = "D:\\excel\\xlsx_test.xlsx";
XSSFWorkbook xssfWorkbook = new XSSFWorkbook( fileName); // 循环工作表Sheet
for(int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++){
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt( numSheet);
if(xssfSheet == null){
continue;
} // 循环行Row
for(int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++ ){
XSSFRow xssfRow = xssfSheet.getRow( rowNum);
if(xssfRow == null){
continue;
} // 循环列Cell
for(int cellNum = 0; cellNum <= xssfRow.getLastCellNum(); cellNum++){
XSSFCell xssfCell = xssfRow.getCell( cellNum);
if(xssfCell == null){
continue;
}
System.out.print(" "+getValue(xssfCell));
}
System.out.println();
}
}
} @SuppressWarnings("static-access")
private String getValue(XSSFCell xssfCell){
if(xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN){
return String.valueOf( xssfCell.getBooleanCellValue());
}else if(xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC){
return String.valueOf( xssfCell.getNumericCellValue());
}else{
return String.valueOf( xssfCell.getStringCellValue());
}
} }

转自:http://hao0610.iteye.com/blog/1160678

上一篇:用GoEasy推送实现Java实时推送


下一篇:动态添加Redis密码认证