导出Excel
2016-04-15 09:55
274 查看
package com.excel.export; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExportExcel2 { public static void main(String[] args) throws Exception { List<String> title = new ArrayList<String>(); title.add("id"); title.add("name"); title.add("sex"); List<List<String>> contents = new ArrayList<List<String>>(); for(int i=1;i<11;i++){ List<String> list = new ArrayList<String>(); list.add(i+""); list.add("James_"+i); list.add("man"); contents.add(list); } if (title == null) { throw new Exception("导出表头不能为空"); } if (contents != null && contents.get(0).size() != title.size()) { throw new Exception("表头和行数据不对称"); } //创建Excel工作簿 XSSFWorkbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet(); Row row = sheet.createRow(0); Cell cell = null; //插入第一行数据 id,name,sex for(int i=0;i<title.size();i++){ cell = row.createCell(i); cell.setCellValue(title.get(i)); } //追加数据 for(int i=1;i<=contents.size();i++){ Row nextRow = sheet.createRow(i); for(int j=0;j<contents.get(i-1).size();j++){ Cell cell2 = nextRow.createCell(j); cell2.setCellValue(contents.get(i-1).get(j)); } } //创建一个文件 File file = new File("e:/content.xlsx"); try { file.createNewFile(); FileOutputStream fop = FileUtils.openOutputStream(file); workbook.write(fop); fop.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("------done----"); } }
http://blog.csdn.net/ptzrbin/article/details/8751293
相关文章推荐
- resizableImageWithCapInsets 图片拉伸
- Awk使用案例总结(运维必会)
- HTML5的新特性
- C# 模拟光照运算以及背面剔除
- 解决Android 6.0动态添加权限问题
- VC++如何将程序最小化到托盘
- base64
- 第五轮回 不要写函数
- WordPress 添加面包屑导航 Breadcrumb
- 命名空间在扩展方法中的妙用
- 修复VirtualBox "This kernel requires the following features not present on the CPU: pae Unable to boot
- 用Hexo搭建属于自己的Blog
- 内核内存池管理技术实现分析【转】
- 类加载机制
- 【转】android系统常用URI
- 实验5 数独游戏界面设计
- Python -- 大小写转换
- C++实现简单的信息管理系统
- 利用IPV6实现宿舍远程连接实验室并免费上网
- 图片转码为高度为5000,宽度为7300,报错内存溢出。修改配置后,转码正常