excel poi

4187 단어 Excel

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();
                }
            }
        }

    }
}

좋은 웹페이지 즐겨찾기