java 创建一个简单的excel 表代码

来源:互联网 发布:算法 第4版 pdf 高清 编辑:程序博客网 时间:2024/06/16 18:18
1:首先下载poi-3.6-20091214.jar放到lib中,下载地址:

http://download.csdn.net/detail/evangel_z/3895051

2:创建Student.java,代码如下:

import java.util.Date;

public class Student {

private int id;
private String name;
private int age;
private Date birth;

public Student() {
}

public Student(int id, String name, int age, Date birth) {
this.id = id;
this.name = name;
this.age = age;
this.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;
}
}

3:创建CreateSimpleExcelToDisk.java,代码如下:

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


/**
 * 创建一个简单的excel
 * @author Administrator
 * 
 */
public class CreateSimpleExcelToDisk {

private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

private static List<Student> getStudent() throws Exception {
List<Student> list = new ArrayList<Student>();
String time =sdf.format(new Date()); 
Date date = sdf.parse(time); //系统当前时间 把String类型的时间转换为Date类型
Student stu1 = new Student(1, "张三", 20, date);
Student stu2 = new Student(2, "李四", 22, date);
Student stu3 = new Student(3, "王五", 18, date);
list.add(stu1);
list.add(stu2);
list.add(stu3);
return list;
}

public static void main(String[] args) throws Exception {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook excel = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = excel.createSheet("studentTable");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short。 createCell(short columnIndex) 弃用, (2008年8月)使用createCell(int)。 
HSSFRow row = sheet.createRow((int) 0);  //创建行
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = excel.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = row.createCell((int)0); //创建列
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell((int) 1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((int) 2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell((int) 3);
cell.setCellValue("生日");
cell.setCellStyle(style);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List list = CreateSimpleExcelToDisk.getStudent();  
for (int i = 0; i < list.size(); i++){
row = sheet.createRow((int) i + 1);
Student stu = (Student) list.get(i);
// 第四步,创建单元格,并设置值   
row.createCell((int) 0).setCellValue((double) stu.getId());
row.createCell((int) 1).setCellValue(stu.getName());
row.createCell((int) 2).setCellValue((double) stu.getAge());
cell = row.createCell((int) 3);
cell.setCellValue(sdf.format(stu.getBirth()));
}  
// 第六步,将文件存到指定位置
try  
       {  
          FileOutputStream fout = new FileOutputStream("E:/students.xls");  
          excel.write(fout);  
            fout.close();  
        }  
        catch (Exception e)  
        {  
           e.printStackTrace();  
       }  
}
}

0 0