如何把数据库里面的数据导出成excel表

bownviva 2010-03-20 01:15:45
如题. 要求是用java来完成这一过程.
...全文
72 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bownviva 2010-03-20
  • 打赏
  • 举报
回复
谁能给个 把excel导进数据库的代码
zings 2010-03-20
  • 打赏
  • 举报
回复
做报表的貌似人很多。。。我不是其中一个。。。
jack20209 2010-03-20
  • 打赏
  • 举报
回复
package com.be.sccm.common.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
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;

/**
*
* 利用开源组件POI3.0.2动态导出EXCEL文档
* 应用泛型,代表任意一个符合javabean风格的类
* byte[]表jpg格式的图片数据
* @param <T>
*
*/

public class ExportExcelUtil {

private HSSFWorkbook workBook;

private OutputStream out;

public void exportExcel(HSSFWorkbook workBook,String title,Collection dataset) {
exportExcel(workBook,title, null, dataset, "yyyy-MM-dd");
}

public void exportExcel(HSSFWorkbook workBook,String title,String[] headers, Collection dataset) {
exportExcel(workBook,title, headers, dataset, "yyyy-MM-dd");
}

/**
*
* @param title
* 表格标题名
* @param headers
* 表格属性列名数组
* @param dataset
* 需要显示的数据集合
* @param dateFormat
* 时间格式
*
*/

@SuppressWarnings("unchecked")
public void exportExcel(HSSFWorkbook paramWorkBook,String title, String[] headers,
Collection dataset, String dateFormat) {

// 声明一个工作薄
if(workBook==null){
if(paramWorkBook!=null){
this.workBook=paramWorkBook;
}else{
this.workBook = new HSSFWorkbook();
}
}
// 生成一个表格

HSSFSheet sheet = workBook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 生成一个样式
HSSFCellStyle style = workBook.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 = workBook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = workBook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

// 生成另一个字体
HSSFFont font2 = workBook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);
// 声明一个画图的顶级管理器
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 定义注释的大小和位置,详见文档
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,0, 0, 0, (short) 4, 2, (short) 6, 5));

// 设置注释内容
comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
comment.setAuthor("xie");
// 产生表格标题行
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);
}

Iterator it = dataset.iterator();
int index = 0;
while (it.hasNext()) {
index++;
row = sheet.createRow(index);
Object t = (Object) it.next();
// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
Field[] fields = t.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style2);
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 = null;
if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
textValue = sdf.format(date);
} else if (value instanceof byte[]) {
// 有图片时,设置行高为60px;
row.setHeightInPoints(60);
// 设置图片所在列宽度为80px,注意这里单位的一个换算
sheet.setColumnWidth(i, (short) (35.7 * 80));
// sheet.autoSizeColumn(i);
byte[] bsValue = (byte[]) value;
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,
1023, 255, (short) 6, index, (short) 6, index);
anchor.setAnchorType(2);
patriarch.createPicture(anchor, workBook.addPicture(
bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));
} else {
// 其它数据类型都当作字符串简单处理
textValue = value.toString();
}
// 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
if (textValue != null) {
Pattern p = Pattern.compile("^\\d+(\\.\\d+)?$");
Matcher matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
} else {
HSSFRichTextString richString = new HSSFRichTextString(textValue);
HSSFFont font3 = workBook.createFont();
font3.setColor(HSSFColor.BLUE.index);
richString.applyFont(font3);
cell.setCellValue(richString);
}
}

} catch(Exception e){
e.printStackTrace();
}finally {

}

}
}

}

/**
*
* 必须显示调用输出
* @param out
* @throws IOException
*/
public void writeExcel(OutputStream paramOut) throws IOException{
try {
this.workBook.write(paramOut);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
paramOut.close();
}
}

public static void main(String[] args) {

// 测试学生

ExportExcelUtil ex = new ExportExcelUtil();

String[] headers = { "学号", "姓名", "年龄", "性别", "出生日期" };

List<Student> dataset = new ArrayList<Student>();

dataset.add(new Student(10000001, "张三", 20, true, new Date()));

dataset.add(new Student(20000002, "李四", 24, false, new Date()));

dataset.add(new Student(30000003, "王五", 22, true, new Date()));


String[] headers2 = { "图书编号", "图书名称", "图书作者", "图书价格", "图书ISBN",

"图书出版社", "封面图片" };

List<Book> dataset2 = new ArrayList<Book>();

try {

BufferedInputStream bis = new BufferedInputStream(

new FileInputStream("book.jpg"));

byte[] buf = new byte[bis.available()];

while ((bis.read(buf)) != -1) {

//

}

dataset2.add(new Book(1, "jsp", "leno", 300.33f, "1234567",

"清华出版社", buf));

dataset2.add(new Book(2, "java编程思想", "brucl", 300.33f, "1234567",

"阳光出版社", buf));

dataset2.add(new Book(3, "DOM艺术", "lenotang", 300.33f, "1234567",

"清华出版社", buf));

dataset2.add(new Book(4, "c++经典", "leno", 400.33f, "1234567",

"清华出版社", buf));

dataset2.add(new Book(5, "c#入门", "leno", 300.33f, "1234567",

"汤春秀出版社", buf));

OutputStream out = new FileOutputStream("E:\\a.xls");
ex.exportExcel(null,"a",headers, dataset);
ex.exportExcel(null,"b",headers2, dataset2);

ex.writeExcel(out);

JOptionPane.showMessageDialog(null, "导出成功!");

System.out.println("excel导出成功!");

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();
}

}

public HSSFWorkbook getWorkBook() {
return workBook;
}

public void setWorkBook(HSSFWorkbook workBook) {
this.workBook = workBook;
}

public OutputStream getOut() {
return out;
}

public void setOut(OutputStream out) {
this.out = out;
}

}
  • 打赏
  • 举报
回复
用力顶个,顺便看下头像

62,619

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧