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

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

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

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

  1. package Demo;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.util.List;
  8. import org.apache.poi.hssf.usermodel.HSSFPicture;
  9. import org.apache.poi.hssf.usermodel.HSSFPictureData;
  10. import org.apache.poi.hssf.usermodel.HSSFShape;
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.apache.poi.ss.usermodel.Cell;
  14. import org.apache.poi.ss.usermodel.DateUtil;
  15. import org.apache.poi.ss.usermodel.Row;
  16. import org.apache.poi.xssf.usermodel.XSSFSheet;
  17. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  18.  
  19. public class ExcleXlsxDemo {
  20. /**
  21. *
  22. *
  23. */
  24. public void xlsxTypeExcle(String filePath) {
  25. // File excelFile = new File("/Users/mike/table.xlsx");
  26. File excelFile = new File(filePath);
  27. XSSFWorkbook wb;
  28. try {
  29. wb = new XSSFWorkbook(new FileInputStream(excelFile));
  30. XSSFSheet sheet = wb.getSheetAt(0);
  31. for (Row row : sheet) {
  32. for (Cell cell : row) {
  33. switch (cell.getCellType()) {
  34. case Cell.CELL_TYPE_STRING:
  35. System.out.print(cell.getRichStringCellValue().getString());
  36. System.out.print("|");
  37. break;
  38. case Cell.CELL_TYPE_NUMERIC:
  39. if (DateUtil.isCellDateFormatted(cell)) {
  40. System.out.print(String.valueOf(cell.getDateCellValue()));
  41. } else {
  42. System.out.print(cell.getNumericCellValue());
  43. }
  44. System.out.print("|");
  45. break;
  46. case Cell.CELL_TYPE_BOOLEAN:
  47. System.out.print(cell.getBooleanCellValue());
  48. System.out.print("|");
  49. break;
  50. default:
  51. }
  52. }
  53. System.out.println();
  54. }
  55. } catch (FileNotFoundException e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. } catch (IOException e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. /**
  65. *
  66. * 读取xls格式的excle
  67. *
  68. * @param args
  69. * @throws IOException
  70. * @throws FileNotFoundException
  71. * @throws Exception
  72. */
  73. @SuppressWarnings("deprecation")
  74. public void xlsTypeExcle(String filePath) throws FileNotFoundException, IOException {
  75. // File excelFile = new File("/Users/mike/table5.xls");
  76. File xlsExcelFile = new File(filePath);
  77. HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(xlsExcelFile));
  78. HSSFSheet sheet = wb.getSheetAt(0);
  79. System.out.println("***************"+filePath+" 的内容如下:");
  80. for (Row row : sheet) {
  81. for (Cell cell : row) {
  82. switch (cell.getCellType()) {
  83. case Cell.CELL_TYPE_STRING:
  84. System.out.print(cell.getRichStringCellValue().getString());
  85. System.out.print("|");
  86. break;
  87. case Cell.CELL_TYPE_NUMERIC:
  88. if (DateUtil.isCellDateFormatted(cell)) {
  89. System.out.print(String.valueOf(cell.getDateCellValue()));
  90. } else {
  91. System.out.print(cell.getNumericCellValue());
  92. }
  93. System.out.print("|");
  94. break;
  95. case Cell.CELL_TYPE_BOOLEAN:
  96. System.out.print(cell.getBooleanCellValue());
  97. System.out.print("|");
  98. break;
  99. default:
  100. }
  101. }
  102. System.out.println();
  103. }
  104.  
  105. }
  106.  
  107. public static void main(String[] args) {
  108. ExcleXlsxDemo xlsx =new ExcleXlsxDemo();
  109. String xlsx_Excle ="D:\\2007Text.xlsx";
  110. String xls_excle="D:\\2003table5.xls";
  111. try {
  112. xlsx.xlsTypeExcle(xls_excle);
  113. xlsx.xlsxTypeExcle(xlsx_Excle);
  114. } catch (FileNotFoundException e) {
  115. // TODO Auto-generated catch block
  116. e.printStackTrace();
  117. } catch (IOException e) {
  118. // TODO Auto-generated catch block
  119. e.printStackTrace();
  120. }
  121.  
  122. }
  123.  
  124. }

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单元格读取

  1. File excelFile = new File("/Users/mike/table5.xls");
  2. HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
  3. HSSFSheet sheet = wb.getSheetAt(0);
  4.  
  5. for (Row row : sheet) {
  6. for (Cell cell : row) {
  7. switch (cell.getCellType()) {
  8. case Cell.CELL_TYPE_STRING://字符串
  9. System.out.print(cell.getRichStringCellValue().getString());
  10. System.out.print("|");
  11. break;
  12. case Cell.CELL_TYPE_NUMERIC://数值与日期
  13. if (DateUtil.isCellDateFormatted(cell)) {
  14. System.out.print(String.valueOf(cell.getDateCellValue()));
  15. } else {
  16. System.out.print(cell.getNumericCellValue());
  17. }
  18. System.out.print("|");
  19. break;
  20. case Cell.CELL_TYPE_BOOLEAN://boolean类型
  21. System.out.print(cell.getBooleanCellValue());
  22. System.out.print("|");
  23. break;
  24. default:
  25. }
  26. }
  27. System.out.println();

图片读取

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

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

读取sheet名称

  1. wb.getSheetName(0)

整个实例

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.util.List;
  4.  
  5. import org.apache.poi.hssf.usermodel.HSSFPicture;
  6. import org.apache.poi.hssf.usermodel.HSSFPictureData;
  7. import org.apache.poi.hssf.usermodel.HSSFShape;
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  10. import org.apache.poi.ss.usermodel.Cell;
  11. import org.apache.poi.ss.usermodel.DateUtil;
  12. import org.apache.poi.ss.usermodel.Row;
  13.  
  14. public final class TestImportExcel {
  15.  
  16.     public static void main(String[] args) throws Exception  {
  17.  
  18.         File excelFile = new File("/Users/mike/table5.xls");
  19.         HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
  20.         HSSFSheet sheet = wb.getSheetAt(0);
  21.  
  22.         for (Row row : sheet) {
  23.             for (Cell cell : row) {
  24.                 switch (cell.getCellType()) {
  25.                 case Cell.CELL_TYPE_STRING:
  26.                     System.out.print(cell.getRichStringCellValue().getString());
  27.                     System.out.print("|");
  28.                     break;
  29.                 case Cell.CELL_TYPE_NUMERIC:
  30.                     if (DateUtil.isCellDateFormatted(cell)) {
  31.                         System.out.print(String.valueOf(cell.getDateCellValue()));
  32.                     } else {
  33.                         System.out.print(cell.getNumericCellValue());
  34.                     }
  35.                     System.out.print("|");
  36.                     break;
  37.                 case Cell.CELL_TYPE_BOOLEAN:
  38.                     System.out.print(cell.getBooleanCellValue());
  39.                     System.out.print("|");
  40.                     break;
  41.                 default:
  42.                 }
  43.             }
  44.             System.out.println();
  45.         }
  46.          
  47.         //读取图片
  48.         List<HSSFPictureData> pictures = wb.getAllPictures(); 
  49.         for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { 
  50.             if (shape instanceof HSSFPicture) {
  51.                 HSSFPicture pic = (HSSFPicture) shape; 
  52.                 int pictureIndex = pic.getPictureIndex()-1; 
  53.                 HSSFPictureData picData = pictures.get(pictureIndex);
  54.                 System.out.println("image-size:" + picData.getData().length);
  55.             } 
  56.         } 
  57.          
  58.         System.out.println(wb.getSheetName(0));
  59.     }
  60. }

xlsx读取单元格

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

  1. File excelFile = new File("/Users/mike/table.xlsx");
  2. XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(excelFile));
  3. XSSFSheet sheet = wb.getSheetAt(0);
  4.  
  5. for (Row row : sheet) {
  6.     for (Cell cell : row) {
  7.         switch (cell.getCellType()) {
  8.         case Cell.CELL_TYPE_STRING:
  9.             System.out.print(cell.getRichStringCellValue().getString());
  10.             System.out.print("|");
  11.             break;
  12.         case Cell.CELL_TYPE_NUMERIC:
  13.             if (DateUtil.isCellDateFormatted(cell)) {
  14.                 System.out.print(String.valueOf(cell.getDateCellValue()));
  15.             } else {
  16.                 System.out.print(cell.getNumericCellValue());
  17.             }
  18.             System.out.print("|");
  19.             break;
  20.         case Cell.CELL_TYPE_BOOLEAN:
  21.             System.out.print(cell.getBooleanCellValue());
  22.             System.out.print("|");
  23.             break;
  24.         default:
  25.         }
  26.     }
  27.     System.out.println();
  28. }

xlsx读取图片

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

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

xlsx示例

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.util.List;
  4.  
  5. import org.apache.poi.ss.usermodel.Cell;
  6. import org.apache.poi.ss.usermodel.DateUtil;
  7. import org.apache.poi.ss.usermodel.Row;
  8. import org.apache.poi.xssf.usermodel.XSSFPictureData;
  9. import org.apache.poi.xssf.usermodel.XSSFSheet;
  10. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  11.  
  12. public final class TestImportXlsx {
  13.  
  14.     public static void main(String[] args) throws Exception  {
  15.  
  16.         File excelFile = new File("/Users/mike/table.xlsx");
  17.         XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(excelFile));
  18.         XSSFSheet sheet = wb.getSheetAt(0);
  19.  
  20.         for (Row row : sheet) {
  21.             for (Cell cell : row) {
  22.                 switch (cell.getCellType()) {
  23.                 case Cell.CELL_TYPE_STRING:
  24.                     System.out.print(cell.getRichStringCellValue().getString());
  25.                     System.out.print("|");
  26.                     break;
  27.                 case Cell.CELL_TYPE_NUMERIC:
  28.                     if (DateUtil.isCellDateFormatted(cell)) {
  29.                         System.out.print(String.valueOf(cell.getDateCellValue()));
  30.                     } else {
  31.                         System.out.print(cell.getNumericCellValue());
  32.                     }
  33.                     System.out.print("|");
  34.                     break;
  35.                 case Cell.CELL_TYPE_BOOLEAN:
  36.                     System.out.print(cell.getBooleanCellValue());
  37.                     System.out.print("|");
  38.                     break;
  39.                 default:
  40.                 }
  41.             }
  42.             System.out.println();
  43.         }
  44.          
  45.         //读取图片
  46.         List<XSSFPictureData> pictures = wb.getAllPictures(); 
  47.         for (int i = 0; i < pictures.size(); i++) {
  48.             XSSFPictureData pictureData = pictures.get(i);
  49.             byte[] picData = pictureData.getData();
  50.             System.out.println("image-size:" + picData.length);
  51.          }
  52.         System.out.println(wb.getSheetName(0));
  53.     }
  54. }
 

关于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. Leecode刷题之旅-C语言/python-342 4的幂

    这里不列举普通的方法了. 发现一个好帖: 学习一下: https://blog.csdn.net/butterfly5211314/article/details/86099993 --------- ...

  2. hadoop伪分布式组件安装

    一.版本建议 Centos V7.5 Java V1.8 Hadoop V2.7.6 Hive V2.3.3 Mysql V5.7 Spark V2.3 Scala V2.12.6 Flume V1. ...

  3. MySql访客连接设置

    步骤: 1 . 打开命令窗口,切换到mysql安装目录 可以在控制台目录切换,也可以打开所在安装目录后再打开控制台 2 . 执行命令:mysql -u root -p 3 . 无法访问的话,查看防火墙 ...

  4. C#课后小作业

    有关C#基础的练手 跟大家一起分享下 1.让用户输入一个100以内的数 打印1-100之间所有的数,用户输入的数除外 2.让用户输入一个100以内的数 打印1-这个数之间所有的数的和 3.使用一个fo ...

  5. 我们一起学习WCF 第三篇头消息验证用户身份

    前言:今天我主要写的是关于头消息的一个用处验证用户信息 下面我画一个图,可以先看图 第一步:我们先开始做用户请求代码 首先:创建一个可执行的上下文对象块并定义内部传输的通道 using (Operat ...

  6. python接口自动化1-发送get请求 前言

    前言 requests模块,也就是老污龟,为啥叫它老污龟呢,因为这个官网上的logo就是这只污龟,接下来就是学习它了. 一.环境安装 1.用pip安装requests模块 >>pip in ...

  7. 《C++设计新思维》Command设计模式读后感

    原文内容提领: 本书第5章标题为泛化仿函数,我认为本章真正讲述的内容可以总结出一句话! 如何利用C++老标准实现C++11新标准类似std::function提供的功能. std::function简 ...

  8. 数据库sql优化总结之2-百万级数据库优化方案+案例分析

    项目背景 有三张百万级数据表 知识点表(ex_subject_point)9,316条数据 试题表(ex_question_junior)2,159,519条数据 有45个字段 知识点试题关系表(ex ...

  9. Angular7运行机制--根据腾讯课堂米斯特吴 《Angular4从入门到实战》学习笔记分析完成

  10. redis 批量删除操作

    redis 批量删除操作 需要在redis里面清空一批数据,redis没有支持通配符删除, 只有del key1 key2 ... 但是可以通配符获取 KEYS PATTERN 然后利用linux管道 ...