读取excel

来源:互联网 发布:苹方字体ttf windows 编辑:程序博客网 时间:2024/06/06 03:14

public static void readExcel() throws IOException {
int totalRows;// 获取excel总行数
int totalCols;// 获取excel总列数
String readPath = “C:/Users/lc/Desktop/1.xlsx”;
File file = new File(readPath);
FileInputStream is = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(is);
Sheet sheet = wb.getSheetAt(0);
totalRows = sheet.getPhysicalNumberOfRows();
totalCols = sheet.getRow(0).getPhysicalNumberOfCells();
for (int i = 0; i < totalRows; i++) {
Row row = sheet.getRow(i);
for (int j = 0; j < totalCols; j++) {
String cellValue = “”;
Cell cell = row.getCell(j);
if (null != cell) {
// 以下是判断数据的类型
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
cellValue = cell.getNumericCellValue() + “”;
break;

                case HSSFCell.CELL_TYPE_STRING: // 字符串                    cellValue = cell.getStringCellValue();                    break;                case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean                    cellValue = cell.getBooleanCellValue() + "";                    break;                case HSSFCell.CELL_TYPE_FORMULA: // 公式                    cellValue = cell.getCellFormula() + "";                    break;                case HSSFCell.CELL_TYPE_BLANK: // 空值                    cellValue = "";                    break;                case HSSFCell.CELL_TYPE_ERROR: // 故障                    cellValue = "非法字符";                    break;                default:                    cellValue = "未知类型";                    break;                }            }            System.out.println(cellValue + " ");        }        System.out.println();    }}
1 0
原创粉丝点击