java使用poi读写Excel
package com.demo.excel; import com.demo.pojo.Student;
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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List; import static javax.xml.bind.JAXBIntrospector.getValue; /**
* Created by xfma on 2017/1/19.
*/
public class ReadExcel { final static String excelFileName = "F:/Test.xls"; public static void main(String[] args) {
try {
createExcel(createStudent());
List<Student> list = readExcel(excelFileName);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName() + "\t" + list.get(i).getAge() + "\t" + list.get(i).getSchool() + "\t" + list.get(i).getAddress());
}
} catch (Exception e) {
e.getStackTrace();
} } /**
* 读Excel
*
* @param excelFileName 文件名
* @return
* @throws Exception
*/
public static List<Student> readExcel(String excelFileName) throws Exception { boolean isExcel2003 = true;
if (!excelFileName.endsWith("xls")) {
isExcel2003 = false;
}
File file = new File(excelFileName);
FileInputStream fis = new FileInputStream(file);
Workbook workbook = null;
/*excel2003和2007不是用同一个对象读取的*/
if (isExcel2003) {
workbook = new HSSFWorkbook(fis);
} else {
workbook = new XSSFWorkbook(fis);
} Sheet sheet = workbook.getSheetAt(0);//得到第0个Sheet
sheet.getLastRowNum();
List<Student> list = new ArrayList<Student>();
for (int r = 0; r < sheet.getLastRowNum(); r++) {
Row row = sheet.getRow(r + 1);//越过标题,从第二行读
if (row != null) {
Student student = new Student();
Cell name = row.getCell(0);
Cell age = row.getCell(1);
Cell address = row.getCell(2);
Cell school = row.getCell(3);
student.setName(getValue(name).toString());
float f = Float.parseFloat(getValue(age).toString());
student.setAge((int) f);
student.setAddress(getValue(address).toString());
student.setSchool(getValue(school).toString());
list.add(student);
}
}
return list;
} /**
* 生成Excel
*
* @param list 对象集合
* @throws Exception
*/
public static void createExcel(List<Student> list) throws Exception {
//1.创建一个HSSFWorkbook对象,每个HSSFWorkbook对应一个新的Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
//2.在HSSFWorkbook中添加一个sheet,对应Excel中的一个sheet表
HSSFSheet sheet = workbook.createSheet("学生信息表");
String[] cells = new String[]{"姓名", "年龄", "地址", "学校"};//表头
int rowSize = list.size() + 1;//从第二条开始读,去掉标题
for (int r = 0; r < rowSize; r++) {
HSSFRow row = sheet.createRow(r);
for (int c = 0; c < cells.length; c++) { HSSFCell cell = row.createCell(c);
if (r == 0) {
cell.setCellValue(cells[c]);//创建表头
} else {
/*往表内写数据*/
int index = r - 1;//从第一条数据往里面写
switch (c) {
case 0:
cell.setCellValue(list.get(index).getName());
continue;
case 1:
cell.setCellValue(list.get(index).getAge());
continue;
case 2:
cell.setCellValue(list.get(index).getAddress());
continue;
case 3:
cell.setCellValue(list.get(index).getSchool());
continue;
}
} }
}
FileOutputStream fileOut = new FileOutputStream(excelFileName);
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
} /**
* 创建示例
*
* @return
*/
public static List<Student> createStudent() {
List<Student> list = new ArrayList<Student>();
Student s1 = new Student("小黑", 18, "上海浦东", "复旦大学");
Student s2 = new Student("小白", 19, "上海普陀", "同济大学");
Student s3 = new Student("小玉", 22, "上海黄埔", "上海交通大学");
Student s4 = new Student("小红", 20, "上海静安", "上海财经大学");
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
return list;
}
}
pojo:
package com.demo.pojo; import java.io.Serializable; /**
* Created by xfma on 2017/1/19.
*/
public class Student implements Serializable{
private String name;
private Integer age;
private String address;
private String school; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getSchool() {
return school;
} public void setSchool(String school) {
this.school = school;
} public Student(String name, Integer age, String address, String school) {
this.name = name;
this.age = age;
this.address = address;
this.school = school;
} public Student() {
}
}
pom.xml:
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<!-- poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
java使用poi读写Excel的更多相关文章
- java 使用POI读写Excel文件(兼容2003、2007)
package com.jadyer.demo; import java.io.File; import java.io.FileOutputStream; import java.io.IOExce ...
- Java Struts2 POI创建Excel文件并实现文件下载
Java Struts2 POI创建Excel文件并实现文件下载2013-09-04 18:53 6059人阅读 评论(1) 收藏 举报 分类: Java EE(49) Struts(6) 版权声明: ...
- jxl读写excel, poi读写excel,word, 读取Excel数据到MySQL
这篇blog是介绍: 1. java中的poi技术读取Excel数据,然后保存到MySQL数据中. 2. jxl读写excel 你也可以在 : java的poi技术读取和导入Excel了解到写入Exc ...
- java使用POI实现excel文件的读取,兼容后缀名xls和xlsx
需要用的jar包如下: 如果是maven管理的项目,添加依赖如下: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --&g ...
- [转]POI读写Excel 修改
[转]POI读写Excel 修改 一.Excel基础 二.HSSF概况 三.通过usermodel读取文件 四.通过usermodel写入文件 五.通过eventusermodel读取文件 六.HSS ...
- Java之POI读取Excel的Package should contain a content type part [M1.13]] with root cause异常问题解决
Java之POI读取Excel的Package should contain a content type part [M1.13]] with root cause异常问题解决 引言: 在Java中 ...
- poi读写Excel
poi读写Excel 对于一个程序员来说,文件操作是经常遇到的,尤其是对Excel文件的操作. 在这里介绍一下我在项目中用到的一个操作Excel的工具——POI.关于POI的一些概念,网络上很多,详细 ...
- 《手把手教你》系列技巧篇(六十六)-java+ selenium自动化测试 - 读写excel文件 - 上篇(详细教程)
1.简介 在自动化测试,有些我们的测试数据是放到excel文件中,尤其是在做数据驱动测试的时候,所以需要懂得如何操作获取excel内的内容.由于java不像python那样有直接操作Excle文件的类 ...
- 《手把手教你》系列技巧篇(六十七)-java+ selenium自动化测试 - 读写excel文件 - 中篇(详细教程)
1.简介 前面介绍了POI可以操作excel,也简单的提到另一个操作excle的工具,本篇介绍一个其他的可以操作excel的工具,但是这个工具有一个前提,excel文件版本只能是97-2003版本,如 ...
随机推荐
- PHP使用mysqli连接MySQL数据库
使用mysqli函数库连接MySQL,支持面向对象和面向过程两种方式: 1.面向对象的使用方式 建立一个连接 $db = new mysqli('localhost', 'root', '123456 ...
- SpringMVC传值(对象或字符串)给前台js
java对象到js对象 1.先使用Jackson把对象转换成json串 ObjectMapper objectMapper = new ObjectMapper(); String json = ob ...
- 不得不看的Java代码性能优化总结
原文:https://blog.csdn.net/mr_smile2014/article/details/50112723 前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么 ...
- Mysql的建表规范与注意事项
一. 表设计规范 库名.表名.字段名必须使用小写字母,“_”分割. 库名.表名.字段名必须不超过12个字符. 库名.表名.字段名见名知意,建议使用名词而不是动词. 建议使用InnoDB存储引擎. 存储 ...
- chrome使用
本文转载于http://www.cnblogs.com/tester-l/p/5743031.html Chrome调试工具各个工具的作用: Element Elements板块你可以看到整个页面的D ...
- 解决eclipse中overlaps the location of another project: 'xxxx'
找遍网络发现各种解释,最常见的一种是: new -> android project -> create project from exist source出现如下错误信息:Invalid ...
- 转python调用Go代码
Go 1.5发布了,其中包含了一个特性:可以编译生成动态链接库,经试验,生成的.so文件可以被python加载并调用.下面举个例子: 先写一个go文件main.go: package main imp ...
- HTML5 的四个亮点
1.XDM cross-document-messaging 跨文档消息传递. 2.原生拖放功能. 3.新媒体元素 audio.video. 4.历史状态管理.
- Struts2学习之拦截器栈
© 版权声明:本文为博主原创文章,转载请注明出处 拦截器栈: - 从结构上看:拦截器栈相当于多个拦截器的组合 - 从功能上看:拦截器栈也是拦截器 默认拦截器栈: - 在struts-core.jar中 ...
- 使用Firebug进行断点调试详解
利用Firebug我们可以非常方便地对网页上的任何JavaScript代码进行断点调试. 首先,使用快捷键F12在当前页面打开Firebug,并切换到脚本选项卡. 其次,我们需要为指定的js代码添加断 ...