最近项目要用到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. PDMS二次开发之PML开发一些常见查询语句

    1.查找session 以及session number var !DBname DBname !db = object db(!DBname) !session = !db.lastsession( ...

  2. 自动执行任务管理---TaskManage

    这篇主要配合数据使用 先说数据库 名字都很标准---------------------------------- ------------------------------------------ ...

  3. Thinking in Java——笔记(14)

    Type Information The need for RTTI Because it is a dynamically bound method, the proper behavior wil ...

  4. 【java基础学习】IO流

    IO流 字节流InputStream和OutputStream 字符流Writer和Reader 装饰模式

  5. 自定义RecyclerView.ItemDecoration,实现RecyclerView的分割线效果

    [转] 原文 自定义RecyclerView.ItemDecoration,实现RecyclerView的分割线效果 字数1598 阅读302 评论2 喜欢23 1.背景   RecyclerView ...

  6. rem的使用

    浏览器的默认字体高是16px. 兼容性: 目前,IE9+,Firefox.Chrome.Safari.Opera 的主流版本都支持了rem. 对于不支持的浏览器,要多写一个绝对单位的声明,这样浏览器就 ...

  7. js弹出框,禁刷新

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. JMeter学习-036-JMeter调试工具之三---Debug Sampler

    前面两篇文章分别讲述了 HTTP Mirror Server 和 Debug PostProcessor 的脚本调试实例.此文主要讲述第三种调试工具:DebugSampler,其主要是查看JMeter ...

  9. shell 除法 小数点

    比如: num1=2 num2=3 num3=`echo "scale=2; $num1/$num2" | bc` 使用bc工具,sclae控制小数点后保留几位 还有一种方法 aw ...

  10. kdiff3的主窗口说明 Base Local Remote 分别代表什么分支