CODE:
package com.way.poi.writetoexcel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.way.poi.bean.Student;
/**
*
* @author fenzaiway
*
* 模拟数据,把数据写入到Excel中、
*
*/
public class GenerateExcel {
private static String xlsPath = "D:\\student.xls";
private static List<Student> studentList = null;
private static Student[] students = new Student[4];
/**
* 静态块初始化数据
*/
static{
studentList = new ArrayList<Student>();
students[0] = new Student("张三","男",23,"一班",94);
students[1] = new Student("李四","女",22,"二班",90);
students[2] = new Student("王五","男",21,"五班",84);
students[3] = new Student("赵六","女",24,"三班",98);
studentList.addAll(Arrays.asList(students));
}
/**
* 主函数
*/
public static void main(String[] args) {
long start = System.currentTimeMillis();
generateExcel(xlsPath);
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end -start) + "毫秒");
}
//创建Excel文件
public static void generateExcel(String path){
//创建工作簿对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建工作表对象并命名
HSSFSheet sheet = wb.createSheet("学生信息统计表");
//遍历集合对象创建行和单元格
for(int i = 0; i < studentList.size(); i++){
//取出Student对象
Student student = studentList.get(i);
//创建行
HSSFRow row = sheet.createRow(i);
//创建单元格并赋值
HSSFCell nameCell = row.createCell(0);
nameCell.setCellValue(student.getName());
HSSFCell genderCell = row.createCell(1);
genderCell.setCellValue(student.getGender());
HSSFCell ageCell = row.createCell(2);
ageCell.setCellValue(student.getAge());
HSSFCell sclassCell = row.createCell(3);
sclassCell.setCellValue(student.getSclass());
HSSFCell scoreCell = row.createCell(4);
sclassCell.setCellValue(student.getScore());
}
//生成文件
File file = new File(path);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
wb.write(fos);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
*
* @author fenzaiway
*
* 模拟数据,把数据写入到Excel中、
*
*/
public class GenerateExcel {
private static String xlsPath = "D:\\student.xls";
private static List<Student> studentList = null;
private static Student[] students = new Student[4];
/**
* 静态块初始化数据
*/
static{
studentList = new ArrayList<Student>();
students[0] = new Student("张三","男",23,"一班",94);
students[1] = new Student("李四","女",22,"二班",90);
students[2] = new Student("王五","男",21,"五班",84);
students[3] = new Student("赵六","女",24,"三班",98);
studentList.addAll(Arrays.asList(students));
}
/**
* 主函数
*/
public static void main(String[] args) {
long start = System.currentTimeMillis();
generateExcel(xlsPath);
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end -start) + "毫秒");
}
//创建Excel文件
public static void generateExcel(String path){
//创建工作簿对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建工作表对象并命名
HSSFSheet sheet = wb.createSheet("学生信息统计表");
//遍历集合对象创建行和单元格
for(int i = 0; i < studentList.size(); i++){
//取出Student对象
Student student = studentList.get(i);
//创建行
HSSFRow row = sheet.createRow(i);
//创建单元格并赋值
HSSFCell nameCell = row.createCell(0);
nameCell.setCellValue(student.getName());
HSSFCell genderCell = row.createCell(1);
genderCell.setCellValue(student.getGender());
HSSFCell ageCell = row.createCell(2);
ageCell.setCellValue(student.getAge());
HSSFCell sclassCell = row.createCell(3);
sclassCell.setCellValue(student.getSclass());
HSSFCell scoreCell = row.createCell(4);
sclassCell.setCellValue(student.getScore());
}
//生成文件
File file = new File(path);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
wb.write(fos);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
bean:
package com.way.poi.bean;
public class Student {
private String name;
private String gender;
private int age;
private String sclass;
private int score;
public Student(){
super();
}
public Student(String name, String gender, int age, String sclass, int score) {
super();
this.name = name;
this.gender = gender;
this.age = age;
this.sclass = sclass;
this.score = score;
}
private String gender;
private int age;
private String sclass;
private int score;
public Student(){
super();
}
public Student(String name, String gender, int age, String sclass, int score) {
super();
this.name = name;
this.gender = gender;
this.age = age;
this.sclass = sclass;
this.score = score;
}
public String toString(){
return "Student [age= " + age +",gender= " + gender +",name= " + name +",sclass= " + sclass +",score= " + score +"]";
}
return "Student [age= " + age +",gender= " + gender +",name= " + name +",sclass= " + sclass +",score= " + score +"]";
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSclass() {
return sclass;
}
public void setSclass(String sclass) {
this.sclass = sclass;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSclass() {
return sclass;
}
public void setSclass(String sclass) {
this.sclass = sclass;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}