最近项目要用到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. jackson注解使用心得

    maven依赖: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId ...

  2. CodeIgniter(CI)框架中的验证码

    在CodeIgniter框架中,CI本身自带了验证码,但是查看文档的时候,发现: 需要新建一个表,用来存储验证码信息.因为习惯了session存储验证码信息,所以我把我认为比较好看的验证码应用在了CI ...

  3. sql server 游标 写给自己

    ) --定义两个局部变量 @id @name 全局变量是两个@@name ) Declare Cur Cursor For --定义一个游标 select id,name from temp1 --查 ...

  4. words

    conscious[英][ˈkɒnʃəs][美][ˈkɑnʃəs]consensus[英][kənˈsensəs][美][kənˈsɛnsəs] scious sensuswaterflood; de ...

  5. webdriver零碎知识点

    #零碎知识点,用于记录平时遇到的比较杂的知识点 driver.current_url 获取当前url phantomjs 实现无浏览器界面自动化测试(driver = webdriver.Phanto ...

  6. 在mvc中实现图片验证码的刷新

    首先,在项目模型(Model)层中建立一个生成图片验证码的类ValidationCodeHelper,代码如下: public class ValidationCodeHelper { //用户存取验 ...

  7. 基于ssh框架开发的购物系统的质量属性

    根据前面的博客,我们已经大致了解了ssh架构开发整体概念:Struts是一个实现了MVC模式的经典的框架:Hibernate是轻量级Java EE应用的持久层解决方案,以面向对象的方式提供了持久化类到 ...

  8. test for cvx library in matlab - windows

    Download the zip file of cvx http://cvxr.com/cvx/download/ by downloading cvx-w64.zip Require a lice ...

  9. Python学习笔记 for windows 二

    函数 abs(-20)                                        //结果为:20,绝对值函数 def 函数名称([参数1,参数2,参数3]): 执行语句 retu ...

  10. 好用的Markdown编辑器一览

    Markdown 是一种简单的.轻量级的标记语法.用户可以使用诸如 * # 等简单的标记符号以最小的输入代价生成极富表现力的文档. Markdown具有很多优点: 写作中添加简单符号即完成排版,所见即 ...