利用Apache POI 实现简单的Excel表格导出
1、利用POI API实现简单的Excel表格导出
首先假设一个学生实体类:
package com.sun.poi.domain;
import java.io.Serializable;
import java.util.Date;
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
//学生ID
private int studentId;
//学生姓名
private String name;
//年龄
private int age;
//出生年月
private Date birth;
public Student() {
}
public Student(int studentId, String name, int age, Date birth) {
super();
this.studentId = studentId;
this.name = name;
this.age = age;
this.birth = birth;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
利用POI 实现Excel导出功能:
package com.sun.poi.fuction;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.sun.poi.domain.Student;
public class CreateSimpleExcelToDisk {
private static SimpleDateFormat sdf;
private static List<Student> getStudent() throws Exception{
List<Student> studentList = new ArrayList<Student>();
sdf = new SimpleDateFormat("yyyy-mm-dd");
Student stu1 = new Student(1, "张三", 12, sdf.parse("1999-12-30"));
Student stu2 = new Student(1, "李四", 12, sdf.parse("2000-4-20"));
Student stu3 = new Student(1, "王二", 12, sdf.parse("2003-12-15"));
Student stu4 = new Student(1, "麻子", 12, sdf.parse("1989-11-22"));
Student stu5 = new Student(1, "铁蛋", 12, sdf.parse("1999-5-11"));
studentList.add(stu1);
studentList.add(stu2);
studentList.add(stu3);
studentList.add(stu4);
studentList.add(stu5);
return studentList;
}
public static void main(String[] args) throws Exception {
//获取数据源
List<Student> studentList = getStudent();
//1、创建一个webbook,表示一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
//2、在Excel文件中添加一个sheet页
HSSFSheet sheet = wb.createSheet("学生信息表");
//3、在sheet页中添加表头,第一行
HSSFRow row = sheet.createRow((int)0);
//4、设置单元格的样式
HSSFCellStyle style = wb.createCellStyle();
//设置样式居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//5、创建一个单元格,并设置居中样式
HSSFCell cell = row.createCell(0);
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("生日");
cell.setCellStyle(style);
for (int i = 0; i < studentList.size(); i++) {
row = sheet.createRow(i + 1);
Student stu = (Student)studentList.get(i);
row.createCell(0).setCellValue(stu.getStudentId());
row.createCell(1).setCellValue(stu.getName());
row.createCell(2).setCellValue(stu.getAge());
row.createCell(3).setCellValue(sdf.format(stu.getBirth()));
}
try {
//6、导出到磁盘上
FileOutputStream fout = new FileOutputStream("D:/student.xls");
wb.write(fout);
fout.close();
System.out.println("导出excel成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}
利用Apache POI 实现简单的Excel表格导出的更多相关文章
- Java 使用Apache POI读取和写入Excel表格
1,引入所用的包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxm ...
- Apache POI – Reading and Writing Excel file in Java
来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...
- jfinal中excel表格导出
今天工作中遇到了excel表格导出的操作,还好有写好的模板,不然我也是焦头烂额,下面记录一下excel表格导出的操作步骤,同时用来给正在学习jfinal的小伙伴一些参考和学习. 首先我们需要把表格查询 ...
- excel表格导出之后身份证号列变成了科学计数法
excel表格导出之后身份证号列变成了科学计数法 解决:写sql查询出所有数据,并在身份证列添加字符,然后导出,将要复制的excel表格设置单元格格式问文本类型,然后复制粘贴,再把加入的字符删除,搞定 ...
- 利用Apache POI操作Excel
最近在做接口,有个功能是利用Excel导入汽车发动机所需零件信息到线上系统中.简单回顾一下之前学过的用java操作Excel. 1.maven配置Apache POI pom.xml中配置POIjar ...
- 利用java反射机制实现读取excel表格中的数据
如果直接把excel表格中的数据导入数据库,首先应该将excel中的数据读取出来. 为了实现代码重用,所以使用了Object,而最终的结果是要获取一个list如List<User>.Lis ...
- Java利用Apache poi导出图表
jar compile('org.apache.poi:poi:4.0.1') compile('org.apache.poi:poi-scratchpad:4.0.1') compile('org. ...
- java使用org.apache.poi读取与保存EXCEL文件
一.读EXCEL文件 package com.ruijie.wis.cloud.utils; import java.io.FileInputStream; import java.io.FileNo ...
- POI tools 参数化生成excel表格
package com.eccom.neteagle.server.confsave.service.impl; import java.io.File; import java.io.FileNot ...
随机推荐
- Xamarin自定义布局系列——ListView的一个自定义实现ItemsControl(横向列表)
在以前写UWP程序的时候,了解到在ListView或者ListBox这类的列表空间中,有一个叫做ItemsPannel的属性,它是所有列表中子元素实际的容器,如果要让列表进行横向排列,只需要在Xaml ...
- 使用spring webflow,在前台页面中如何显示后端返回的错误信息
刚刚接触spring webflow,相关的资料并不是很多,并且大都是那种入门的 .xml文件的配置. 用到的CAS 最新的4.0版本用的就是web-flow流,前台页面使用的是表单提交.于是我就碰到 ...
- Build your own linino system 编译你自己的linino系统
懒癌犯了,先简单写过程,之后有时间再补上每一步的理由吧.对着来一遍,有bug请留言,我会尝试回答.(づ ̄ 3 ̄)づ ------------------------------------------ ...
- JAVA-Servlet-过滤器知识总结
JAVA Filter是什么呢? Servlet过滤器实际上就是一个标准的java类,这个类通过实现Filter接口获得过滤器的功能.它在jsp容器启动的时候通过web.xml配置文件被系统加载.Se ...
- shell中的条件表达式
条件表达式返回的结果都为布尔型 真为1,假为0 条件测试的表达式 [expression] [[expression]] test expression 这三种条件表达式的效果是一样的 比较符 整数比 ...
- 服务器证书安装配置指南(SLB)
一.生成证书请求 1.下载CSR生成工具 您需要使用CSR生成工具来创建证书请求. 下载AutoCSR: http://www.itrus.cn/soft/autocsr.rar 2.生成服务器 ...
- WEB开发性能优化--核心定义介绍篇(1)
推荐理由 随着 互联网的蓬勃发展,并且伴随着产品功能的越来越复杂,对于技术人员来说最大的挑战就是如何在保证业务快速发展的同时,也可保证不断复杂的业务对用户体验的影响,其中对用户来说最重要的体验指标是如 ...
- dotweb——go语言的一个微型web框架(一)
dotweb是16年正式托管到github的一个开源项目,go语言的web框架目前也有很多,出名的有bee和echo.它们都是很优秀的框架,但是我们喜欢更轻.更小的东西,经历一些之后我们更青睐微服务这 ...
- sql server删除主键约束所想到的
从网上找到了下面一段代码: declare @Pk varchar(100);select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('表 ...
- 面向对象的三大特征——封装、继承、多态(&常用关键字)
一.封装 Encapsulation 在面向对象程式设计方法中,封装是指,一种将抽象性函式接口的实作细节部份包装.隐藏起来的方法. 封装的概念(针对服务器开发,保护内部,确保服务器不出现问题) 将类的 ...