POI演示

Apache POI - the Java API for Microsoft Documents

可以导出exel文件

pom.xml

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>3.10-FINAL</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>3.10-FINAL</version>
</dependency>

Person.java

package com.luheng.poidemo;

public class Person {
    private String name;

    private String gender;

    private String address;

    public Person() {
    }

    public Person(String name, String gender, String address) {
        this.name = name;
        this.gender = gender;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

PoiController.java

@Controller
@RequestMapping("/poi")
public class PoiController {

    @RequestMapping("/export")
    public void export(HttpServletResponse response){
        //适用List集合造一些数据作为要导出的数据
        List<Person> list = new ArrayList<Person>();
        list.add(new Person("小明", "男", "北京"));
        list.add(new Person("小亮", "男", "天津"));
        list.add(new Person("小王", "男", "上海"));
        //调用方法创建HSSFWorkbook工作簿对象
        HSSFWorkbook wb = exportText(list);
        try {
            //定义导出文件的名称,看不懂的同学可以先行了解一下文件下载
            String fileName = new String("personRelation.xls".getBytes("UTF-8"),"ISO-8859-1");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition","attachment; filename="+fileName);
            OutputStream os = response.getOutputStream();

            //将工作薄写入到输出流中
            wb.write(os);
            os.close();
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

    //创建标题行样式
    public HSSFCellStyle headStyle(HSSFWorkbook wb){
        HSSFCellStyle headStyle = wb.createCellStyle();                       //创建样式对象
        HSSFFont headFont = wb.createFont();                                  //创建字体
        headFont.setFontName("微软雅黑");
        headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headFont.setColor(HSSFFont.COLOR_RED);

        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headStyle.setFont(headFont);
        return headStyle;
    }

    //创建内容行样式
    public static HSSFCellStyle contentStyle(HSSFWorkbook wb){
        HSSFCellStyle contentStyle = wb.createCellStyle();
        HSSFFont contentFont = wb.createFont();
        contentFont.setFontName("微软雅黑");
        contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        contentFont.setColor(HSSFFont.COLOR_NORMAL);

        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        contentStyle.setFont(contentFont);
        return contentStyle;
    }

    //创建HSSFWorkbook工作薄对象
    public  HSSFWorkbook exportText(List<Person> list){
        try {
            //创建工作薄对象
            HSSFWorkbook wb = new HSSFWorkbook();
            //创建标题行样式
            HSSFCellStyle headStyle = headStyle(wb);
            //创建内容行样式
            HSSFCellStyle contentStyle = contentStyle(wb);

            //创建表
            HSSFSheet sheet_1 =  wb.createSheet("人员信息");
            //设置表的默认列宽
            sheet_1.setDefaultColumnWidth(30);

            //创建标题行
            HSSFRow headRow = sheet_1.createRow(0);
            HSSFCell head_cell_1 = headRow.createCell(0);           //创建标题行第一列
            head_cell_1.setCellValue("姓名");                        //第一列内容
            head_cell_1.setCellStyle(headStyle);                    //将标题行样式添加

            HSSFCell head_cell_2 = headRow.createCell(1);
            head_cell_2.setCellValue("性别");
            head_cell_2.setCellStyle(headStyle);

            HSSFCell head_cell_3 = headRow.createCell(2);
            head_cell_3.setCellValue("地址");
            head_cell_3.setCellStyle(headStyle);

            //为内容行添加数据和样式
            for (int i = 1; i <= list.size(); i++) {
                HSSFRow contentRow = sheet_1.createRow(i);
                HSSFCell content_cell_1 = contentRow.createCell(0);
                content_cell_1.setCellValue(list.get(i-1).getName());
                content_cell_1.setCellStyle(contentStyle);

                HSSFCell content_cell_2 = contentRow.createCell(1);
                content_cell_2.setCellValue(list.get(i-1).getGender());
                content_cell_2.setCellStyle(contentStyle);

                HSSFCell content_cell_3 = contentRow.createCell(2);
                content_cell_3.setCellValue(list.get(i-1).getAddress());
                content_cell_3.setCellStyle(contentStyle);
            }
            return wb;
        } catch (Exception e) {
            e.getStackTrace();
        }
        return null;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值