zoukankan      html  css  js  c++  java
  • 导入工具包ImportExcelUtils

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFDateUtil;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.joda.time.DateTime;

    import java.io.FileInputStream;
    import java.util.Date;

    public class ImportExcelUtils {
    public void cellType(FileInputStream inputStream) throws Exception {
    //创建工作簿
    //03版,excel后缀为.xls的,07版本为.xlsx的,需要将HSSFWorkbook()更换为XSSFWorkbook()
    Workbook workbook = new HSSFWorkbook(inputStream);
    Sheet sheet = workbook.getSheetAt(0);//获取第一页
    //获取标题内容
    Row rowTitle = sheet.getRow(0);
    if (rowTitle != null){
    int cellCount = rowTitle.getPhysicalNumberOfCells();//获取列的数量
    for (int cellNum = 0; cellNum < cellCount ; cellNum++) {
    Cell cell = rowTitle.getCell(cellNum);
    if (cell != null){
    int cellType = cell.getCellType();//获取列的类型
    }
    }
    }

        //读取表中内容
        int rowCount = sheet.getPhysicalNumberOfRows();
        for (int rowNum = 1; rowNum < rowCount; rowNum++) {
            Row rowData = sheet.getRow(rowNum);
            if (rowData != null){
                //读取列
                int cellCount = rowData.getPhysicalNumberOfCells();
                for (int cellNum = 0; cellNum < cellCount ; cellNum++) {
                    Cell cell = rowData.getCell(cellNum);
                    if (cell != null){
                        int cellType = cell.getCellType();
                        String cellValue = "";
                        //判断列数据类型
                        switch (cellType){
                            case HSSFCell.CELL_TYPE_STRING://字符串
                                cellValue = cell.getStringCellValue();
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN://布尔
                                cellValue = String.valueOf(cell.getBooleanCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_BLANK://空
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC://数字(日期、数字)
                                if (HSSFDateUtil.isCellDateFormatted(cell)){//日期
                                    Date date = cell.getDateCellValue();
                                    cellValue = new DateTime(date).toString("yyyy-MM-dd");
                                }else{
                                    //防止数字过长
                                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                    cellValue = cell.toString();
                                }
                                break;
                            case HSSFCell.CELL_TYPE_ERROR://错误
                                break;
                        }
                    }
                }
            }
        }
    
        //关闭流
        inputStream.close();
    }
    

    }

  • 相关阅读:
    伴郎
    MySQL出现Waiting for table metadata lock的场景浅析
    相同name,取最小的id的值,mysql根据相同字段 更新其它字段
    Sequence contains no elements
    Check if List<Int32> values are consecutive
    comparison of truncate vs delete in mysql/sqlserver
    Are query string keys case sensitive?浏览器种输入url附带的参数是否区分大小写
    Understanding Action Filters (C#) 可以用来做权限检查
    糖果缤纷乐攻略
    DNGuard HVM Unpacker(3.71 trial support and x64 fixed)
  • 原文地址:https://www.cnblogs.com/dhc1995/p/14898266.html
Copyright © 2011-2022 走看看