- 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
- */
- 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;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0311201410897.html
来源: http://www.codesnippet.cn/detail/0311201410897.html