apache POI 对Excel表的操作

 

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;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值