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. 牛客网——E进阶吧阶乘

    链接:https://www.nowcoder.net/acm/contest/75/E来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32768K,其他语言65536K ...

  2. Android 遍历全国的地区二(获取天气)

    根据上次的内容 1. 界面布局 weather_layout.xml <LinearLayout xmlns:android="http://schemas.android.com/a ...

  3. python实现八大排序算法

    插入排序 核心思想 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为 O(n^2).是稳定的排序方法.插入算法 ...

  4. 深度学习(七十一)darknet 源码阅读

    深度学习(七十一)darknet 源码阅读

  5. Java第三次作业--面向对象基础(封装)

    Deadline: 2017-4-6 23:00 一.学习要点 认真看书并查阅相关资料,掌握以下内容: 掌握简单类的设计 掌握利用对象引用建立类与类之间的联系 掌握this关键字 掌握static关键 ...

  6. Linux修改串口irq

    /******************************************************************************* * Linux修改串口irq * 说明 ...

  7. Codeforces 990B :Micro-World

    B. Micro-World time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  8. javascript reg 不加入分组

    from :https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-what-does-a-question ...

  9. Struts2自定义标签3模仿原有的s:if s:elseif s:else自定义自己的if elsif else

    第一步:webroot/web-inf下简历str.tld文件 <?xml version="1.0" encoding="UTF-8"?> < ...

  10. MySQL 练习题4

    1.表结构如下: #课程表 CREATE TABLE `course` ( `c_id` ) NOT NULL, `c_name` ) DEFAULT NULL, `t_id` ) DEFAULT N ...