热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java使用poi解析Excel文件

本文取自http:www.cnblogs.comhongtenpjava_poi_excel_xls_xlsx.htmljava中读取Excel文件并解析Excel2007及以

本文取自http://www.cnblogs.com/hongten/p/java_poi_excel_xls_xlsx.html


java中读取Excel文件并解析


  • Excel2007及以前的文件使用[HSSFWorkbook]6
  • Excel2007后的文件使用[XSSFWorkbook]6


使用poi的jar包

这里写图片描述


Common.java

public class Common {public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";public static final String EMPTY = "";public static final String POINT = ".";public static final String NOT_FILE = "Not File!";
}

Util.java

public class Util {public static String getPostfix(String path){if( path == null || Common.EMPTY.equals(path.trim())){return Common.EMPTY;}if(path.contains(Common.POINT)){return path.substring(path.lastIndexOf(Common.POINT) + 1,path.length());}return Common.EMPTY;}
}

ReadExcel.java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import com.manniu.ninecloud.entities.Devices;/*** @Description 解析Excel文件* @data 2015-06-01* @author guo*/
public class ReadExcel {public List<Devices> readExcel(String path) throws IOException{if (path &#61;&#61; null || Common.EMPTY.equals(path)) {return null;}else {String postfix &#61; Util.getPostfix(path);if(!Common.EMPTY.equals(postfix)){if(Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)){return readXlsx(path);}else if (Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {return readXls(path);}} else {System.out.println(path&#43;Common.NOT_FILE);}}return null;}/*** &#64;读取2007及之前的Excel文档*/public List<Devices> readXlsx(String path) throws IOException {InputStream io &#61; new FileInputStream(path);XSSFWorkbook xsBook &#61; new XSSFWorkbook(io);Devices devices &#61; null;List<Devices> list &#61; new ArrayList<Devices>();//循环行for (int rowNum &#61; 0; rowNum (); rowNum&#43;&#43;) {XSSFSheet xSheet &#61; xsBook.getSheetAt(rowNum);if (xSheet &#61;&#61; null) {continue;}for (int num &#61; 1; num (); num&#43;&#43;) {XSSFRow xRow &#61; xSheet.getRow(num);if (xRow !&#61; null) {devices &#61; new Devices();devices.setState(Integer.parseInt(getValue(xRow.getCell(0))));devices.setType(Integer.parseInt(getValue(xRow.getCell(1))));devices.setModel(getValue(xRow.getCell(2)));devices.setVer(getValue(xRow.getCell(3)));devices.setPn(getValue(xRow.getCell(4)));devices.setVn(getValue(xRow.getCell(5)));devices.setSn(getValue(xRow.getCell(6)));list.add(devices);}}}return list;}//读取Excel2007前的.xls文件public List<Devices> readXls(String path) throws IOException {InputStream io &#61; new FileInputStream(path);HSSFWorkbook hBook &#61; new HSSFWorkbook(io);Devices devices &#61; null;List<Devices> list &#61; new ArrayList<Devices>();for (int rowNum &#61; 0; rowNum (); rowNum&#43;&#43;) {HSSFSheet hSheet &#61; hBook.getSheetAt(rowNum);if (hSheet &#61;&#61; null) {continue;}for (int num &#61; 1; num (); num&#43;&#43;) {HSSFRow sRow &#61; hSheet.getRow(num);if (sRow !&#61; null) {devices &#61; new Devices();devices.setState(Integer.parseInt(getValue(sRow.getCell(0))));devices.setType(Integer.parseInt(getValue(sRow.getCell(1))));devices.setModel(getValue(sRow.getCell(2)));devices.setVer(getValue(sRow.getCell(3)));devices.setPn(getValue(sRow.getCell(4)));devices.setVn(getValue(sRow.getCell(5)));devices.setSn(getValue(sRow.getCell(6)));list.add(devices);}}}return list;}&#64;SuppressWarnings("static-access")private String getValue(XSSFCell xCell){if(xCell.getCellType() &#61;&#61; xCell.CELL_TYPE_BOOLEAN){return String.valueOf(xCell.getBooleanCellValue());}else if (xCell.getCellType() &#61;&#61; xCell.CELL_TYPE_NUMERIC) {return String.valueOf(xCell.getNumericCellValue());}else {return String.valueOf(xCell.getStringCellValue());} }&#64;SuppressWarnings("static-access")private String getValue(HSSFCell hCell){if (hCell.getCellType() &#61;&#61; hCell.CELL_TYPE_BOOLEAN) {return String.valueOf(hCell.getBooleanCellValue());}else if (hCell.getCellType() &#61;&#61; hCell.CELL_TYPE_NUMERIC) {return String.valueOf(hCell.getNumericCellValue());}else {return String.valueOf(hCell.getStringCellValue());}}//Test mainpublic static void main(String[] args) throws IOException {String excel2003 &#61; "D://test/test.xls";String excel2010 &#61; "D://test/test1.xlsx";ReadExcel re &#61; new ReadExcel();List list &#61; re.readExcel(excel2003);if(list !&#61; null){for (Devices devices : list) {System.out.println(devices.getState()&#43;"--"&#43;devices.getType());}}}
}

poi 包下载地址


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 纠正网上的错误:自定义一个类叫java.lang.System/String的方法
    本文纠正了网上关于自定义一个类叫java.lang.System/String的错误答案,并详细解释了为什么这种方法是错误的。作者指出,虽然双亲委托机制确实可以阻止自定义的System类被加载,但通过自定义一个特殊的类加载器,可以绕过双亲委托机制,达到自定义System类的目的。作者呼吁读者对网上的内容持怀疑态度,并带着问题来阅读文章。 ... [详细]
  • 本文整理了Java中java.lang.NoSuchMethodError.getMessage()方法的一些代码示例,展示了NoSuchMethodErr ... [详细]
  • 在工作中,遇到需要将excel表中的特定数据提取出来,并将数据以键值对的形式存储到map集合中。因为我用的是maven管理的jar包,所 ... [详细]
  • Birthdate ... [详细]
  • Java使用poi 5.0解析Excel工作簿的例子
    写在之前Excel文档是日常办公中非常普遍的一种数据记录模式。在业务场景中,往往有“导入Excel到某某系统中”的需求,所以这里记录一种使用poi5.0系 ... [详细]
  • 跨站的艺术XSS Fuzzing 的技巧
    作者|张祖优(Fooying)腾讯云云鼎实验室对于XSS的漏洞挖掘过程,其实就是一个使用Payload不断测试和调整再测试的过程,这个过程我们把它叫做F ... [详细]
  • 题目描述:一个DNA序列由ACGT四个字母的排列组合组成。G和C的比例(定义为GC-Ratio)是序列中G和C两个字母的总的出现次数除以总的字母数目(也就是序列长度)。在基因工程中,这个 ... [详细]
  • 两个N位数a和b相乘,手算的话一般是a的末位分别乘以b的末位到首位,然后a的倒数第二位分别乘以b的末位到首位,直到a的首位分别乘以b的末位到首位,最后按位数相加。这个过程的时间复杂度是O(n2)的。 ... [详细]
  • 显示中文星期几
    显示中文星期几引自:第一种方法:直接翻译,最笨、最容易想到的方法。Code获得中文星期名称 ... [详细]
  • Java常用类:String类目录Java常用类:String类StringString类常用方法案例演示String字符串是常量,创建之后不可改变字符串字面值存储在字符串池中,可 ... [详细]
author-avatar
罂粟花wd2010
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有