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表格导出的更多相关文章

  1. Java 使用Apache POI读取和写入Excel表格

    1,引入所用的包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxm ...

  2. 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, ...

  3. jfinal中excel表格导出

    今天工作中遇到了excel表格导出的操作,还好有写好的模板,不然我也是焦头烂额,下面记录一下excel表格导出的操作步骤,同时用来给正在学习jfinal的小伙伴一些参考和学习. 首先我们需要把表格查询 ...

  4. excel表格导出之后身份证号列变成了科学计数法

    excel表格导出之后身份证号列变成了科学计数法 解决:写sql查询出所有数据,并在身份证列添加字符,然后导出,将要复制的excel表格设置单元格格式问文本类型,然后复制粘贴,再把加入的字符删除,搞定 ...

  5. 利用Apache POI操作Excel

    最近在做接口,有个功能是利用Excel导入汽车发动机所需零件信息到线上系统中.简单回顾一下之前学过的用java操作Excel. 1.maven配置Apache POI pom.xml中配置POIjar ...

  6. 利用java反射机制实现读取excel表格中的数据

    如果直接把excel表格中的数据导入数据库,首先应该将excel中的数据读取出来. 为了实现代码重用,所以使用了Object,而最终的结果是要获取一个list如List<User>.Lis ...

  7. Java利用Apache poi导出图表

    jar compile('org.apache.poi:poi:4.0.1') compile('org.apache.poi:poi-scratchpad:4.0.1') compile('org. ...

  8. java使用org.apache.poi读取与保存EXCEL文件

    一.读EXCEL文件 package com.ruijie.wis.cloud.utils; import java.io.FileInputStream; import java.io.FileNo ...

  9. POI tools 参数化生成excel表格

    package com.eccom.neteagle.server.confsave.service.impl; import java.io.File; import java.io.FileNot ...

随机推荐

  1. git remote log error

    使用git pull的时候收到以下信息: error: there are still refs under 'refs/remotes/origin/xxxx'From 10.1.25.57:yyy ...

  2. 【2017-03-20】HTML基础知识、文字标记、图片标记、空格换行、表格、表格嵌套及布局、超链接

    一.HTML基础知识 HTML: 网站(站点) - 网页 网站是由一个或者多个网页组合起来的 HTML作为文件后缀名,可以把文件变为网页 HTML是一门编程语言的名字:超文本标记语言 超越了文字的范畴 ...

  3. Codeforces Gym 100269E Energy Tycoon 贪心

    题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...

  4. [译]Selenium Python文档:六、页面对象

    本章是介绍页面对象设计模式的教程.一个页面对象代表了web应用用户接口的一片区域,你的测试代码将与之交互的. 使用页面对象模式的好处: 可以创建在多个测试样例中都可使用的可重用代码 减少重复性代码 如 ...

  5. HTML5本地图片裁剪并上传

    最近做了一个项目,这个项目中需要实现的一个功能是:用户自定义头像(用户在本地选择一张图片,在本地将图片裁剪成满足系统要求尺寸的大小).这个功能的需求是:头像最初剪切为一个正方形.如果选择的图片小于规定 ...

  6. Hibernate中遇见的问题

    1.在用Hibernate写增删改查时,有两种方式:a.HQL语句,b.原生态sql语句 如果我要执行的语句是:select * from Question HQL是这么写的: Session ses ...

  7. pyqt样式表语法笔记(中)--原创

    pyqt样式表语法笔记(中) pyqt QSS python 样式表 一.弹窗 在日常的各种桌面软件的使用中,我们都会碰到弹窗.例如注册,登录的时候,会有相应的信息弹窗,这里就以信息收集弹窗为例进行弹 ...

  8. Tcl与Design Compiler (六)——基本的时序路径约束

    本文属于原创手打(有参考文献),如果有错,欢迎留言更正:此外,转载请标明出处 http://www.cnblogs.com/IClearner/  ,作者:IC_learner 时序约束可以很复杂,这 ...

  9. Idea Maven 建本地仓库-导入本地JAR包

    需求 IDEA 很方便集成了Maven,但是也有相应的问题,比如使用Maven仓没有包的时候不太方便,这时我们需要建立自已的本地仓库来实现 实现 找到Idea的安装目录下面的Maven,我的在 C:\ ...

  10. h5开发app之在线生成二维码

    h5通过jquery和qrcode在线生成二维码 首先我们需要下载一个qrcode.js文件,然后依次引入jquery和qrcode文件. 1.创建一个输入框以便做演示使用: <input id ...