Java模块 -- Java 导出Excel

来源:互联网 发布:程序员项目介绍范文 编辑:程序博客网 时间:2024/06/03 21:52

Java 导出 Excel文件 小Demo

用的是 POI ,jar包百度上很好...这里就不多说了...

代码结构图:



students 实体类

package com.code.model;import java.util.Date;/** * Created by CYX on 2016/5/30. */public class Students {    private int id;    private String name;    private int age;    private Date birth;    @Override    public String toString() {        return "Students{" +                "id=" + id +                ", name='" + name + '\'' +                ", age=" + age +                ", birth=" + birth +                '}';    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    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 Date getBirth() {        return birth;    }    public void setBirth(Date birth) {        this.birth = birth;    }    public Students() {    }    public Students(int id, String name, int age, Date birth) {        this.id = id;        this.name = name;        this.age = age;        this.birth = birth;    }}


CreateSimpleExcelToDisk

package com.code.excel;import com.code.model.Students;import org.apache.poi.hssf.usermodel.*;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;/** * Created by CYX on 2016/5/30. * *      常用组件 *          HSSFWorkbook : excel的文档对象 *          HSSFSheet : Excel的表单 *          HSSFRow : Excel的行 *          HSSFCell : Excel的格子单元 *          HSSFFont : excel的字体 *          HSSFDateFormat : 日期格式 *          HSSFHeader : sheet头 *          HSSFFooter : sheet尾 * *      样式 *          HSSFCellStyle : cell样式 * *      辅助操作 *          HSSFDateUtil : 日期 *          HSSFPrintSetup : 打印 *          HSSFErrorConstant : 错误信息表 * */public class CreateSimpleExcelToDisk {    public static void main(String[] args) throws Exception{        //第一步,创建HSSFWorkbook对象,对应的一个Excel文件        HSSFWorkbook wb = new HSSFWorkbook();        //第二步, (创建HSSFSheet对象) 在webbook中添加一个sheet,对应Excel文件中的sheet        HSSFSheet sheet = wb.createSheet("学生表一");        //第三步, (创建HSSFRow对象) 在sheet中添加表头第0行,注意 老版本的poi对Excel的行数列数有限制short        HSSFRow row = sheet.createRow((int) 0);        //第四步,创建单元格,并设置表头,设置表头居中        HSSFCellStyle style = wb.createCellStyle();        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //创建一个居中格式        //(创建HSSFCell对象)        HSSFCell cell = row.createCell((short) 0);        cell.setCellValue("学号");        cell.setCellStyle(style);        cell = row.createCell((short) 1);        cell.setCellValue("姓名");        cell.setCellStyle(style);        cell = row.createCell((short) 2);        cell.setCellValue("年龄");        cell.setCellStyle(style);        cell = row.createCell((short) 3);        cell.setCellValue("生日");        cell.setCellStyle(style);        //第五步,写入实体数据,实际应用中这些数据从数据库中得到        List list = CreateSimpleExcelToDisk.getStudents();        for (int i = 0; i < list.size(); i++) {            row = sheet.createRow((int) i + 1);            Students students = (Students) list.get(i);            //第四步,创建单元格,并设置值            row.createCell((short)0).setCellValue((double)students.getId());            row.createCell((short) 1).setCellValue(students.getName());            row.createCell((short) 2).setCellValue((double)students.getAge());            cell = row.createCell((short) 3);            cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(students.getBirth()));        }        //第六步,将文件存到指定位置        FileOutputStream fout = new FileOutputStream("D:/students.xls");        wb.write(fout);        fout.close();    }    //手工构建一个简单格式的Excel    private static List<Students> getStudents() throws Exception{        List list = new ArrayList();        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");        Students user1 = new Students(1,"张三",16,sdf.parse("1997-03-12"));        Students user2 = new Students(1,"李四",17,sdf.parse("1997-03-13"));        Students user3 = new Students(1,"王五",18,sdf.parse("1997-03-14"));        Students user4 = new Students(1,"赵六",19,sdf.parse("1997-03-15"));        Students user5 = new Students(1,"钱七",20,sdf.parse("1997-03-16"));        list.add(user1);        list.add(user2);        list.add(user3);        list.add(user4);        list.add(user5);        return list;    }}

然后直接跑就好了...






代码参考:  http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html


0 0
原创粉丝点击