java使用POI解析2007以上的Excel表格
Posted 云端观云
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java使用POI解析2007以上的Excel表格相关的知识,希望对你有一定的参考价值。
来自http://hao0610.iteye.com/blog/1160678
使用poi来解析Excel的xls和xlsx。
解析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());
- }
- }
- }
以上是关于java使用POI解析2007以上的Excel表格的主要内容,如果未能解决你的问题,请参考以下文章
POI以SAX方式解析Excel2007大文件(包含空单元格的处理) Java生成CSV文件实例详解
java 用POI 解析word中的表格,POI只能识别word中创建的表格。 如果表格是从Excel中copy过来的, POI无法识