Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How java reads local excel files and converts excel contents into java objects

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

小编今天带大家了解java如何读取本地excel文件并将excel内容转换成java对象,文中知识点介绍的非常详细。觉得有帮助的朋友可以跟着小编一起浏览文章的内容,希望能够帮助更多想解决这个问题的朋友找到问题的答案,下面跟着小编一起深入学习"java如何读取本地excel文件并将excel内容转换成java对象"的知识吧。

在技术的高速发展中我们学习编程不仅仅是为了制作相关的应用程序了,我们开始的使用编程来进行文件之间的操作,那么今天就来和大家分享有关于,java读取本地excel 文件,将excel内容转换成java对象这方面的相关内容。我们的操作工具eclipse + maven。

下面是我们的一个步骤:

1. java操作excel所使用的jar包 poi-ooxml

org.apache.poi

poi-ooxml

3.14

2.在本地建立一个excel文件

3.书写代码实现

package com.daojia.certify.excelimport;import java.io.FileInputStream;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;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.alibaba.fastjson.JSON;import com.daojia.certify.util.DateBuilder;/** * @Description : TODO导入excel * @date : Mar 31, 2018 5:17:14 PM */public class ExcelImport { public static void main(String args[]){ ExcelImport excelImport = new ExcelImport(); try { excelImport.importExcelAction(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //导入Excel数据 public void importExcelAction() throws Exception { //文件路径 String filePath ="C:/Users/daojia/Desktop/test.xlsx"; XSSFWorkbook wookbook = new XSSFWorkbook(new FileInputStream(filePath)); XSSFSheet sheet = wookbook.getSheet("Sheet1"); //获取到Excel文件中的所有行数 int rows = sheet.getPhysicalNumberOfRows(); //遍历行 List list = new ArrayList(); for (int i = 1; i < rows; i++) { // 读取左上端单元格 XSSFRow row = sheet.getRow(i); // 行不为空 if (row != null) { Map map = new HashMap(); //获取到Excel文件中的所有的列 int cells = row.getPhysicalNumberOfCells(); //姓名 XSSFCell nameCell =row.getCell(1); String name = getValue(nameCell); //性别 XSSFCell sexCell =row.getCell(2); String sex = getValue(sexCell); //年龄 XSSFCell ageCell =row.getCell(3); String age = getValue(ageCell); //出生年月 XSSFCell birthCell =row.getCell(4); String birth = DateBuilder.convertDateToString(birthCell.getDateCellValue(), "yyyy-MM-dd"); map.put("name", name); map.put("sex", sex); map.put("age", age); map.put("birth", birth); list.add(map); } } System.out.println("list = "+JSON.toJSONString(list)); } private String getValue(XSSFCell xSSFCell){ if(null == xSSFCell){ return ""; } if (xSSFCell.getCellType() == xSSFCell.CELL_TYPE_BOOLEAN) { // 返回布尔类型的值 return String.valueOf(xSSFCell.getBooleanCellValue()); } else if (xSSFCell.getCellType() == xSSFCell.CELL_TYPE_NUMERIC) { // 返回数值类型的值 return String.valueOf(xSSFCell.getNumericCellValue()); } else { // 返回字符串类型的值 return String.valueOf(xSSFCell.getStringCellValue()); } }

输出结果:

list = [{"age":"","birth":"1991-09-03","name":"张三","sex":"男"},

{"age":"23.0","birth":"1992-09-03","name":"李四","sex":"女"},

{"age":"34.0","birth":"1993-09-03","name":"王五","sex":"男"},

{"age":"54.0","birth":"1994-09-03","name":"赵六","sex":"女"}]

注意:

1.如果excel格式为 .xlsx ,使用以上方法,如果excel格式为 .xls 以上的 XSSFCell 变成 HSSFCell

2.excel中的日期处理

首先将excel日期设置格式为 2017/09/08

其次使用 getDateCellValue 方法读取日期格式即可

Java的优点是什么1. 简单,只需理解基本的概念,就可以编写适合于各种情况的应用程序;2. 面向对象;3. 分布性,Java是面向网络的语言;4. 鲁棒性,java提供自动垃圾收集来进行内存管理,防止程序员在管理内存时容易产生的错误。;5. 安全性,用于网络、分布环境下的Java必须防止病毒的入侵。6. 体系结构中立,只要安装了Java运行时系统,就可在任意处理器上运行。7. 可移植性,Java可以方便地移植到网络上的不同机器。8.解释执行,Java解释器直接对Java字节码进行解释执行。

感谢大家的阅读,以上就是"java如何读取本地excel文件并将excel内容转换成java对象"的全部内容了,学会的朋友赶紧操作起来吧。相信小编一定会给大家带来更优质的文章。谢谢大家对网站的支持!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report