码迷,mamicode.com
首页 > 数据库 > 详细

导出数据库任意表数据为EXCEL

时间:2014-07-19 19:15:15      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   http   java   使用   os   文件   

public class ExportAsExcel {
final Logger logger = LoggerFactory.getLogger(getClass());
static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 公共组件,导出Excel表格数据方法<p>
* 使用方法: 数据为List<Object>类型,注意当中object的属性理论上必须为java基本数据类型
* 当然也支持像日期这种格式,即完整支持数据库中字段类型
* @author pery
* @param className 数据模型Object的类完整名称
* ,eg:com.swust.kelab.Department
* @param propertyName 数据模型类的属性对应的列名,
* 如“depaId”对应列表“机构id“,
* 未指定列名的属性不会被导出
* @param tableName 导出的excel中的表名称
* @param record 导出的文件绝对路径(必须为存在的文件,在本方法中没有做文件路径检查)
* @param objectList 要导出的数据集
* @return 成功返回true,失败返回false;
*/
public static boolean exportExcel(String className,Map propertyName,String tableName,File record,List<? extends Object> objectList){
HSSFWorkbook wbook = new HSSFWorkbook();
HSSFSheet wsheet = wbook.createSheet(tableName); //表名
HSSFCellStyle cellStyle=getStyle(wbook);
// 设置Excel表头
HSSFRow excelTitle = wsheet.createRow(0);
excelTitle.setHeightInPoints(22);
if(objectList.size()<=0){
wsheet.autoSizeColumn(0);
HSSFCell titleCell = excelTitle.createCell(0);
titleCell.setCellValue("对不起,没有可用数据导出");
titleCell.setCellStyle(cellStyle);
}
Class c = null;
try {
c = Class.forName(className);
}
catch (ClassNotFoundException e1) {
System.out.println("穿入反射用到的类名字有误");
e1.printStackTrace();
return false;
}
java.lang.reflect.Field[] flds = c.getDeclaredFields();
int j=0;
for (int i = 0; i < flds.length; i++) {
String columName=flds[i].getName();
if(propertyName.containsKey(columName)){
wsheet.setColumnWidth(j+1, (int)(100*35.7));
HSSFCell titleCell = excelTitle.createCell(j++);
titleCell.setCellValue((String)propertyName.get(columName));
titleCell.setCellStyle(cellStyle);
}
}
for (int i = 0; i < objectList.size(); i++) {
Object obj = objectList.get(i);
HSSFRow row = wsheet.createRow(i + 1);
int k=0;
for (j = 0; j < flds.length; j++) {
String columName=flds[j].getName();
if(!propertyName.containsKey(columName)){
continue;
}
HSSFCell hssfCell = row.createCell(k++);
try {
flds[j].setAccessible(true);
Object t = flds[j].get(obj);
if(t instanceof Date){
t = dateFormat.format(t);
}
if(t!=null){
hssfCell.setCellValue(String.valueOf(t));
}
} catch (IllegalArgumentException e) {
System.out.println("该字段不是基本数据格式字段");
e.printStackTrace();
} catch (IllegalAccessException e) {
System.out.println("非法访问");
e.printStackTrace();
try {
FileOutputStream fOut = new FileOutputStream(record);
wbook.write(fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
System.out.println("文件路径错误");
e.printStackTrace();
} catch (IOException e) {
System.out.println("I/O错误");
e.printStackTrace();
}
return true;
* Excel表格格式属性调整
* @param wbook
* @return
*/http://www.huiyi8.com/moban/ html模板
private static HSSFCellStyle getStyle(HSSFWorkbook wbook){
HSSFCellStyle cellStyle = wbook.createCellStyle();
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_DOUBLE);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);
HSSFFont font = wbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
return cellStyle;
}

导出数据库任意表数据为EXCEL,布布扣,bubuko.com

导出数据库任意表数据为EXCEL

标签:style   http   java   使用   os   文件   

原文地址:http://www.cnblogs.com/cjings/p/3853535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!