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(); } } }
查看全文
相关阅读:
章程系统管理明天软考的童鞋进来顶起!!!
线程任务java并发包小结(二)
模板统计LA 4670 Dominating Patterns
背包问题选中递归求解0 1背包问题
转发forwardingiptables remote port forwarding
函数用户PHP自学之路错误及异常处理机制、错误日志
内存图片内存溢出和泄漏
命令视频Matlab下查看摄像头设备信息
JAVA数据格式化
数字转大写(java)
原文地址:https://www.cnblogs.com/wzh123/p/3473767.html
最新文章
android 读取SD卡或者其他地方文件功能函数
APK签名导出向导
android 程序中判断当前是否连接网络,网络是否可用
当前Windows7防挂马较其他系统要好 狼人:
页面片段缓存(一) 狼人:
无公害认证让浠水养鸡户尝到甜头 狼人:
用C#实现HTTP协议下的多线程文件传输 狼人:
McAfee称微软推杀毒软件意在加强反盗版力度 狼人:
SaaS应用程序存安全隐患 或泄露敏感数据 狼人:
商业周刊:用户不愿共享资料被Facebook出卖 狼人:
热门文章
回顾.NET Remoting分布式开发 狼人:
瑞星搜狐畅游合作 “云安全”首次嵌入网游客户端 狼人:
改善代码设计 —— 处理概括关系(Dealing with Generalization) 狼人:
.NET :静态类的理解 狼人:
广东电信就断网事件致歉 人为攻击可能性大 狼人:
了解并解决云计算合规性问题 狼人:
黑客产业链年产值数亿元 大学生沦为后备军 狼人:
浠水扶助机制“孵出”全国养鸡大县 狼人:
代码开发者web开发不容错过的20段css代码
文件属性windows server 2008的NTFS文件系统管理
Copyright © 2011-2022 走看看