JAVA利用Apache Poi写Excel文件
2016-06-12 08:28
531 查看
接着上次的利用Apache Poi读Excel文件
http://blog.csdn.net/ankh_zxy/article/details/51636589
本次文章参考自
http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html
再次直奔主题
修改过的Student类
WriteExcel类
Client类进行测试
再次声明文章参考自:
http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html
谢谢。
http://blog.csdn.net/ankh_zxy/article/details/51636589
本次文章参考自
http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html
再次直奔主题
修改过的Student类
public class Student { private String no; private String name; private Date startTime; private long time; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public long getTime() { return time; } public void setTime(long time) { this.time = time; } public List<Student> getStudents() { List<Student> list = new ArrayList<Student>(); for (int i = 0; i < 4; i++) { Student student = new Student(); student.setNo("3140000" + i); student.setName("Student" + i); student.setStartTime(new Date()); student.setTime(900L + i); list.add(student); } return list; } }
WriteExcel类
public class WriteExcel { public void createExcel(List<Student> list) { //创建一个webbook,对应一个Excel文件 XSSFWorkbook wb = new XSSFWorkbook(); //在webbook中添加一个sheet,对应Excel文件中的sheet XSSFSheet sheet = wb.createSheet("学生表一"); //在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short XSSFRow row = sheet.createRow(0); //创建单元格,并设置值表头 设置表头居中 XSSFCellStyle style = wb.createCellStyle(); style.setAlignment(XSSFCellStyle.ALIGN_CENTER); XSSFCell 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); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (int i = 0; i < list.size(); i++) { row = sheet.createRow(i + 1); Student student = list.get(i); row.createCell(0).setCellValue(student.getNo()); row.createCell(1).setCellValue(student.getName()); row.createCell(2).setCellValue(sdf.format(student.getStartTime())); String time = String.valueOf((student.getTime() - student.getTime() % 60) / 60 + "m" + student.getTime() % 60 + "s"); row.createCell(3).setCellValue(time); } try { sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String fileName = sdf.format(new Date()); FileOutputStream fos = new FileOutputStream("E:\\test\\" + fileName + ".xls"); wb.write(fos); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }
Client类进行测试
public class Client { public static void main(String[] args) throws IOException { WriteExcel writeExcel = new WriteExcel(); Student student = new Student(); writeExcel.createExcel(student.getStudents()); } }
再次声明文章参考自:
http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html
谢谢。
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树