zoukankan      html  css  js  c++  java
  • Java读取execl数据

    1.先创建execl:sample.xls

    2.Java部分:先下载导入poi-*.jar架包。楼主使用的是poi-3.9.jar  

    package test;
    
    import java.io.FileInputStream;
    
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    
    public class ExeclDemo {
        public static void main(String[] args) {
            ExeclDemo execlDemo = new ExeclDemo();
            try {
                execlDemo.sheet1();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void sheet1(){
            String filePath = "C:/Users/it_zhaojie/Desktop/sample.xls";
            FileInputStream stream;
            try {
                stream = new FileInputStream(filePath);
                HSSFWorkbook workbook = new HSSFWorkbook(stream);// 读取现有的Excel
                HSSFSheet sheet = workbook.getSheet("Sheet1");// 得到指定名称的Sheet
                for (Row row : sheet) {
                    for (Cell cell : row) {
                        System.out.print(cell + "	");
                    }
                    System.out.println();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }

    2.1不读取第一行只读取内容

        public void sheet2(){
            String filePath = "C:/Users/it_zhaojie/Desktop/sample.xls";
            FileInputStream stream;
            try {
                stream = new FileInputStream(filePath);
                HSSFWorkbook workbook = new HSSFWorkbook(stream);// 读取现有的Excel
                HSSFSheet sheet = workbook.getSheet("Sheet1");// 得到指定名称的Sheet
                List<Row> list = new ArrayList<Row>();
                for (Row row : sheet) {
                    list.add(row);
                }
                
                for (int i = 1; i < list.size(); i++) {
                    for (Cell cell : list.get(i)) {
                        System.out.print(cell + "	");
                    }
                    System.out.println();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }

    3.运行效果

  • 相关阅读:
    docker 的基本使用
    yum 工具安装的程序默认目录与位置
    Tampermonkey 脚本记录
    Windows 安装配置sublime-text3与破解
    heidisql 轻量级工具
    ubuntu 系统配置静态IP地址
    linux 日志分析
    Windows 桌面整理工具
    高效实用Linux命令行
    物联网实验4 alljoyn物联网实验之手机局域网控制设备
  • 原文地址:https://www.cnblogs.com/Jack-zhao/p/13139299.html
Copyright © 2011-2022 走看看