JAVA 操作Excel工具类
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工具类的更多相关文章
- java操作excel 工具类
java操作excel 可参考https://blog.csdn.net/xunwei0303/article/details/53213130 直接上代码: 一.java生成excel文件: pac ...
- Java操作Excel工具类(poi)
分享一个自己做的poi工具类,写不是很完全,足够我自己当前使用,有兴趣的可以自行扩展 1 import org.apache.commons.lang3.exception.ExceptionUtil ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- java 解析excel工具类
java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...
- Java操作Redis工具类
依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...
- java里poi操作Excel工具类【我改】
参考原文: https://www.cnblogs.com/yizhang/p/7244917.html 我改: package test; import java.io.File; import j ...
- Java解析Excel工具类(兼容xls和xlsx)
依赖jar <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml&l ...
- java导出excel工具类
java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...
随机推荐
- vim按下ctrl+s僵死
CTRL+S表示停止向终端停止输出 CTRL+Q恢复向终端输出流
- hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0
Description “Farm Game” is one of the most popular games in online community. In the community each ...
- delphi7完全关闭一个窗体
如果一个工程中有若干个form,在程序运行中若要彻底关闭其中的一个窗体 除了点击右上角的小叉叉外,也可以在form的close事件中添加一句话 procedure TLockScreen.FormCl ...
- PHP7.2.12-Configuration-Option
# PHP7.2.12 <pre style="background-color: rgb(255,250,233);"> `configure' configures ...
- idea装在spring框架报ClassNotFound(转)
原文链接:http://blog.csdn.net/getyouwant/article/details/50417030 环境:intellij 15 ,spring 3.1 本来新建了一个spri ...
- swift 触摸与手势
class MyView: UIView { var lView:UIView! var time:NSTimer! override init(frame: CGRect) { super.init ...
- Swift 获取plist文件展示在TableView上
// 1.定义二维数组 var data:[[String]]! override func viewDidLoad() { super.viewDidLoad() // 2.实例化tableView ...
- Tomcat启动 Unable to process Jar entry [javassist/XXXXXX.class]
例如: 03-Mar-2017 17:01:45.864 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.startup.Co ...
- WinForm获取当前路径汇总
Winform获取应用程序的当前路径的方法集合汇总,值得收藏备用 具体如下, //获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Assembl ...
- ubuntu17.10安装LAMP并测试部署php探针系统
ubuntu17.10修改密码以及安装LAMP并部署php探针系统 步骤1:ubuntu17.10配置IP (这个版本配置IP方式改变较大,apt-get upgrade更新至最新以前配置方式也可以用 ...