Java导出Excel和CSV(简单Demo)

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

JavaBean

 1 public class ReportInfo {
 2     
 3     int id;              
 4     String date; 
 5     int num;         
 6     int percent;      
 7     
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getDate() {
15         return date;
16     }
17     public void setDate(String date) {
18         this.date = date;
19     }
20     public int getNum() {
21         return num;
22     }
23     public void setNum(int num) {
24         this.num = num;
25     }
26     public int getPercent() {
27         return percent;
28     }
29     public void setPercent(int percent) {
30         this.percent = percent;
31     }
32     
33 }
View Code

工具类的简单实现

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);
    }
    
}
View Code

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

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

原文地址:https://www.cnblogs.com/waimai/p/3410549.html