码迷,mamicode.com
首页 > 编程语言 > 详细

java使用poi生成Excel文件

时间:2018-06-02 19:39:46      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:length   南京   test   路径   lis   package   用户   for   color   

1. maven导入poi包:

  

技术分享图片
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
pom.xml

 

2. 新建测试数据实体类:

  

技术分享图片
package com.clz.testexportexcel;

public class ExcelModel {

    private String name;
    private int age;
    private String phone;
    private String address;
    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 String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public ExcelModel(String name, int age, String phone, String address) {
        super();
        this.name = name;
        this.age = age;
        this.phone = phone;
        this.address = address;
    }
    public ExcelModel() {
        super();
    }
    
}
ExcelModel

3. 新建工具类及测试

技术分享图片
package com.clz.testexportexcel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

/**
 * 生成导出excel
 * @author Administrator
 *
 */
public class TestExportExcel {

    public static void main(String[] args) {
        
        String exportFileName = "testExcel.xls";
        String exportFilePath = "E:\\testExcel";
        List<ExcelModel> excelModels = new ArrayList<ExcelModel>();
        ExcelModel em1 = new ExcelModel("张三", 18, "123456", "上海");
        ExcelModel em2 = new ExcelModel("李四", 20, "456789", "南京");
        ExcelModel em3 = new ExcelModel("王五", 22, "987412", "杭州");
        excelModels.add(em1);
        excelModels.add(em2);
        excelModels.add(em3);
        String exportExcel = exportExcel(excelModels, exportFilePath, exportFileName);
        System.out.println(exportExcel);
        
    }
    
    /**
     * 导出excel
     * @param excelModel        数据实体类
     * @param exportFilePath    导出文件路径
     * @param exportFileName    导出文件名称
     * @return 生成文件路径
     */
    public static String exportExcel(List<ExcelModel> excelModels, String exportFilePath, String exportFileName) {
        
        String fileUrl = null;
        HSSFWorkbook wb = null;
        OutputStream os = null;
        try {
            // 判断文件夹是否存在,不存在就创建
            File fileDir = new File(exportFilePath);
            if(!fileDir.exists()) {
                fileDir.mkdirs();
            }
            /**
             *  生成excel文件
             */
            // 第一步,创建一个webbook,对应一个Excel文件
            wb = new HSSFWorkbook();
            // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet("用户信息导出");
            // 第三步,在sheet中添加表头第0行
            HSSFRow row = sheet.createRow((int) 0);
            // 第四步,创建单元格,并设置值表头 设置表头居中
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER); // 居中格式
            HSSFFont font = wb.createFont();
            font.setColor(HSSFFont.COLOR_RED);//HSSFColor.VIOLET.index //字体颜色
            //把字体应用到当前的样式
            style.setFont(font);
            HSSFCell cell=null;
            //表头(与实体类数据一一对应)
            String[] headers={"姓名", "年龄", "电话", "地址"};
            for(int i=0;i<headers.length;i++){
                cell = row.createCell((short) i);
                cell.setCellValue(headers[i]);
                cell.setCellStyle(style);
            }
            // 写数据到excel
            for (int i = 0; i < excelModels.size(); i++) {
                ExcelModel em = excelModels.get(i);
                // 创建行(从第二行开始)
                row = sheet.createRow((int) i + 1); 
                // 创建列并赋值
                row.createCell(0).setCellValue(em.getName());
                row.createCell(1).setCellValue(em.getAge());
                row.createCell(2).setCellValue(em.getPhone());
                row.createCell(3).setCellValue(em.getAddress());
            }
            // 写入文件
            fileUrl = exportFilePath + File.separator + exportFileName;
            os = new FileOutputStream(fileUrl);
            wb.write(os);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(wb != null) {
                try {
                    wb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if(fileUrl != null && new File(fileUrl).exists()) {
            return fileUrl;
        }
        return null;
    }
    
}
TestExportExcel

 

java使用poi生成Excel文件

标签:length   南京   test   路径   lis   package   用户   for   color   

原文地址:https://www.cnblogs.com/changlezhong/p/9126262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!