package zk.excel;
import java.io.FileOutputStream;
import java.io.OutputStream;
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;
public class Excel
{
public static void main(String[] args) throws Exception
{
Student student = new Student("张凯", "123456", "男", 23,
"7095638@qq.com");
HSSFWorkbook excel = new HSSFWorkbook();
HSSFSheet sheet = excel.createSheet("用户信息");
HSSFRow titleRow = sheet.createRow(0);
sheet.setColumnWidth(4,5000);
HSSFCell titleCell0 = titleRow.createCell(0);
titleCell0.setCellValue("姓名");
HSSFCell titleCell1 = titleRow.createCell(1);
titleCell1.setCellValue("密码");
HSSFCell titleCell2 = titleRow.createCell(2);
titleCell2.setCellValue("性别");
HSSFCell titleCell3 = titleRow.createCell(3);
titleCell3.setCellValue("年龄");
HSSFCell titleCell4 = titleRow.createCell(4);
titleCell4.setCellValue("Email");
HSSFRow valueRow = sheet.createRow(1);
HSSFCell valueCell0 = valueRow.createCell(0);
valueCell0.setCellValue(student.getName());
HSSFCell valueCell1 = valueRow.createCell(1);
valueCell1.setCellValue(student.getPassword());
HSSFCell valueCell2 = valueRow.createCell(2);
valueCell2.setCellValue(student.getGender());
HSSFCell valueCell3 = valueRow.createCell(3);
valueCell3.setCellValue(student.getAge());
HSSFCell valueCell4 = valueRow.createCell(4);
valueCell4.setCellValue(student.getEmail());
OutputStream os = new FileOutputStream("excel.xls");
excel.write(os);
}
}
//要写入到Excel表中的类
class Student
{
private String name;
private String password;
private String gender;
private int age;
private String Email;
public Student(String name, String password, String gender, int age,
String email)
{
this.name = name;
this.password = password;
this.gender = gender;
this.age = age;
Email = email;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
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 getEmail()
{
return Email;
}
public void setEmail(String email)
{
Email = email;
}
}