Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV。

JavaBean

 public class ReportInfo {

     int id;
String date;
int num;
int percent; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPercent() {
return percent;
}
public void setPercent(int percent) {
this.percent = percent;
} }

工具类的简单实现

public class ExportUtil {

    public static void main(String[] args) throws FileNotFoundException, IOException{

        List<ReportInfo> list = new ArrayList<ReportInfo>();
createData(list);
exportCSV(list, "/tmp/report.csv");
exportExcel(list, "/tmp/report.xls");
} public static void exportCSV(List<ReportInfo> list, String filePath) throws IOException{
File file = new File(filePath);
if(file.exists())
file.delete();
CsvWriter wr = new CsvWriter(filePath, ',', Charset.forName("UTF-8"));
String[] contents = {"No", "date", "num", "percent"};
wr.writeRecord(contents);
ReportInfo info = new ReportInfo();
for(int i = 0; i < list.size(); i++){
info = list.get(i);
contents[0] = String.valueOf(info.getId());
contents[1] = info.getDate();
contents[2] = String.valueOf(info.getNum());
contents[3] = info.getPercent() + "%";
wr.writeRecord(contents);
}
wr.close();
} public static void exportExcel(List<ReportInfo> list, String filePath) throws IOException{ File file = new File(filePath);//"/tmp/tmpfiles/workbook.xls"
if(file.exists())
file.delete();
FileOutputStream fileOut = new FileOutputStream(filePath);//创建excel表格//"/tmp/tmpfiles/workbook.xls"
Workbook wb = new HSSFWorkbook();//获取workbook
//FileOutputStream fileOut = new FileOutputStream("workbook.xls");
HSSFSheet sheet = (HSSFSheet) wb.createSheet("report");// 生成一个表格 sheet.setColumnWidth(1, 4000); HSSFRow row = sheet.createRow((short)0);//创建行并插入表头
row.createCell(0).setCellValue("No");
row.createCell(1).setCellValue("date");
row.createCell(2).setCellValue("num");
row.createCell(3).setCellValue("percent"); ReportInfo info = new ReportInfo();
for(int i = 1; i <= list.size(); i++){//循环插入数据
info = list.get(i-1);
row = sheet.createRow(i);
row.createCell(0).setCellValue(info.getId());
row.createCell(1).setCellValue(info.getDate());
row.createCell(2).setCellValue(info.getNum());
row.createCell(3).setCellValue(info.getPercent()+"%");
} wb.write(fileOut);
fileOut.close();
} public static void createData(List<ReportInfo> list){
ReportInfo tp = new ReportInfo();
tp.setId(1);
tp.setNum(2);
tp.setPercent(50);
tp.setDate("2013-08-20");
list.add(tp);
} }

后来看到xwdreamer一篇文章,使用了Java的泛型和反射将JavaBean的属性依次填充到Excel行中,如能自由指定JavaBean的属性所在列效果更好。

POI的官方快速入门示例:http://poi.apache.org/spreadsheet/quick-guide.html

Java导出Excel和CSV(简单Demo)的更多相关文章

  1. [转载]Java导出Excel

    一.需求介绍 当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开.或者是:用户已经习惯用E ...

  2. java导出excel报错:getOutputStream() has already been called for this response

    对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...

  3. java导出excel表格

    java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...

  4. java导出excel报表

    1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...

  5. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  6. java导出excel模板数据

    Java导出excel数据模板,这里直接贴代码开发,流程性的走下去就是步骤: String[] colName=new String[]{"期间","科目代码" ...

  7. java导出excel工具类

    java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...

  8. js原生导出excel和csv

    ​ 严格意义来说并不是真正的excel文件,只是可以用excel打开查看而已,实际上的格式是逗号分隔文件即csv文件. 这里有几个坑要说一下: 不加Unicode的utf8头部标识excel打开文件会 ...

  9. Java代码导入导出 Excel 表格最简单的方法

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...

随机推荐

  1. JavaScript高级程序设计17.pdf

    导航和打开窗口 使用window.open()方法可以导航到一个特定的URL也可以打开一个新的浏览器窗口,接收4个参数:要加载的URL.窗口目标.特性字符串和一个表示新页面是否取代浏览器历史记录中当前 ...

  2. Poj 2887-Big String Splay

    题目:http://poj.org/problem?id=2887       Big String Time Limit: 1000MS   Memory Limit: 131072K Total ...

  3. spring maven pom

    https://spring.io/blog/2009/12/02/obtaining-spring-3-artifacts-with-maven/

  4. hdu 4403 枚举

    #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #inclu ...

  5. poj 1064 Cable master【浮点型二分查找】

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29554   Accepted: 6247 Des ...

  6. 某外企mono for android试题

    Hitcents C#Programming Test This test is designed to evaluate generalC# and Xamarin development skil ...

  7. w10 系统升级

    怎么把电脑升级到w10系统? 下载一个软件,Windows10Upgrade9252.exe, 5M左右,把windows更新开启后,运行即可! 升级后,请把windows.old 文件夹删除,这个文 ...

  8. (转)PHP连接数据库之PHP连接MYSQL数据库代码

    PHP连接数据库之PHP连接MYSQL数据库代码 < ?php $mysql_server_name='localhost'; //改成自己的mysql数据库服务器 $mysql_usernam ...

  9. Delphi QQ表情的实现

    Delphi QQ表情的实现 QQ表情描述 蓝框 提示信息 鼠标在这个表情上面 这个表情才动 可以增加表情 表情打包 单击这个表情插入表情 关闭本窗体   主要使用Webbrowsr来实现的   -- ...

  10. [RxJS] Filtering operators: takeUntil, takeWhile

    take(), takeLast(), first(), last(), those opreators all take number or no param. takeUtil and takeW ...