Bean转Excel对象

  

 /*
* 文件名:BeanToExcel.java
*/ import java.util.ArrayList;
import java.util.List; import jxl.Sheet;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException; /**
* TODO 添加类的一句话简单描述。
*/
public class BeanToExcel<T> { private WritableSheet wrightsheet;
private Sheet readSheet;
private List<T> beanList;
private Mapper<T> mapper;
private List<WritableCell> needChangeCells = new ArrayList<WritableCell>(); public BeanToExcel(WritableSheet sheet, List<T> beanList, Mapper<T> mapper){
this.wrightsheet = sheet;
this.beanList = beanList;
this.mapper = mapper;
} public BeanToExcel(Sheet sheet, Mapper<T> mapper){
this.readSheet = sheet;
this.mapper = mapper;
this.beanList = new ArrayList<T>();
} public void writeExcel() throws Exception{
try {
write();
} catch (RowsExceededException e) {
DEBUGGER.error("Failed to writeExcel", e);
throw e;
} catch (WriteException e) {
DEBUGGER.error("Failed to writeExcel", e);
throw e;
}
} public List<T> readBeans(){
for(int i=1; i<readSheet.getRows(); i++){
try {
T bean = mapper.toBean(readSheet.getRow(i));
if (bean instanceof ExcelInfo) {
((ExcelInfo) bean).setRow(i);
((ExcelInfo) bean).setReadSheet(readSheet);
}
if (bean != null){
beanList.add(bean);
}
} catch (ExcelException e) {
needChangeCells.addAll(e.getCellList());
}
}
return beanList;
} protected void write() throws RowsExceededException, WriteException{
for(int i = 0 ; i < beanList.size(); i++){
T bean = beanList.get(i);
List<WritableCell> line = mapper.toRow(i+1, bean);
for(WritableCell cell : line){
wrightsheet.addCell(cell);
}
}
} public List<WritableCell> getNeedChangeCells() {
return needChangeCells;
} }

  

  Excel.java

  

 /*
* 文件名:ExcelInfo.java
*/ import jxl.Sheet; /**
* ExcleInfo
*/
public class ExcelInfo
{ private int row; private Sheet readSheet; public int getRow()
{
return row;
} public void setRow(int row)
{
this.row = row;
} public Sheet getReadSheet()
{
return readSheet;
} public void setReadSheet(Sheet readSheet)
{
this.readSheet = readSheet;
}
}

  ExcelException.java

  

 /*
* 文件名:ExcelException.java
*/ import java.util.List; import jxl.write.WritableCell; /**
* TODO 添加类的一句话简单描述。
*/
public class ExcelException extends RuntimeException { private static final long serialVersionUID = -3113079946804687851L; public static ExcelException DEFAULT = new ExcelException("未知异常"); private String msg;
private List<WritableCell> cellList; public ExcelException(String msg) {
this.msg = msg;
} public ExcelException(String msg, List<WritableCell> cellList) {
this.msg = msg;
this.cellList = cellList;
} public ExcelException(String msg, StackTraceElement[] e, List<WritableCell> cellList) {
this.msg = msg;
this.cellList = cellList;
this.setStackTrace(e);
} @Override
public String getMessage() {
return this.msg;
} public List<WritableCell> getCellList() {
return cellList;
} }

  Mapper.java

  

 /*
* 文件名:Mapper.java
*/ import java.util.List; import jxl.Cell;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WriteException; /**
* TODO 添加类的一句话简单描述。
*/
public abstract class Mapper<T> { public abstract List<WritableCell> toRow(int row,T t); public abstract T toBean(Cell[] rowCells) throws ExcelException; public WritableCell createCell(int column, int row, String content){
Label label = new Label(column, row, content);
return label;
} public WritableCell createErrorCell(int column, int row, String content){
Label label = new Label(column, row, content);
WritableCellFormat cellFormat = new WritableCellFormat();
try {
cellFormat.setBackground(Colour.YELLOW);
label.setCellFormat(cellFormat);
} catch (WriteException e) {
DEBUGGER.error("Failed to createErrorCell", e);
}
return label;
} }

  

JAVA 操作Excel工具类的更多相关文章

  1. java操作excel 工具类

    java操作excel 可参考https://blog.csdn.net/xunwei0303/article/details/53213130 直接上代码: 一.java生成excel文件: pac ...

  2. Java操作Excel工具类(poi)

    分享一个自己做的poi工具类,写不是很完全,足够我自己当前使用,有兴趣的可以自行扩展 1 import org.apache.commons.lang3.exception.ExceptionUtil ...

  3. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

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

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

  5. java 解析excel工具类

      java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...

  6. Java操作Redis工具类

    依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  7. java里poi操作Excel工具类【我改】

    参考原文: https://www.cnblogs.com/yizhang/p/7244917.html 我改: package test; import java.io.File; import j ...

  8. Java解析Excel工具类(兼容xls和xlsx)

    依赖jar <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml&l ...

  9. java导出excel工具类

    java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...

随机推荐

  1. java日志体系的思考(转)

    Java 日志缓存机制的实现 Java 日志管理最佳实践 混乱的 Java 日志体系 log日志远程统一记录 浅谈后端日志系统 Java异常处理和接口约定 用SLF4j/Logback打印日志-1 用 ...

  2. 浅谈jsonp

    要谈jsonp,首先要弄明白jsonp是什么,它是用来干嘛的.jsonp其实就是我们常用的script标签,用来解决跨域的,只不过这个标签是动态创建的,为啥要动态创建涅. 举个小栗子: 假如我们远程文 ...

  3. mysql-connect-odbc连接

  4. 线程互斥,mutex

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  5. New Concept English Two 5

    $课文9 冷遇 83. On Wednesday evening, we went to the Town Hall. 星期三的晚上,我们去了市政厅. 84. It was the last day ...

  6. Buildroot make网卡interfaces文件被修改

    /******************************************************************************** * Buildroot make网卡 ...

  7. Windows7 SP1 64bit配置IIS7.5和ASP.NET4

    一.安装前的环境 1. Windows7 SP1 64bit: 2. 在安装IIS7.5之前,安装了Visual Studio 2010或.NET Framework4: 二.安装IIS7.5 1.  ...

  8. 小数第n位

    问题描述 我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数. 如果我们把有限小数的末尾加上无限多个0,它们就有了统一的形式. 本题的任务是:在上面的约定下,求整数除法小数点后的第n位开始 ...

  9. HDU 4123 Bob’s Race(RMQ)

    题意是说给出一棵树,N(10^5)个顶点,以及每条边的权值,现在需要选择连续的K个点(顶点编号连续),可以被选出来的条件是: 若d[i]代表顶点i到树上其他点的距离的最大值,使得区间[a, b]的d值 ...

  10. 善待Erlang 代码 -- Xref 实践

    Xref 是一个交叉引用工具,通过分析定义的函数间的调用关系,用于查找函数. 模块. 应用程序和版本之间的依赖关系. 通俗而言,Xref 可以检查代码中函数的调用关系.比如在 moduleA 中的 f ...