com.beginner.core.utils.ObjectExcelRead.java Source code

Java tutorial

Introduction

Here is the source code for com.beginner.core.utils.ObjectExcelRead.java

Source

/*
 * Copyright 2015-9999 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beginner.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

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 com.beginner.core.plugin.PageData;

/**
 * EXCEL?
 * @version
 */
public class ObjectExcelRead {

    /**
     * @param filepath //
     * @param filename //??
     * @param startrow //?
     * @param startcol //?
     * @param sheetnum //sheet
     * @return list
     */
    @SuppressWarnings("deprecation")
    public static List<Object> readExcel(String filepath, String filename, int startrow, int startcol,
            int sheetnum) {
        List<Object> varList = new ArrayList<Object>();

        try {
            File target = new File(filepath, filename);
            FileInputStream fi = new FileInputStream(target);
            HSSFWorkbook wb = new HSSFWorkbook(fi);
            HSSFSheet sheet = wb.getSheetAt(sheetnum); //sheet 0
            int rowNum = sheet.getLastRowNum() + 1; //???

            for (int i = startrow; i < rowNum; i++) { //

                PageData varpd = new PageData();
                HSSFRow row = sheet.getRow(i); //
                int cellNum = row.getLastCellNum(); //????

                for (int j = startcol; j < cellNum; j++) { //

                    HSSFCell cell = row.getCell(Short.parseShort(j + ""));
                    String cellValue = null;
                    if (null != cell) {
                        switch (cell.getCellType()) { // excel?????
                        case 0:
                            cellValue = String.valueOf((int) cell.getNumericCellValue());
                            break;
                        case 1:
                            cellValue = cell.getStringCellValue();
                            break;
                        case 2:
                            cellValue = cell.getNumericCellValue() + "";
                            // cellValue = String.valueOf(cell.getDateCellValue());
                            break;
                        case 3:
                            cellValue = "";
                            break;
                        case 4:
                            cellValue = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case 5:
                            cellValue = String.valueOf(cell.getErrorCellValue());
                            break;
                        }
                    } else {
                        cellValue = "";
                    }
                    varpd.put("var" + j, cellValue);
                }
                varList.add(varpd);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return varList;
    }
}