使用groovy读取excel里面内容
来源:互联网 发布:淘宝卖家提现收费吗 编辑:程序博客网 时间:2024/04/30 05:23
使用groovy读取excel里面内容
需要依赖
apache.org/poi
项目目录结构:
项目 assets 下面放了一个people.xlsx文件
PS:
- 本文依赖 Groovy读取excel文件 尝试读取后,发现不能运行
- 下载 Apache的POI组建 遇到问题
项目的源码:
import org.apache.poi.ss.usermodel.Rowimport org.apache.poi.xssf.usermodel.XSSFCellimport org.apache.poi.xssf.usermodel.XSSFRowimport org.apache.poi.xssf.usermodel.XSSFSheetimport org.apache.poi.xssf.usermodel.XSSFWorkbook/** * Created by WangQing on 15/11/20. */class TestGroovy { void updateResourceDate(){ def filePath = "./assets/people.xlsx" File file = new File(filePath) FileInputStream is = new FileInputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(is); workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK); //循环sheet (0..<workbook.sheetIterator().collect {return it}.@size).each {s-> XSSFSheet sheet = workbook.getSheetAt(s); int rows = sheet.physicalNumberOfRows; //忽略第一行,标题行 (1..<rows).each{r-> XSSFRow row = sheet.getRow(r); def cells = row.physicalNumberOfCells; (0..<cells).each{c-> XSSFCell cell = row.getCell(c); def name = ""; switch (c) { case 0: name = "A:" break; case 1: name = "B:" break case 2: name = "C:" break case 3: name = "D:" break } print name + " "+cell+ ", "; } println ""; } } } static main(args) { TestGroovy a = new TestGroovy(); a.updateResourceDate(); }}
项目运行结果:
Apache的POI组建 遇到问题
首先进入网址:http://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.13-20150929.zip
下载文件的时候,一般在工程里面依赖的是 jar,但是下面的是 .zip 很是疑惑,这个文件 26.4M,虽然不相信,但是下载下来后解压开,才明白:
其中该项目中使用了
xmlbeans-2.6.0.jar
这个文件在上面的 lib 下面。
因为我使用的mac 2010 office, 所以文件保存的是:.xlsx。开始使用的
HSSFRow ,发现报错:
Request processing failed; nested exception is org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. POI only supports OLE2 Office documents
POIFSFileSystem excelFile = new POIFSFileSystem(new FileInputStream("xxx.xlsx"));HSSFWorkbook wb = new HSSFWorkbook(excelFile);
原因是:
HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
所以,你在使用的时候,如果是2003版的,将项目的中 XSS 替换成 HSS。
0 0
- 使用groovy读取excel里面内容
- VB.NET读取EXCEL 里面的内容
- Groovy 读取excel文件
- groovy : poi 读取 Excel xlsx
- groovy : poi 读取 Excel xlsx
- 使用Java读取Excel文件内容
- 使用Java读取Excel文件内容
- 使用Java读取Excel文件内容
- 使用Java读取Excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 1100. Mars Numbers (20)
- 第一个JAVA程序
- 用SendCloud发送短信验证码
- 《嵌入式产品分析与设计》第8章整理
- apache kafka监控系列-KafkaOffsetMonitor
- 使用groovy读取excel里面内容
- GCD 创建异步线程
- onWindowFocusChanged重要作用 and Activity生命周期
- Spring+webservice写一个test小实例
- mybatis自动生成对应model,dao,xml文件
- 初识内核模块编译
- 数据库:多表之间的设计以及对应类之间的关系,与数据库的操作。
- iOS 表视图的分区头部文字设置
- Java用wait和notify实现线程协作