1.读取Excle文件内容的方法 拿过来可以直接用 ;

2.参照 http://www.anyrt.com/blog/list/importexcel.html#6

更多知识请参考:http://www.anyrt.com/blog/index.html

说明:本次中使用的jra包有:POI3.9

 package Demo;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFPictureData;
import org.apache.poi.hssf.usermodel.HSSFShape;
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.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcleXlsxDemo {
/**
*
*
*/
public void xlsxTypeExcle(String filePath) {
// File excelFile = new File("/Users/mike/table.xlsx");
File excelFile = new File(filePath);
XSSFWorkbook wb;
try {
wb = new XSSFWorkbook(new FileInputStream(excelFile));
XSSFSheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getRichStringCellValue().getString());
System.out.print("|");
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(String.valueOf(cell.getDateCellValue()));
} else {
System.out.print(cell.getNumericCellValue());
}
System.out.print("|");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
System.out.print("|");
break;
default:
}
}
System.out.println();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
*
* 读取xls格式的excle
*
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws Exception
*/
@SuppressWarnings("deprecation")
public void xlsTypeExcle(String filePath) throws FileNotFoundException, IOException {
// File excelFile = new File("/Users/mike/table5.xls");
File xlsExcelFile = new File(filePath);
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(xlsExcelFile));
HSSFSheet sheet = wb.getSheetAt(0);
System.out.println("***************"+filePath+" 的内容如下:");
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getRichStringCellValue().getString());
System.out.print("|");
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(String.valueOf(cell.getDateCellValue()));
} else {
System.out.print(cell.getNumericCellValue());
}
System.out.print("|");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
System.out.print("|");
break;
default:
}
}
System.out.println();
} } public static void main(String[] args) {
ExcleXlsxDemo xlsx =new ExcleXlsxDemo();
String xlsx_Excle ="D:\\2007Text.xlsx";
String xls_excle="D:\\2003table5.xls";
try {
xlsx.xlsTypeExcle(xls_excle);
xlsx.xlsxTypeExcle(xlsx_Excle);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

Java POI读取excel 支持xls、xlsx

报表存在读取数据源来自excel,系统常常出现读取excel需求,Java POI不仅可以输出excel,也可以读取excel中单元格数据、图片数据,poi也支持excel2007(xlsx)读取,使用poi-3.8-20120326.jar,poi-ooxml-3.8-20120326.jar,poi-scratchpad-3.8-20120326.jar,这些Jar包可以从apache poi获取

关于2003-2007版本Excle单元格读取

File excelFile = new File("/Users/mike/table5.xls");
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
HSSFSheet sheet = wb.getSheetAt(0); for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING://字符串
System.out.print(cell.getRichStringCellValue().getString());
System.out.print("|");
break;
case Cell.CELL_TYPE_NUMERIC://数值与日期
if (DateUtil.isCellDateFormatted(cell)) {
System.out.print(String.valueOf(cell.getDateCellValue()));
} else {
System.out.print(cell.getNumericCellValue());
}
System.out.print("|");
break;
case Cell.CELL_TYPE_BOOLEAN://boolean类型
System.out.print(cell.getBooleanCellValue());
System.out.print("|");
break;
default:
}
}
System.out.println();

图片读取

先获取excel所有的图片,再查询pictureIndex 根据pictureIndex获取对应的图片

//读取图片
List<HSSFPictureData> pictures = wb.getAllPictures(); 
for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { 
    if (shape instanceof HSSFPicture) {
        HSSFPicture pic = (HSSFPicture) shape; 
        int pictureIndex = pic.getPictureIndex()-1; 
        HSSFPictureData picData = pictures.get(pictureIndex);
        System.out.println("image-size:" + picData.getData().length);
    } 

读取sheet名称

wb.getSheetName(0)

整个实例

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
 
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFPictureData;
import org.apache.poi.hssf.usermodel.HSSFShape;
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.DateUtil;
import org.apache.poi.ss.usermodel.Row;
 
public final class TestImportExcel {
 
    public static void main(String[] args) throws Exception  {
 
        File excelFile = new File("/Users/mike/table5.xls");
        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
        HSSFSheet sheet = wb.getSheetAt(0);
 
        for (Row row : sheet) {
            for (Cell cell : row) {
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getRichStringCellValue().getString());
                    System.out.print("|");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        System.out.print(String.valueOf(cell.getDateCellValue()));
                    } else {
                        System.out.print(cell.getNumericCellValue());
                    }
                    System.out.print("|");
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue());
                    System.out.print("|");
                    break;
                default:
                }
            }
            System.out.println();
        }
         
        //读取图片
        List<HSSFPictureData> pictures = wb.getAllPictures(); 
        for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { 
            if (shape instanceof HSSFPicture) {
                HSSFPicture pic = (HSSFPicture) shape; 
                int pictureIndex = pic.getPictureIndex()-1; 
                HSSFPictureData picData = pictures.get(pictureIndex);
                System.out.println("image-size:" + picData.getData().length);
            } 
        } 
         
        System.out.println(wb.getSheetName(0));
    }
}

xlsx读取单元格

xlsx读取大概与xls读取相类似,xlsx采用是X开头的类,采用的是XSSFWorkbook ;

File excelFile = new File("/Users/mike/table.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(excelFile));
XSSFSheet sheet = wb.getSheetAt(0);
 
for (Row row : sheet) {
    for (Cell cell : row) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            System.out.print(cell.getRichStringCellValue().getString());
            System.out.print("|");
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                System.out.print(String.valueOf(cell.getDateCellValue()));
            } else {
                System.out.print(cell.getNumericCellValue());
            }
            System.out.print("|");
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            System.out.print(cell.getBooleanCellValue());
            System.out.print("|");
            break;
        default:
        }
    }
    System.out.println();
}

xlsx读取图片

xlsx读取图片直接获取所有的图片,然后遍历图片获取图片数据

//读取图片
List<XSSFPictureData> pictures = wb.getAllPictures(); 
for (int i = 0; i < pictures.size(); i++) {
    XSSFPictureData pictureData = pictures.get(i);
    byte[] picData = pictureData.getData();
    System.out.println("image-size:" + picData.length);
 }

xlsx示例

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
 
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
public final class TestImportXlsx {
 
    public static void main(String[] args) throws Exception  {
 
        File excelFile = new File("/Users/mike/table.xlsx");
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(excelFile));
        XSSFSheet sheet = wb.getSheetAt(0);
 
        for (Row row : sheet) {
            for (Cell cell : row) {
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getRichStringCellValue().getString());
                    System.out.print("|");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        System.out.print(String.valueOf(cell.getDateCellValue()));
                    } else {
                        System.out.print(cell.getNumericCellValue());
                    }
                    System.out.print("|");
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue());
                    System.out.print("|");
                    break;
                default:
                }
            }
            System.out.println();
        }
         
        //读取图片
        List<XSSFPictureData> pictures = wb.getAllPictures(); 
        for (int i = 0; i < pictures.size(); i++) {
            XSSFPictureData pictureData = pictures.get(i);
            byte[] picData = pictureData.getData();
            System.out.println("image-size:" + picData.length);
         }
        System.out.println(wb.getSheetName(0));
    }
}
 

关于java读取excle文件的相关方法 ;的更多相关文章

  1. 通过java读取excle数据的方法,今天用到了留下来供以后参考使用

    近期项目属于一个棋牌类项目 用到的配置表比较多 所以在这里 贴一下代码,留下来可以参考.也希望对有需要的朋友有所帮助哦 >1.需求将一个excle表格中的数据 读取 然后封装成自定义的对象,本项 ...

  2. 关于解决java读取excel文件遇空行抛空指针的问题 !

    关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...

  3. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  4. Java读取Excel文件的几种方法

    Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...

  5. Java读取txt文件

    package com.loongtao.general.crawler.slave.utils; import java.io.BufferedReader; import java.io.File ...

  6. java 读取XML文件作为配置文件

    首先,贴上自己的实例: XML文件:NewFile.xml(该文件与src目录同级) <?xml version="1.0" encoding="UTF-8&quo ...

  7. java 读取TXT文件的方法

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  8. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest pub ...

  9. java读取xml文件报“org.xml.sax.SAXParseException: Premature end of file” .

    背景:java读取xml文件,xml文件内容只有“<?xml version="1.0" encoding="UTF-8"?>”一行 java读取该 ...

随机推荐

  1. 20155235 《Java程序设计》 实验二 实验三 敏捷开发与XP实践

    20155235 <Java程序设计> 实验二 实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验内容 没有Linux基础的同学建议先学习<Linux基础入 ...

  2. 20155330 2016-2017-2 《Java程序设计》第十周学习总结

    20155330 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 学习目标 了解计算机网络基础 掌握Java Socket编程 理解混合密码系统 掌握Java ...

  3. jq移除最后一个class的值

    $(".his_pg_jl li").on("click",function() {//挂一个点击事件 $(this).addClass('back_img') ...

  4. PostgreSQL Streaming Replication的FATAL ERROR

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@gm ...

  5. uber司机 如何提高评分、接单率、成单率?

    接单率/成单率的解释 接单率计算方法为:成功接单的订单数 除以 系统派单的订单数. 成单率计算方法为:成功完成的订单数 除以 系统派单的订单数. 滴滴快车单单2.5倍,注册地址:http://www. ...

  6. 4361: isn

    4361: isn https://lydsy.com/JudgeOnline/problem.php?id=4361 分析: dp+容斥. 首先计算出每个长度有多少种子序列是非降的.这一步可以$n^ ...

  7. iOS 上架的坑

    有3D-touch机型的坑 昨天在上线的时候遇到了一个坑,最后导致的结果是找了好几个小时,直接到半夜才能上线. 入正题: 坑是:项目运行在456上没什么问题,但是在6S以上的机型就有点击事件不响应的情 ...

  8. 探究linux设备驱动模型之——platform虚拟总线(三)最终章

    这篇是最终章了,结束这一章后,对于platform平台总线驱动的使用方法应该是能够无压力掌握.但是这一章涉及的内容会比前面两章多一些. 我们会一步一步地来完善上一章的例子.完善的目的是能够在应用层去控 ...

  9. HTTP简单教程

    目录 HTTP简介 HTTP工作原理 HTTP消息结构 客户端请求消息 服务器响应消息 实例 HTTP请求方法 HTTP响应头信息 HTTP状态码 HTTP状态码分类 HTTP状态码列表 HTTP c ...

  10. TensorFlow(实战深度学习框架)----深层神经网络(第四章)

    深层神经网络可以解决部分浅层神经网络解决不了的问题. 神经网络的优化目标-----损失函数 深度学习:一类通过多层非线性变化对高复杂性数据建模算法的合集.(两个重要的特性:多层和非线性) 线性模型的最 ...