java 创建一个简单的excel 表代码
来源:互联网 发布:算法 第4版 pdf 高清 编辑:程序博客网 时间:2024/06/16 18:18
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();
}
}
}
- java 创建一个简单的excel 表代码
- 利用poi简单的创建一个excel
- 在Java导出数据创建excel表时候出现json对话框或者出现一个带有js代码的东西
- 创建存储过程的一个简单代码
- 创建一个简单的代码发布服务
- 简单的java读取excel表中的数据代码
- 在PowerShell中操作Excel - 创建一个简单的报表
- 使用 POI创建一个简单的 Excel 文件
- java在Java代码中创建Excel表格并进行插入查看表格里面一个单元格的内容
- Java Excel操作工具类,创建一个Excel,只需一行代码。
- 用java创建一个简单的服务器
- JAVA创建一个登录面板的代码
- 一个简单的java线程代码
- 一个简单的Java网络编程代码
- 一个简单的图片截取代码 java
- 一个简单的java Thread代码
- 【代码示例】 一个简单的Java死锁
- 一个java代码生成器的简单实现
- MissingInteger
- DataTable相关:DataTable排序、查询、合并操作
- java实现转盘抽奖;
- Unity 之 Sharder 通道遮罩 ColorMask - 九
- 测试
- java 创建一个简单的excel 表代码
- 蓝桥杯 结果填空 黄金队列
- 数据挖掘回顾六:分类算法之 AdaBoost 集成算法
- python处理http请求中的protobuf
- 虚拟机连接不上网络
- UIViewController各个方法的加载顺序
- Oracle 查询表信息(字段+备注)
- 左上三角蛇形矩阵
- Front end foundation course 3(css1)