zoukankan      html  css  js  c++  java
  • Java导出Excel表格

    public void main(){

      Workbook wookbook = ExcelUtil.createWorkbook(upload);
      Sheet sheet = wookbook.getSheetAt(0);//读取第一个表格
    int rows = sheet.getPhysicalNumberOfRows();//获取表格的行数
      for (int i = 3; i < rows; i++) {//从第三行开始是输入的数据
        Row row = sheet.getRow(i);

        //空行校验
        if (row == null || isRowEmpty(row)|| row.getFirstCellNum() < 0) {//空行跳过  试过很多种方法,都不太理想,用这三种方法校验基本上就都可以校验出来了
        continue;
        }
    
    

        //获取Excel整数
        HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
        String code = dataFormatter.formatCellValue(row.getCell(2));
        
        //获取公式计算后的数据
        String sellPrice =getCellValue(row.getCell(5));

      }
    }

    //校验是否为空行
    public  boolean isRowEmpty(Row row) throws Exception {
    for (int r = row.getFirstCellNum(); r < row.getLastCellNum(); r++) {
    Cell cell = row.getCell(r);
    if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK)
    return false;
    }
    return true;
    }

    //获取公式计算后的值
    public String getCellValue(Cell cell) throws Exception{
    int cellType = cell.getCellType();
    String cellValue = "";
    switch (cellType) {
    case HSSFCell.CELL_TYPE_NUMERIC:
    cellValue = String.valueOf(cell.getNumericCellValue());
    break;

    case HSSFCell.CELL_TYPE_FORMULA:
    try {
    cellValue = cell.getStringCellValue();
    } catch (IllegalStateException e) {
    cellValue = String.valueOf(cell.getNumericCellValue());
    }
    break;

    default:
    cellValue = cell.getStringCellValue();
    }

    return cellValue.trim();
    }
  • 相关阅读:
    C#3.0实现变异赋值(Mutantic Assignment)
    博客园积分算法探讨
    C#动静结合编程之二: 两种哲学
    REST构架风格介绍之二:CRUD
    C# vs C++之一:委托 vs 函数指针
    REST构架风格介绍之一:状态表述转移
    C#动静结合编程之三:Duck Typing
    C#动静结合编程之四:泛型委托
    C# vs C++之二:GC vs RAII
    Ecshop文章分类列表页如何自定义Title以提高SEO效果
  • 原文地址:https://www.cnblogs.com/mingyi123/p/6752330.html
Copyright © 2011-2022 走看看