项目结构同上一篇

泛型通用的写法

ExportExcel.java

package excel;

import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.util.HSSFColor; public class ExportExcel<T> {
public void exportExcel(String title, String[] headers, List<T> list, OutputStream out){
//声明一个工作薄
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//生成一个表格
HSSFSheet sheet = hssfWorkbook.createSheet(title);
//设置表格默认列宽度
sheet.setDefaultColumnWidth(15);
//生成一个样式
HSSFCellStyle style = hssfWorkbook.createCellStyle();
//设置样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//生成字体
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
//产生表格标题行
HSSFRow row = sheet.createRow(0);
for(int i = 0; i < headers.length; i++){
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
int index = 0;
for(T t: list){
index++;
row = sheet.createRow(index);
Field[] fields = t.getClass().getDeclaredFields();
for(int i = 0; i < fields.length; i++){
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
Field field = fields[i];
String fieldName = field.getName();
String getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try{
Class tCls = t.getClass();
Method getMethod = tCls.getMethod(getMethodName,
new Class[] {});
Object value = getMethod.invoke(t, new Object[] {});
String textValue = value.toString();
HSSFRichTextString richString = new HSSFRichTextString(textValue);
HSSFFont font3 = hssfWorkbook.createFont();
font3.setColor(HSSFColor.BLUE.index);
richString.applyFont(font3);
cell.setCellValue(richString);
}catch (Exception e) {
e.printStackTrace();
}
}
}
try{
hssfWorkbook.write(out);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} public void exportExcels(List<T> list, OutputStream out){
//声明一个工作薄
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//生成一个表格
HSSFSheet sheet = hssfWorkbook.createSheet();
//设置表格默认列宽度
sheet.setDefaultColumnWidth(20);
//生成一个样式
HSSFCellStyle style = hssfWorkbook.createCellStyle();
//设置样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//生成字体
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
//产生表格标题行
HSSFRow row = sheet.createRow(0);
T x = list.get(0);
for(int i = 0; i < x.getClass().getDeclaredFields().length; i++){
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(x.getClass().getDeclaredFields()[i].getName());
cell.setCellValue(text);
}
int index = 0;
for(T t: list){
index++;
row = sheet.createRow(index);
Field[] fields = t.getClass().getDeclaredFields();
for(int i = 0; i < fields.length; i++){
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
Field field = fields[i];
String fieldName = field.getName();
String getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try{
Class tCls = t.getClass();
Method getMethod = tCls.getMethod(getMethodName,
new Class[] {});
Object value = getMethod.invoke(t, new Object[] {});
String textValue;
if(value == null){
continue;
}
if(value instanceof Date){
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
textValue = sdf.format(date);
}else{
textValue = value.toString();
}
HSSFRichTextString richString = new HSSFRichTextString(textValue);
HSSFFont font3 = hssfWorkbook.createFont();
font3.setColor(HSSFColor.BLUE.index);
richString.applyFont(font3);
cell.setCellValue(richString);
}catch (Exception e) {
// e.printStackTrace();
}
}
}
try{
hssfWorkbook.write(out);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

非泛型硬编码的写法:

package client;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import mysql.mapper.StudentMapper; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.util.HSSFColor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import station.mapper.StationApplyMapper; import excel.ExportExcel; import Student.StationApply;
import Student.StationApplyExample;
import Student.Student;
import Student.StudentExample; public class PoiDemo { public static void main(String[] args) throws IOException{
long t1 = System.currentTimeMillis();
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
StationApplyMapper stationApplyMapper = (StationApplyMapper) ctx.getBean("stationApplyMapper");
StationApplyExample stationApplyExample = new StationApplyExample();
List<StationApply> list = stationApplyMapper.selectByExample(stationApplyExample);
OutputStream out = new FileOutputStream("D://a.xls");
// new ExportExcel<Student>().exportExcel("test", headers, list, out);
// new ExportExcel<StationApply>().exportExcels(list, out);
exportExcels(list, out);
out.close();
System.out.println("success!");
long t2 = System.currentTimeMillis();
System.out.println(t2 - t1);
} public static void exportExcels(List<StationApply> list, OutputStream out){
//声明一个工作薄
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//生成一个表格
HSSFSheet sheet = hssfWorkbook.createSheet();
//设置表格默认列宽度
sheet.setDefaultColumnWidth(20);
//生成一个样式
HSSFCellStyle style = hssfWorkbook.createCellStyle();
//设置样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//生成字体
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
//产生表格标题行
HSSFRow row = sheet.createRow(0);
StationApply x = list.get(0);
for(int i = 0; i < x.getClass().getDeclaredFields().length; i++){
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(x.getClass().getDeclaredFields()[i].getName());
cell.setCellValue(text);
}
int index = 0;
for(StationApply t: list){
if(t == null){
continue;
}
index++;
row = sheet.createRow(index);
HSSFCell cell = row.createCell(0);
// SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
try{
HSSFRichTextString richString = new HSSFRichTextString(String.valueOf(t.getId()));
cell.setCellValue(richString);
cell = row.createCell(1);
richString = new HSSFRichTextString(String.valueOf(t.getGmtCreate()));
cell.setCellValue(richString);
cell = row.createCell(2);
richString = new HSSFRichTextString(String.valueOf(t.getGmtModified()));
cell.setCellValue(richString);
cell = row.createCell(3);
richString = new HSSFRichTextString(t.getCreator());
cell.setCellValue(richString);
cell = row.createCell(4);
richString = new HSSFRichTextString(t.getModifier());
cell.setCellValue(richString);
cell = row.createCell(5);
richString = new HSSFRichTextString(t.getIsDeleted());
cell.setCellValue(richString);
cell = row.createCell(6);
richString = new HSSFRichTextString(t.getIsDeleted());
cell.setCellValue(richString);
cell = row.createCell(7);
richString = new HSSFRichTextString(t.getName());
cell.setCellValue(richString);
cell = row.createCell(8);
richString = new HSSFRichTextString(t.getState());
cell.setCellValue(richString);
cell = row.createCell(9);
richString = new HSSFRichTextString(t.getApplierName());
cell.setCellValue(richString);
cell = row.createCell(10);
richString = new HSSFRichTextString(t.getIdenNum());
cell.setCellValue(richString);
cell = row.createCell(11);
richString = new HSSFRichTextString(t.getMobile());
cell.setCellValue(richString);
cell = row.createCell(12);
richString = new HSSFRichTextString(t.getCovered());
cell.setCellValue(richString);
cell = row.createCell(13);
richString = new HSSFRichTextString(t.getProducts());
cell.setCellValue(richString);
cell = row.createCell(14);
richString = new HSSFRichTextString(t.getLogisticsState());
cell.setCellValue(richString);
cell = row.createCell(15);
richString = new HSSFRichTextString(t.getDescription());
cell.setCellValue(richString);
cell = row.createCell(16);
richString = new HSSFRichTextString(t.getFormat());
cell.setCellValue(richString);
cell = row.createCell(17);
richString = new HSSFRichTextString(t.getAlipayAccount());
cell.setCellValue(richString);
cell = row.createCell(18);
richString = new HSSFRichTextString(t.getTaobaoNick());
cell.setCellValue(richString);
cell = row.createCell(19);
richString = new HSSFRichTextString(String.valueOf(t.getStationId()));
cell.setCellValue(richString);
cell = row.createCell(20);
richString = new HSSFRichTextString(String.valueOf(t.getOwnOrgId()));
cell.setCellValue(richString);
}catch (Exception e) {
e.printStackTrace();
}
}
try{
hssfWorkbook.write(out);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

测试耗时2s左右 测试数据10000条记录 每条记录20个字段

web实例http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html

数据库导出到excel的更多相关文章

  1. php将数据库导出成excel的方法

    <?php $fname = $_FILES['MyFile']['name']; $do = copy($_FILES['MyFile']['tmp_name'],$fname); if ($ ...

  2. 【Java EE 学习 17 下】【数据库导出到Excel】【多条件查询方法】

    一.导出到Excel 1.使用DatabaseMetaData分析数据库的数据结构和相关信息. (1)测试得到所有数据库名: private static DataSource ds=DataSour ...

  3. .Net之路(十三)数据库导出到EXCEL

    .NET中导出到Office文档(word,excel)有我理解的两种方法.一种是将导出的文件存放在server某个目录以下,利用response输出到浏览器地址栏,直接打开:还有直接利用javasc ...

  4. ThinkPHP中,运用PHPExcel,将数据库导出到Excel中

    1.将PHPExcel插件放在项目中,本人位置是ThinkPHP文件夹下,目录结构如下/ThinkPHP/Library//Vendor/...2.直接根据模型,配置三个变量即可使用./** * Ex ...

  5. 从数据库导出到excel

    在项目 扬中 News shenbaocreateall //选中的id string cc = Request["IDcheck"];            Response.C ...

  6. 如何使用NPOI 导出到excel和导入excel到数据库

    近期一直在做如何将数据库的数据导出到excel和导入excel到数据库. 首先进入官网进行下载NPOI插件(http://npoi.codeplex.com/). 我用的NPOI1.2.5稳定版. 使 ...

  7. 数据库多张表导出到excel

    数据库多张表导出到excel public static void export() throws Exception{ //声明需要导出的数据库 String dbName = "hdcl ...

  8. java 对excel操作 读取、写入、修改数据;导出数据库数据到excel

    ============前提加入jar包jxl.jar========================= // 从数据库导出数据到excel public List<Xskh> outPu ...

  9. 数据库数据用Excel导出的3种方法

    将数据库数据用Excel导出主要有3种方法:用Excel.Application接口.用OleDB.用HTML的Tabel标签 方法1——Excel.Application接口: 首先,需要要Exce ...

随机推荐

  1. 【leetcode系列】Valid Parentheses

    非常经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也能够直接用java自带的Stack类. 自己实现的栈代码: import java.util.LinkedList; class Stack ...

  2. HDU 1879 继续畅通工程 (Prim(普里姆算法)+Kruskal(克鲁斯卡尔))

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  3. jQuery Mobile组件

    一.页面 jQuery Mobile 应用了 HTML5 标准的特性,在结构化的页面中完整的页面结构分为header.content.footer 这三个主要区域. 在body 中插入内容块: < ...

  4. Android中adb push和adb install的使用区别

    Android中adb push和adb install的使用区别  转载 本篇文章由史迎春(@三俗小女子)投稿.转载请注明原文地址. 在Android实际开发中,经常会使用adb命令,安装应用程序可 ...

  5. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  6. Spring集成Quartz定时器

    <!-- Spring集成Quartz开始 --> <bean id="startQuertz" lazy-init="false" auto ...

  7. SVN创建分支

    工具:TortoiseSVN 创建一个空白项目,例如OA 从客户端检出OA,在OA文件夹下新建三个子文件夹 trunk:存放开发的主线,团队成员在开发的时候一直要用这个库中的内容 branches:存 ...

  8. RHEL6.4 NFS文件共享服务搭建

    NFS文件共享服务 1 实验方案 使用2台RHEL6.4虚拟机,其中一台作为NFS共享服务器(192.168.100.1).另外一台作为测试用的NFS客户机(192.168.100.2) 2.实现 2 ...

  9. Gartner 认定 Microsoft 为具有远见卓识的云基础结构即服务提供商

    四个月前, Windows Azure 基础结构服务结束了预览版阶段,正式发布了,它具有业内领先的 SLA.随后, 凭借愿景的完整性和执行力,Gartner 很快认可了 Microsoft 在市场中的 ...

  10. ZOJ 2562 More Divisors(高合成数)

    ZOJ 2562 More Divisors(高合成数) ACM 题目地址:ZOJ 2562 More Divisors 题意:  求小于n的最大的高合成数,高合成数指一类整数,不论什么比它小的自然数 ...