生成ExcelAnd创建数据
package com.ninemax.common.utils;
import java.text.SimpleDateFormat;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.ninemax.common.model.ExcelModel;
/**
* 生成Excel表格
*
* @author Darker
*
*/
public class ExcelUtil {
/**
* @throws Exception
*
* @功能:Excel存放数据
*/
public static List<ExcelModel> getExcelModel() throws Exception{
List<ExcelModel> list= new ArrayList<ExcelModel>();
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
ExcelModel user1= new ExcelModel(1,"one",16,df.parse("2015-12-1"));
ExcelModel user2= new ExcelModel(1,"two",16,df.parse("2015-12-2"));
ExcelModel user3= new ExcelModel(1,"three",16,df.parse("2015-12-3"));
list.add(user1);
list.add(user2);
list.add(user3);
return list;
}
/**
* 创建Excel
*
* @return
* @throws Exception
*/
public static HSSFWorkbook CreateExcel() throws Exception {
//1.创建一个workbook,对应一个Excel文件
HSSFWorkbook wb= new HSSFWorkbook();
//2.在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet=wb.createSheet("学生表一");
//3.在sheet中添加表头第0行
HSSFRow row= sheet.createRow((int)0);
//4.创建单元格,并且设置表头,设置表头居中
HSSFCellStyle style=wb.createCellStyle();
//4.1格式居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//4.2单元格信息设置
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);
//5.写入实体数据
List<ExcelModel>list= ExcelUtil.getExcelModel();
for(int i=0;i<list.size();i++){
row=sheet.createRow((int)i+1);
ExcelModel stu=(ExcelModel)list.get(i);
//4.3单元格信息值填写
row.createCell((short)0).setCellValue((double)stu.getId());
row.createCell((short)1).setCellValue(stu.getName());
row.createCell((short)2).setCellValue((double)stu.getAge());
cell=row.createCell((short)3);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBirth()));
}
return wb;
}
}
Model类(如果不是测试用,可以插入数据库的数据)
package com.ninemax.common.model;
import java.util.Date;
/**
*
* @author Darker
*
*/
public class ExcelModel {
private int id;
private String name;
private int age;
private Date birth;
public ExcelModel() {
}
public ExcelModel(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;
}
}
启动方法
package com.ninemax.common.starter;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.ninemax.common.utils.ExcelUtil;
public class ExcelStarter {
/**
* 启动方法
*
* @param args
*
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//调用创建Excel的方法
HSSFWorkbook wb = ExcelUtil.CreateExcel();
//将文件保存到指定位置
FileOutputStream fous= new FileOutputStream("E:/students.xls");
//将设置的内容写入到Excel文件中
wb.write(fous);
//关闭输出流
fous.close();
}
}