java 의 조작 excel 클래스

package com.lilysilk.util;

import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @author dsp
 *@Comments   :     Excel    
 *          excel      sheet 、
 *          、  sheet           
 *
 *
 **/
public class ExcelUtil {
    private static XSSFSheet ExcelWSheet;
    /**
     * excel    
     */
    private static XSSFWorkbook ExcelWBook;
    /**
     *      
     */
    private static XSSFCell ExcelCell;

    /**
     *        excel      sheet  
     *   , excel   ,        ,      excel    sheet  
     * @param Path
     * @param SheetName
     */
    public static void setExcelFile(String Path,String SheetName) {
        FileInputStream ExcelFile;

        try {
            /**
             *    excel   FileInputStream  
             */
            ExcelFile=new FileInputStream(Path);
            /**
             *    EXCEL   execlWXSSFWorkbook  
             */
            ExcelWBook =new XSSFWorkbook(ExcelFile);
            /**
             *     XSSFCell   ,  excel    sheet  ,    sheet  、        
             */
            ExcelWSheet=ExcelWBook.getSheet(SheetName);

        } catch (Exception e) {
            /**
             *  TODO Auto-generated catch block
             */
            e.printStackTrace();
        }
    }
    /**
     *   excel            ,          .xlsx excel  
     * @param rowNum
     * @param colNum
     * @return
     * @throws Exception
     */
    public static String getCellData(int rowNum,int colNum)throws Exception {
        try {
        /**
         *                  ,          
         */
            ExcelCell=ExcelWSheet.getRow(rowNum).getCell(colNum);
        /**
         *               ,   getStringCellValue          
         *              ,      getNumericCellValue          
         */
        String CellData =ExcelCell.getCellType()==XSSFCell.CELL_TYPE_STRING?ExcelCell.getStringCellValue()+"":String.valueOf(Math.round(ExcelCell.getNumericCellValue()));

        return CellData;
        }
        catch(Exception e){
            e.printStackTrace();
            /**
             *       ,       
             */
            return "  ";
        }
    }

    /**
     *   excel          
     * @return
     */
    public static int getLastRowNum() {
        /**
         *     sheet       
         */
        return ExcelWSheet.getLastRowNum();
    }
}

좋은 웹페이지 즐겨찾기