• java 读取excel


     1 /**
     2  * @Author: 唐松怀
     3  * @Date: 2020/2/26 14:40
     4  */
     5 import java.io.File;
     6 import java.io.IOException;
     7 
     8 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
     9 import org.apache.poi.ss.usermodel.Row;
    10 import org.apache.poi.ss.usermodel.Sheet;
    11 import org.apache.poi.ss.usermodel.Workbook;
    12 import org.apache.poi.ss.usermodel.WorkbookFactory;
    13     public class excel01
    14     {
    15         public static void main(String[] args) throws IOException,
    16                 InvalidFormatException
    17         {
    18             File xlsFile = new File("C:\Users\Admin\Desktop\01.xls");
    19             // 获得工作簿
    20             Workbook workbook = WorkbookFactory.create(xlsFile);
    21             // 获得工作表个数
    22             int sheetCount = workbook.getNumberOfSheets();
    23             // 遍历工作表
    24             for (int i = 0; i < sheetCount; i++)
    25             {
    26                 Sheet sheet = workbook.getSheetAt(i);
    27                 // 获得行数
    28                 int rows = sheet.getLastRowNum() + 1;
    29                 // 获得列数,先获得一行,在得到改行列数
    30                 Row tmp = sheet.getRow(0);
    31                 if (tmp == null)
    32                 {
    33                     continue;
    34                 }
    35                 int cols = tmp.getPhysicalNumberOfCells();
    36                 // 读取数据  getPhysicalNumberOfCells 是获取不为空的列个数。
                //
    getLastCellNum 是获取最后一个不为空的列是第几个
    37                 for (int row = 0; row < rows; row++)
    38                 {
    39                     Row r = sheet.getRow(row);
    40                     for (int col = 0; col < cols; col++)
    41                     {
    42                         if (col==2 && row!=0 )   {
    43                             String result=r.getCell(col).getStringCellValue();
    44                             String t01=result;
    45                             String t02= t01.replace("a","").replace("/","-");
    46                             System.out.print(t02+" ");
    47                             continue;
    48                         }
    49                         System.out.printf(r.getCell(col).getStringCellValue()+" ");
    50                     }
    51                     System.out.println();
    52                 }
    53             }
    54         }
    55     }

    excel 格式


    getLastCellNum 是获取最后一个不为空的列是第几个

    RUSH B
  • 相关阅读:
    常用正则表达式实例
    java doc注释
    不让WINDOWS检测硬盘的方法
    maven eclipse插件使用问题解决
    indexof 和 indexofany有什么区别
    asp.net验证码
    C#里如何把数据库里的日期显示为只包含年月日
    雷人的发现 谷歌浏览器三大不为人知的秘密
    三层架构实例
    正则表达式30分钟入门教程
  • 原文地址:https://www.cnblogs.com/tangsonghuai/p/12367641.html
Copyright © 2020-2023  润新知