package com.experot;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
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;
public class ExperotExcel {
/**
* @param args
* @throws ParseException
* @throws IOException
*/
@SuppressWarnings({ "deprecation", "unchecked" })
public static void main(String[] args) throws Exception {
//第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb=new HSSFWorkbook();
//第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet=wb.createSheet("当日业务报表");
//第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row=sheet.createRow((short)0);
//第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style=wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //创建一个居中格式
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);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List list=ExperotExcel.getStudent();
for(int i=0;i<list.size();i++)
{
row=sheet.createRow((int)(i+1));// 创建行的数据
Student student=(Student)list.get(i);
// 第四步,创建单元格,并设置值表头 的值
// HSSFCell cell1=row.createCell((short)0);
cell=row.createCell((short)0);
cell.setCellValue((int)student.getId());
cell=row.createCell((short)1);
cell.setCellValue((String)student.getName());
cell=row.createCell((short)2);
cell.setCellValue((int)student.getAge());
cell=row.createCell((short)3);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(student.getBirth()));
// row = sheet.createRow((int)i+1);
// Student stu = (Student) list.get(i);
// //第四步,创建单元格,并设置值
// 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()));
}
try {
FileOutputStream outputStream=new FileOutputStream("E:/student.xls");
wb.write(outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static List<Student> getStudent() throws Exception
{
List stuList=new ArrayList<Student>();
SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd");
Student student1=new Student(1,"张三",18,df.parse("1987-12-25"));
Student student2=new Student(2,"李四",19,df.parse("1997-01-25"));
Student student3=new Student(3,"王五",20,df.parse("1907-01-25"));
Student student4=new Student(4,"二师兄",21,df.parse("1977-12-25"));
stuList.add(student1);
stuList.add(student2);
stuList.add(student3);
stuList.add(student4);
return stuList;
}
}
/////
循环里两种都可以用,
这里是每列的创建,上面是为了简写,才写在row,到最后才用cell来结束
分享到:
相关推荐
Java 实现导出excel表 POI 所需包
java实现导出excel表 - Java - Web 开发.mhtjava实现导出excel表 - Java - Web 开发.mht
本资源实现了Excel的简单导出实例,同时也实现了导出Excel复合表头的简单实例,适合一些需要用到导出Excel技术的初学者。
Domino notes java版导出excel,需要组件poi支持,解决Domino日常数据批量导出需求!
用java导出一张excel表,一个ExportExcel工具类用来生成excel表,ComplexExportExcelClient报表生成类
java实现导出excel功能,可稍作修改移值到网页上进行数据导出
java导出Excel 时候 相同行合并(可合并多列); 已测试通过;
支持从数据库中查询到数据,以多级表头形式导入到Excel文件中。比如:一级表头:学号、姓名、各科成绩。二级表头:语文、数学、英语(二级表头在各科成绩下面)。
用Java语言采用接口设计模式、代理设计模式、工厂设计模式实现对excel中的表实现基本的导出(写)功能,把一个对象集合输出到一个excel表中。 【基本功能】 将对象集合导出到Excel表 【扩展功能】 1、自由设置表头 2...
该工具类实现java导出树形结构的方法,并未采用excel分组功能实现,而是根据树节点显示层级设置excel样式。针对easyUi,treeGrid开发的导出excel功能。方法简便实用、性强、通俗易懂。项目中亲测,no problem。
java导出excel POI jar包 java代码地址http://blog.csdn.net/awenluck/article/details/51488537
java工程,源代码,可直接运行,需自己改造成web下载方式。
java 数据库 导出 excel
java导出excel超过65536条记录出错 java导出excel超过65536条记录出错 java导出excel超过65536条记录出错 java导出excel超过65536条记录出错
资源描述:一个使用java做的导出数据,并且可以在excel表生成折线图的工具类,使用简单操作方便。
java导出excel
java实现excel的导入导出(poi详解),经过测试poi效率要比jxl要高很多,特别是数据量大的时候jxl根本无法用肉眼入目,本资源是个可运行项目demo,很有参考价值!
java Excel导出 Excel数据处理
java的进销存例子,很好用的,是我们小组一起做的,大家一起研究下。