java 는 poi. 3.10 을 사용 하여 엑셀 2003 (xls 형식) 을 읽 습 니 다.

8915 단어 Excel
최근 에 Excel 에서 데이터 베 이 스 를 가 져 오 는 사례 를 만 들 고 문 서 를 정리 하여 참고 하도록 하고 있 습 니 다.
1. 최신 poi 다운로드 
  
http://poi.apache.org/download.html  
 
2. 압축 해제 관련 jar 패키지 도입 프로젝트
 
3. 사례 소스 코드
 
import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.text.DecimalFormat;



import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

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;

import org.junit.Test;



public class TestExcel {

    //         ­

    static Log log = LogFactory.getLog(TestExcel.class);

    //   Excel     

    public static String filePath = TestExcel.class.getResource("/test.xls").getPath();



    @Test

    public static void main(String[] args) {

        try {

            //    Excel        

            HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(

                    filePath));

            //  Excel   ,            0

            //     :HSSFSheet sheet = workbook.getSheetAt(0);

            HSSFSheet sheet = wookbook.getSheet("Sheet1");

            //    Excel        

            int rows = sheet.getPhysicalNumberOfRows();

            //    ,   0   , 0      ,   。

            for (int i = 1; i < rows; i++) {

                //         

                HSSFRow row = sheet.getRow(i);

                //     

                if (row != null) {

                    //    Excel        

//                    int cells = row.getPhysicalNumberOfCells();

                    String value = "";

                    //    



                    //      POI  excel   cell.getNumbericCellValue()

                    //

                    DecimalFormat df = new DecimalFormat("0");

                    // ***  3 cells  ,       ,      ***

                    for (int j = 0; j < 5; j++) {

                        //       

                        HSSFCell cell = row.getCell(j);

                        if (cell != null) {

                            switch (cell.getCellType()) {

                            case HSSFCell.CELL_TYPE_FORMULA:

                                break;

                            case HSSFCell.CELL_TYPE_NUMERIC:

                                value += df.format(cell.getNumericCellValue())

                                        + ",";

                                break;

                            case HSSFCell.CELL_TYPE_STRING:

                                value += cell.getStringCellValue() + ",";

                                break;

                            default:

                                value += "#"+",";

                                break;

                            }

                        } else {

                            value += "#" + ",";

                        }

                    }

                    //       mysql    

                    String[] val = value.split(",");



                    System.out.println(val[0]);

                    System.out.println(val[1]);

                    System.out.println(val[2]);

                    System.out.println(val[3]);

                    System.out.println(val[4]);

                    System.out.println(" ");

                    // *******           ******

                }

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

 
이 방법 은 엑셀 2003 버 전 만 읽 을 수 있 고 2010 버 전 은 본인 이 공부 하고 있 습 니 다.

좋은 웹페이지 즐겨찾기