最近项目要用到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. javascript入门:prototype和面向对象的实现

    由于工作需要,需要大量使用javascript,于是对其进行了一下学习. 学习任何一个语言,最重要的是掌握其和其他语言不同的关键特性.对javascript来说,我总结就是prototype.就像me ...

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

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

  3. [Python笔记]序列(一)索引、分片

    Python包含6种内建序列:列表.元组.字符串.Unicode字符串.buffer对象.xrange对象. 这些序列支持通用的操作: 索引 索引是从0开始计数:当索引值为负数时,表示从最后一个元素( ...

  4. php try catch throw 用法

    1.try catch 捕捉不到fatal error致命错误 2.只有抛出异常才能被截获,如果异常抛出了却没有被捕捉到,就会产生一个fatal error. 3.父类可以捕获抛出的子类异常,Exce ...

  5. Python之路----------shutil模块

    高级的文件.文件夹.压缩包 处理模块 复制文件: import shutil f1 = open('test') f2 = open('test2','w') shutil.copyfileobj(f ...

  6. IIS 7.5 + PHP-5.6.3 + mysql-5.6.21.1

    禅道项目管理软件源码下载:http://sourceforge.net/projects/zentao/files/6.3/ZenTaoPMS.6.3.stable.zip/download Stp1 ...

  7. 电脑文件出现“windows-文件发生意外问题-可修复(严禁修改)-错误代码0X00000BF8”错误,怎么办

    电脑文件出现"windows-文件发生意外问题-可修复(严禁修改)-错误代码0X00000BF8"错误,怎么办 下载一个"纵情文件修复器"修复一下就可以了 下载 ...

  8. nginx 客户端不缓存header

    location ~* \.(html|htm)$ { add_header Cache-Control no-store; }

  9. hibernate 多表查询

    Hibernate主要支持两种查询方式:HQL查询和Criteria查询.前者应用较为广发,后者也只是调用封装好的接口. 现在有一个问题,就是实现多表连接查询,且查询结果集不与任何一个实体类对应,怎么 ...

  10. [Spring常见问题]java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    这个问题是因为部署在tomcat下的项目中没有springweb包 但是问题来了,但是我的项目中有呀,maven都引了呀,然后我就懵B啦!看到这个博客我就豁然开朗了:http://my.oschina ...