2020-06-04 17:20
@JFinal 相信很多人都是从 springMVC 过来的,为什么这个路由不模仿springMVC的方式,同时也支持现有方式,照顾一下强迫症者(我也是搞题主的模式的)
2020-04-21 17:38
我习惯性的操作如下:
File file = getFile().getFile();
ExcelReader er = new ExcelReader(file, "Sheet1");
List> list = er.getSheetData();
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExcelReader {
    private File file;
    private String sheetName;
    private Sheet sheet;
    private List> listData = null;
    public ExcelReader(String filePath, String sheetName) {
        this.file = new File(filePath);
        this.sheetName = sheetName;
        this.load();
    }
    public ExcelReader(File file, String sheetName) {
        this.file = file;
        this.sheetName = sheetName;
        this.load();
    }
    private void load() {
        FileInputStream inStream = null;
        try {
            inStream = new FileInputStream(file);
            Workbook workBook = WorkbookFactory.create(inStream);
            sheet = workBook.getSheet(sheetName);
            dealSheetData();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inStream != null) {
                    inStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private void dealSheetData() {
        listData = new ArrayList<>();
        int length = sheet.getLastRowNum() + 1;
        for (int i = 0; i < length; i++) {
            Row row = sheet.getRow(i);
            List list = new ArrayList<>();
            if (row != null) {
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j);
                    list.add(getCellValue(cell));
                }
            }
            listData.add(list);
        }
    }
    private String getCellValue(Cell cell) {
        String cellValue = "";
        if (cell != null) {
            switch (cell.getCellTypeEnum()) {
                case NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        DataFormatter formatter = new DataFormatter();
                        cellValue = formatter.formatCellValue(cell);
                    } else {
                        double value = cell.getNumericCellValue();
                        int intValue = (int) value;
                        cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);
                    }
                    break;
                case STRING:
                    cellValue = cell.getStringCellValue();
                    break;
                case BOOLEAN:
                    cellValue = String.valueOf(cell.getBooleanCellValue());
                    break;
                case FORMULA:
                    cellValue = String.valueOf(cell.getCellFormula());
                    break;
                case BLANK:
                    cellValue = "";
                    break;
                case ERROR:
                    cellValue = "";
                    break;
                default:
                    cellValue = cell.toString().trim();
                    break;
            }
        }
        return cellValue.trim();
    }
    public List> getSheetData() {
        return listData;
    }
    public String getCellData(int row, int col) {
        if (row <= 0 || col <= 0) return null;
        if (listData.size() >= row && listData.get(row - 1).size() >= col) {
            return listData.get(row - 1).get(col - 1);
        }
        return null;
    }
    public static void main(String[] args) {
        ExcelReader eh = new ExcelReader("C:\\Users\\lzy\\Desktop\\工作簿1.xls", "Sheet1");
        System.out.println(eh.getCellData(1, 1));
        // System.out.println(eh.getCellData(1, "test1"));
        System.out.println(eh.getCellData(1, 2));
        System.out.println(eh.getSheetData());
    }
}