zoukankan
html css js c++ java
Java 实现导出excel表 POI
1.首先下载poi-3.6-20091214.jar,下载地址如下:
http://download.csdn.net/detail/evangel_z/3895051
2.Student.java
import java.util.Date; public class Student { private int id; private String name; private int age; private Date birth; public Student() { } public Student(int id, String name, int age, Date birth) { this.id = id; this.name = name; this.age = age; this.birth = birth; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } }
3.CreateSimpleExcelToDisk.java
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; public class CreateSimpleExcelToDisk { /** * @功能:手工构建一个简单格式的Excel */ private static List<Student> getStudent() throws Exception { List list = new ArrayList(); SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd"); Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12")); Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12")); Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12")); list.add(user1); list.add(user2); list.add(user3); return list; } public static void main(String[] args) throws Exception { // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("学生表一"); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 HSSFCell cell = row.createCell((short) 0); cell.setCellValue("学号"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("姓名"); cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue("年龄"); cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("生日"); cell.setCellStyle(style); // 第五步,写入实体数据 实际应用中这些数据从数据库得到, List list = CreateSimpleExcelToDisk.getStudent(); for (int i = 0; i < list.size(); i++) { row = sheet.createRow((int) i + 1); Student stu = (Student) list.get(i); // 第四步,创建单元格,并设置值 row.createCell((short) 0).setCellValue((double) stu.getId()); row.createCell((short) 1).setCellValue(stu.getName()); row.createCell((short) 2).setCellValue((double) stu.getAge()); cell = row.createCell((short) 3); cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu .getBirth())); } // 第六步,将文件存到指定位置 try { FileOutputStream fout = new FileOutputStream("E:/students.xls"); wb.write(fout); fout.close(); } catch (Exception e) { e.printStackTrace(); } } }
查看全文
相关阅读:
Mysql初探:内存数据刷盘机制
数据管理流程,基础入门简介
数据分析:复杂业务场景下,量化评估流程
架构设计:服务自动化部署和管理流程
Hadoop框架:MapReduce基本原理和入门案例
架构设计:微服务模式下,实现灰度发布模式
架构设计:分布式结构下,服务部署发布
编码风格:Mvc模式下SSM环境,代码分层管理
开发工具:Mybatis.Plus.插件三种方式的逆向工程
Hadoop框架:HDFS高可用环境配置
原文地址:https://www.cnblogs.com/wzh123/p/3473767.html
最新文章
shell 批量重命名图片脚本 ModifyPicsName
#vim 编辑器注释看不见 #修改vim 注释颜色 #修改vim主题
playbook 依赖关系
# Failed to execute 'write' on 'Document' It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
#高德地图api移动端定位失败解决方案 #H5 原生Geollocation接口Chomre浏览器的坑
ucore操作系统学习(五) ucore lab5用户进程管理
自己动手实现java数据结构(九) 跳表
谈谈对不同I/O模型的理解 (阻塞/非阻塞IO,同步/异步IO)
ucore操作系统学习(四) ucore lab4内核线程管理
ucore操作系统学习(三) ucore lab3虚拟内存管理分析
热门文章
手把手绕过安全狗
ArrayList与LinkedList遍历操作问题
java集合源码分析(四):LinkedList
java集合源码分析(三):ArrayList
java集合源码分析(二):List与AbstractList
java集合源码分析(一):Collection 与 AbstractCollection
Redis在SpringBoot的基本使用
记一次使用策略模式优化代码的经历
设计模式(二):策略模式
Mysql初探:数据库表空间的回收
Copyright © 2011-2022 走看看