excel poi

 

jar包:

 

poi-3.8-20120326.jar

poi-examples-3.8-20120326.jar

poi-ooxml-3.8-20120326.jar

poi-ooxml-schemas-3.8-20120326.jar

dom4j-1.6.1.jar

stax-api-1.0.1.jar

xmlbeans-2.3.0.jar

commons-logging-1.1.jar

junit-3.8.1.jar

log4j-1.2.13.jar

官方下载地址:http://poi.apache.org/download.html

 

 

 

package com.ying.hss;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ExcelImport {

    public static void main(String[] args) {
        FileInputStream in = null;
        try {
            File file = new File("my.xlsx");
            in = new FileInputStream(file);

            Workbook wb = WorkbookFactory.create(in);

            Sheet sheet = wb.getSheetAt(0);

            for (Row row : sheet) {
                int i = row.getRowNum();
                if (i == 0 || i == 1) {
                    continue;
                }
                for (Cell cell : row) {

                    switch (cell.getCellType()) {
                        case HSSFCell.CELL_TYPE_NUMERIC: // 数字  

                            Object value;
                            if (HSSFDateUtil.isCellDateFormatted(cell)) {

                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                                //  如果是date类型则 ,获取该cell的date值  
                                value = sdf.format(HSSFDateUtil.getJavaDate(cell
                                    .getNumericCellValue()));
                            } else { // 纯数字  
                                value = Double.parseDouble(String.valueOf(cell
                                    .getNumericCellValue()));
                            }
                            System.out.print(value + "   ");
                            break;
                        case HSSFCell.CELL_TYPE_STRING: // 字符串  
                            System.out.print(cell.getStringCellValue() + "   ");
                            break;
                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean  
                            System.out.println(cell.getBooleanCellValue() + "   ");
                            break;
                        case HSSFCell.CELL_TYPE_FORMULA: // 公式  
                            System.out.print(cell.getCellFormula() + "   ");
                            break;
                        case HSSFCell.CELL_TYPE_BLANK: // 空值  
                            System.out.println(" ");
                            break;
                        case HSSFCell.CELL_TYPE_ERROR: // 故障  
                            System.out.println(" ");
                            break;
                        default:
                            System.out.print("未知类型   ");
                            break;
                    }
                }
                System.out.println(" ");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

你可能感兴趣的:(Excel)