最近项目要用到excel导出功能,之前也写过类似的代码。因为这次项目中多次用到excel导出。这次长了记性整理了一下 分享给大伙

欢迎一起讨论

生成excel的主工具类:

public class ExcelFactory<T> {
private static final String EXCEL_FONT_FAMILY_SETTING = "Monaco"; //设置字体
private static final int EXCEL_FONT_SIZE_SETTING = 12; //设置字体大小 private WritableWorkbook writableWorkbook = null;
private WritableSheet writableSheet = null; public WritableWorkbook createExcel(OutputStream os, Excel excel,
List<String> header, List<T> datas, ExcelMapper<T> mapper)
throws RowsExceededException, WriteException { //生成excel
try {
writableWorkbook = Workbook.createWorkbook(os);
writableSheet = writableWorkbook.createSheet(excel.getSheetName(),
excel.getSheetNum());
SheetSettings settings = writableSheet.getSettings();
settings.setVerticalFreeze(1); // Write the title
if (header != null && header.size() > 0) {
for (int i = 0; i < header.size(); i++) {
writableSheet.addCell(new Label(i, 0, header.get(i),
setHeaderCellStyle(new CellStyle(EXCEL_FONT_FAMILY_SETTING, EXCEL_FONT_SIZE_SETTING))));
}
} // Write data to file
if (datas != null && datas.size() > 0) {
for (int i = 1; i <= datas.size(); i++) {
mapper.mapToExcel(writableSheet, datas.get(i - 1),
setBodyCellStyle(new CellStyle(EXCEL_FONT_FAMILY_SETTING, EXCEL_FONT_SIZE_SETTING)), i);
}
}
} catch (IOException e) {
return null;
} return writableWorkbook;
} private WritableCellFormat setHeaderCellStyle(CellStyle style) throws WriteException {
WritableFont font = new WritableFont(
WritableFont.createFont(style.getFont()), style.getFontSize(), WritableFont.BOLD);
WritableCellFormat cellFormat = setCentre(style, font); return cellFormat;
} private WritableCellFormat setBodyCellStyle(CellStyle style) throws WriteException {
WritableFont font = new WritableFont(
WritableFont.createFont(style.getFont()), style.getFontSize(), WritableFont.NO_BOLD);
WritableCellFormat cellFormat = setCentre(style, font); return cellFormat;
} private WritableCellFormat setCentre(CellStyle style, WritableFont font)
throws WriteException {
WritableCellFormat cellFormat = new WritableCellFormat(font);
cellFormat.setBackground(style.getBgColor());
cellFormat.setAlignment(Alignment.CENTRE);
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
return cellFormat;
}
}

excel model类:

public class Excel {
private String sheetName;
private int sheetNum; public Excel() { } public Excel(String sheetName, int sheetNum) {
this.sheetName = sheetName;
this.sheetNum = sheetNum;
}
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
public int getSheetNum() {
return sheetNum;
}
public void setSheetNum(int sheetNum) {
this.sheetNum = sheetNum;
}
}

CellStyle的model类:

public class CellStyle {
private String font;
private int fontSize;
private Colour bgColor = Colour.WHITE; public CellStyle() {} public CellStyle(String font, int fontSize) {
this.font = font;
this.fontSize = fontSize;
} public Colour getBgColor() {
return bgColor;
} public void setBgColor(Colour bgColor) {
this.bgColor = bgColor;
} public int getFontSize() {
return fontSize;
} public void setFontSize(int fontSize) {
this.fontSize = fontSize;
} public String getFont() {
return font;
} public void setFont(String font) {
this.font = font;
}
}

ExcelMapper的接口类(用于写数据):

public interface ExcelMapper<T> {
void mapToExcel(WritableSheet sheet, T t, WritableCellFormat wcf, int rowNum);
}

举个例子:

public class FinanceExcelMapper implements ExcelMapper<Finance>{
public void mapToExcel(WritableSheet sheet, Finance finance, WritableCellFormat wcf, int rowNum) {
try {
sheet.addCell(new Label(0, rowNum, String.valueOf(finance.getStudentCourseId())));
sheet.addCell(new Label(1, rowNum, finance.getCardNum()));
sheet.addCell(new Label(2, rowNum, finance.getStuName()));
DefaultDictionaryManager manager = DefaultDictionaryManager.getInstance();
sheet.addCell(new Label(3, rowNum, manager.dictionary(finance.getSignUpComeFrom(), "signUpComeFrom").getItemValue()));
sheet.addCell(new Label(4, rowNum, finance.getDepartmentName()));
sheet.addCell(new Label(5, rowNum, finance.getMajorName()));
sheet.addCell(new Label(6, rowNum, finance.getCourseName()));
sheet.addCell(new Label(7, rowNum, String.valueOf(finance.getCourseTuition())));
sheet.addCell(new Label(8, rowNum, manager.dictionary(finance.getCourseDiscount(), "courseDiscount").getItemValue()));
sheet.addCell(new Label(9, rowNum, String.valueOf(finance.getActualTuition())));
sheet.addCell(new Label(10, rowNum, finance.getFinanceUser()));
sheet.addCell(new Label(11, rowNum, finance.getFinanceTime().toString())); } catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

然后在controller调用传值,然后字节流读取 在写到输出流里就好了。

自己写的java excel导出工具类的更多相关文章

  1. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  2. 基于jdk1.7实现的excel导出工具类

    通用excel导出工具类,基于泛型.反射.hashmap 以及基于泛型.反射.bean两种方式 import java.io.*;import java.lang.reflect.Field;impo ...

  3. EXCEL导出工具类及调用

    一.Excel导出工具类代码 package com.qiyuan.util; import java.io.OutputStream; import java.io.UnsupportedEncod ...

  4. excel导出工具类

    package com.jianwu.util.excel; import com.google.common.collect.Lists;import com.jianwu.exception.Mo ...

  5. 一个很好的通用 excel 导出工具类

    此类用主要 jxl +注解+流 实现扩展性很强,jxl性能会比poi好一点,值得我们学习. package oa.common.utils; import java.io.OutputStream; ...

  6. Java基础学习总结(49)——Excel导入导出工具类

    在项目的pom文件中引入 <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifac ...

  7. 使用Apache poi来编写导出excel的工具类

    在JavaWeb开发的需求中,我们会经常看到导出excel的功能需求,然后java并没有提供操作office文档的功能,这个时候我们就需要使用额外的组件来帮助我们完成这项功能了. 很高兴Apache基 ...

  8. 一个基于POI的通用excel导入导出工具类的简单实现及使用方法

    前言: 最近PM来了一个需求,简单来说就是在录入数据时一条一条插入到系统显得非常麻烦,让我实现一个直接通过excel导入的方法一次性录入所有数据.网上关于excel导入导出的例子很多,但大多相互借鉴. ...

  9. 通用Excel文件导出工具类

    1:Excel格式 2:ExcelUtil.java import java.io.ByteArrayOutputStream; import java.io.IOException; import ...

随机推荐

  1. finally类

    finally叫做最后的执行快,什么是最后的执行快?他的意思是这样的 他是写在try catch 的后面但是只能写一个,他设计这个finally的意思就是,如果try里面出错肯定会往陷阱里 面跑.没有 ...

  2. css解决div的各种浏览器兼容性问题

    方法一: min-height:500px;/*解决ie8.9.ff.chromet*/ height:100%;/*解决ie6.7*/ _height:500px;/*解决ie6超出自动溢出*/ 方 ...

  3. cocostudio做出来的界面如何进行分辨率适配,兼论cocos2dx3的多分辨率适配机制,以及retina适配机制

    cocos有很多代码实际上都不再使用了,看代码时反而误导了程序员. 比如一个简单的分辨率适配,我查到了setContentSize,然后调用setContentSize,毫无用处啊!于是乎,我到处查资 ...

  4. 阿里云服务器Linux CentOS安装配置(六)resin多端口配置、安装、部署

    阿里云服务器Linux CentOS安装配置(六)resin多端口配置.安装.部署 1.下载resin包 http://125.39.66.162/files/2183000003E08525/cau ...

  5. iOS用户信息单例的创建

    UserInfo.h + (UserInfo *) sharedInstance; UserInfo.m #import "UserInfo.h" static UserInfo ...

  6. erlang ssl

    http://itindex.net/detail/50701-tomcat-bio-nio.apr http://blog.csdn.net/libing1991_/article/details/ ...

  7. java中的Comparable接口

    类对象之间比较"大小"往往是很有用的操作,比如让对象数组排序时,就需要依赖比较操作.对于不同的类有不同的语义.如Student类,比较2个学生对象可以比较他们的score分数来评判 ...

  8. chrome中hack解决input:-webkit-autofill自定义样式

    在使用chrome浏览器设计网页时,想将input背景改成透明,也就是 background-color:transparent; 可是效果并不如人意 hack方法: input:-webkit-au ...

  9. SqlServer触发器判断对表操作类型(增、删、改)并将修改后的数据映射到新表

    该文章为原创,日后可能会根据实际开发经验和网友评论,进行相应地方修改,为获得最新博客动态,望在转发博客的时候注明出处. 触发器要实现的功能: (1)获取对表Table1数据操作操作类型(insert. ...

  10. wordpress多站点环境设置上传附件大小

    多站点环境更改上传附件大小: php.ini post_max_size = 8M upload_max_filesize = 10M 另外,后台域名管理中设置/网络设置/可以设置上传文件大小. 代码 ...