java分割excel文件可用jxl
excel导入是经常使用到的功能,如果文件数据量大的话还是建议分割后导入,java常用的API是poi和jxl,我采用的是jxl,那么让我们来看下怎么用jxl来实现分割。
- 需要在pom中导入jxl的包
<!--excel-->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
- 我们需要两个实体类 ,分别是excel信息和文件信息
public class FileInfoModel {
/**
* 文件在数据库中的Id
*/
private Long fileId;
/**
* 是否上传
*/
private boolean isUpload;
/**
* 存储循环过程中的索引值
*/
private int forIndex;
/**
* 文件全名 xxx.xls
*/
private String fileFullName;
/**
* 文件名 xxx
*/
private String fileName;
/**
* 文件后缀 .xls
*/
private String fileSuffix;
/**
* 文件保存路径 e:\\xx\xx
*/
private String filePath;
/**
* 文件的起始行号
*/
private int beginRow;
/**
* 文件的末尾行号
*/
private int endRow;
public FileInfoModel() {
super();
}
public FileInfoModel(String fileFullName) {
super();
this.fileFullName = fileFullName;
}
public FileInfoModel(String fileName, String fileFullName) {
super();
this.fileName = fileName;
this.fileFullName = fileFullName;
}
public FileInfoModel(String fileFullName, String fileName, String fileSuffix) {
super();
this.fileFullName = fileFullName;
this.fileName = fileName;
this.fileSuffix = fileSuffix;
}
public FileInfoModel(String fileFullName, String fileName, String fileSuffix, String filePath) {
super();
this.fileFullName = fileFullName;
this.fileName = fileName;
this.fileSuffix = fileSuffix;
this.filePath = filePath;
}
public Long getFileId() {
return fileId;
}
public void setFileId(Long fileId) {
this.fileId = fileId;
}
public boolean isUpload() {
return isUpload;
}
public void setUpload(boolean isUpload) {
this.isUpload = isUpload;
}
public int getForIndex() {
return forIndex;
}
public void setForIndex(int forIndex) {
this.forIndex = forIndex;
}
public String getFileFullName() {
return fileFullName;
}
public void setFileFullName(String fileFullName) {
this.fileFullName = fileFullName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileSuffix() {
return fileSuffix;
}
public void setFileSuffix(String fileSuffix) {
this.fileSuffix = fileSuffix;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public int getBeginRow() {
return beginRow;
}
public void setBeginRow(int beginRow) {
this.beginRow = beginRow;
}
public int getEndRow() {
return endRow;
}
public void setEndRow(int endRow) {
this.endRow = endRow;
}
}
public class ExcelModel {
/*excel 文件路径*/
private String excelFilePath;
/*分割后的文件目录*/
private String excelChildFileDir;
/*excel 名称*/
private String excelName;
/*sheet 名称*/
private String sheetName = "Sheet1";
/*excel 总记录数*/
private int totalRows;
/*分割文件后每个文件的记录数*/
private int fileSize = 3000;
/*分割后的文件数*/
private int fileCount;
/*分割后的文件集合*/
private List<FileInfoModel> files;
/*计算总行数时是否去表头*/
private boolean isRemoveHeader = true;
/*是否去除重复数据*/
private boolean isCleareRepeat = false;
/*导入文件过程中是否出错*/
private boolean isError;
public boolean isError() {
return isError;
}
public void setError(boolean isError) {
this.isError = isError;
}
public String getExcelFilePath() {
return excelFilePath;
}
public void setExcelFilePath(String excelFilePath) {
this.excelFilePath = excelFilePath;
}
public boolean isCleareRepeat() {
return isCleareRepeat;
}
public void setCleareRepeat(boolean isCleareRepeat) {
this.isCleareRepeat = isCleareRepeat;
}
public String getExcelChildFileDir() {
return excelChildFileDir;
}
public void setExcelChildFileDir(String excelChildFileDir) {
this.excelChildFileDir = excelChildFileDir;
}
public boolean isRemoveHeader() {
return isRemoveHeader;
}
public void setRemoveHeader(boolean isRemoveHeader) {
this.isRemoveHeader = isRemoveHeader;
}
public int getFileCount() {
//根据总记录数及分割文件的行数计算文件数量
fileCount = (int) Math.ceil(this.totalRows / this.fileSize) + 1;
return fileCount;
}
public String getExcelName() {
return excelName;
}
public void setExcelName(String excelName) {
this.excelName = excelName;
}
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
public int getTotalRows() {
if (this.isRemoveHeader) {
return totalRows - 1;
} else {
return totalRows;
}
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public int getFileSize() {
return fileSize;
}
public void setFileSize(int fileSize) {
this.fileSize = fileSize;
}
public List<FileInfoModel> getFiles() {
return files;
}
public void setFiles(List<FileInfoModel> files) {
this.files = files;
}
}
- 添加接口IExcelOperate
public interface IExcelOperate {
/**
* 加载excel文件
* @param fileName
*/
void load(String fileName) throws Exception;
/**
* 读取excel文件数据
*
* @throws Exception
*/
public <T> List<T> readExcel() throws Exception;
/**
* 分割excel
*
* @throws Exception
*/
public void splitExcel() throws Exception;
/**
* 多线程分割excel
*/
public void splitExcelThread() throws Exception;
/**
* 关闭文件
*/
void close();
}
- 让我们来实现接口的方法吧
public class ExcelUtil<T> implements IExcelOperate {
protected final Log log = LogFactory.getLog(getClass());
private ExcelModel excelModel;
private Class tClass;
private Cell[] titleCell;
private jxl.Workbook workBook;
private Sheet sheet;
public int getRows() {
if (this.sheet != null) {
return this.sheet.getRows();
}
return 0;
}
public ExcelUtil() {
super();
}
public ExcelUtil(ExcelModel excelModel, Class tClass) {
this.tClass = tClass;
this.excelModel = excelModel;
}
public ExcelModel getExcelModel() {
return excelModel;
}
public void setExcelModel(ExcelModel excelModel) {
this.excelModel = excelModel;
}
@Override
public void load(String fileName) throws Exception {
try {
File file = new File(fileName);
workBook = Workbook.getWorkbook(file);
this.excelModel.setFiles(new ArrayList<FileInfoModel>());
//默认读取文件的路径
String[] ary = file.getName().split("\\.");
String suffix = ary[1];
this.excelModel.getFiles().add(new FileInfoModel(file.getName(), fileName, "." + suffix, file.getParent()));
if (this.excelModel.isCleareRepeat()) {
this.clearRepeat(file);
workBook = Workbook.getWorkbook(file);
}
if (workBook == null) {
throw new Exception("读取excel文件出错!");
}
sheet = this.excelModel.getSheetName().trim() == "" ? workBook.getSheet(0) : workBook.getSheet(this.excelModel.getSheetName());
if (sheet == null) {
sheet = workBook.getSheet(0);
}
if (sheet == null) {
throw new Exception("读取sheet出错!");
}
this.excelModel.setTotalRows(sheet.getRows());
// 用于存储列标题
titleCell = new Cell[sheet.getColumns()];
// 将列标题存储存到一个一维数组中
for (int i = 0; i < titleCell.length; i++) {
titleCell[i] = sheet.getCell(i, 0);
}
} catch (IOException e) {
workBook.close();
}
}
@Override
public void splitExcel() throws Exception {
//根据文件数分割 excel,重置文件集合
this.excelModel.setFiles(new ArrayList<FileInfoModel>());
//生成Guid作为文件前缀
UUID uuid = UUID.randomUUID();
FileInfoModel fileInfoModel = new FileInfoModel();
for (int i = 1; i <= this.excelModel.getFileCount(); i++) {
fileInfoModel = getFileInfoModelBySplit(i, uuid);
this.excelModel.getFiles().add(this.split(fileInfoModel));
}
}
@Override
public void splitExcelThread() throws Exception {
//根据文件数分割 excel,重置文件集合
this.excelModel.setFiles(new ArrayList<FileInfoModel>());
//生成Guid作为文件前缀
UUID uuid = UUID.randomUUID();
//根据文件数分割 excel,重置文件集合
this.excelModel.setFiles(new ArrayList<FileInfoModel>());
//工作线程
ExecutorService executorService = Executors.newFixedThreadPool(this.excelModel.getFileCount());
FileInfoModel fileInfoModel = new FileInfoModel();
for (int i = 1; i <= this.excelModel.getFileCount(); i++) {
fileInfoModel = getFileInfoModelBySplit(i, uuid);
ExcelThread thread = new ExcelThread(fileInfoModel);
executorService.execute(thread);
this.excelModel.getFiles().add(thread.getReturnFile());
}
executorService.shutdown();
while (!executorService.isTerminated()) {
//检查所有线程都执行完成
}
}
/**
* 分割excel时获取文件信息
*
* @param i
* @return
*/
private FileInfoModel getFileInfoModelBySplit(int i, UUID uuid) {
/*结束行*/
int endRow = i * this.excelModel.getFileSize();
/*起始行*/
int beginRow = (endRow - this.excelModel.getFileSize()) + 1;
/*如果结束行超出总记录数,结束行就等于总记录数*/
if (endRow >= this.excelModel.getTotalRows()) {
endRow = this.excelModel.getTotalRows();
}
//获取文件路径
String filePath = MessageFormat.format("{0}/{1}_{2}.xls", this.excelModel.getExcelChildFileDir(), uuid, i);
FileInfoModel fileInfoModel = new FileInfoModel();
fileInfoModel.setFilePath(filePath);
fileInfoModel.setBeginRow(beginRow);
fileInfoModel.setEndRow(endRow);
fileInfoModel.setForIndex(i - 1);
return fileInfoModel;
}
/**
* 分割excel
*
* @param fileInfoModel
* @return
*/
private FileInfoModel split(FileInfoModel fileInfoModel) {
File file = new File(fileInfoModel.getFilePath());
try {
jxl.write.WritableWorkbook ww = Workbook.createWorkbook(file);
WritableSheet ws = ww.createSheet(this.excelModel.getSheetName(), 0);
//添加表头
for (int iColumn = 0; iColumn < this.titleCell.length; iColumn++) {
ws.addCell(new Label(iColumn, 0, this.titleCell[iColumn].getContents()));
}
//添加数据到excel中
int rowIndex = 1;
for (int iRow = fileInfoModel.getBeginRow(); iRow <= fileInfoModel.getEndRow(); iRow++, rowIndex++) {
Cell[] cells = this.sheet.getRow(iRow);
for (int iCell = 0; iCell < cells.length; iCell++) {
Cell cell = cells[iCell];
//excel 行的索引需要计算
ws.addCell(new Label(iCell, rowIndex, cell.getContents()));
}
}
ww.write();
ww.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] ary = file.getName().split("\\.");
String fileName = ary[0];
String suffix = ary[1];
fileInfoModel.setFileFullName(file.getName());
fileInfoModel.setFileName(fileName);
fileInfoModel.setFileSuffix("." + suffix);
return fileInfoModel;
}
class ExcelThread implements Runnable {// 任务接口
FileInfoModel returnFile;
public FileInfoModel getReturnFile() {
return returnFile;
}
ExcelThread(FileInfoModel fileInfoModel) {
this.returnFile = fileInfoModel;
}
public void run() {
long beginTime = System.currentTimeMillis();
this.returnFile = split(this.returnFile);
System.out.println(MessageFormat.format("分割文件{0},执行耗时{1}秒", this.returnFile.getForIndex(), (System.currentTimeMillis() - beginTime) / 1000f));
}
}
}
java分割excel文件可用jxl的更多相关文章
- JXL包大解析;Java程序生成excel文件和解析excel文件内容
最近需求变化,需要把excel导入 我以前没有做过,所以我查了一些资料 和参考别人的代码 以下是多种方式: import java.io.File; import java.io.FileInputS ...
- Java读取Excel文件的几种方法
Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...
- java读取excel文件内容
1.导入依赖JAR包 <!-- jxl 操作excel --> <dependency> <groupId>org.jxls</groupId> < ...
- java写入excel文件poi
java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...
- java读取excel文件的代码
如下内容段是关于java读取excel文件的内容,应该能对各朋友有所用途. package com.zsmj.utilit; import java.io.FileInputStream;import ...
- 关于解决java读取excel文件遇空行抛空指针的问题 !
关于解决java读取excel文件遇空行抛空指针的问题 ! package exceRead; import java.io.File; import java.io.FileInputStream; ...
- 基于Java+Selenium的WebUI自动化测试框架(十)-----读取Excel文件(JXL)
之前,我们使用了读取XML文件的方式来实现页面元素的读取,并做成了基础页面类.下面,我们来进行一些扩展,通过Excel来读取页面元素. Excel的使用,大多数人应该都不陌生.那么Java读取Exce ...
- Java导出excel文件(使用jxl)
首先要导入jxl的jar包,可以去maven仓库下载:https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl 通过模拟实现创建 ...
- Java 导入Excel文件到数据库
原文:http://www.jb51.net/article/44021.htm 项目中要求读取excel文件内容,并将其转化为xml格式.常见读取excel文档一般使用POI和JExcelAPI这两 ...
随机推荐
- 【百度地图API】小学生找哥哥——小学生没钱打车,所以此为公交查询功能
原文:[百度地图API]小学生找哥哥--小学生没钱打车,所以此为公交查询功能 任务描述: 有位在魏公村附近上小学的小朋友,要去北京邮电大学找哥哥.他身上钱很少,只够坐公交的.所以,百度地图API快帮帮 ...
- zsh的安装与配置
参考: http://cnbin.github.io/blog/2015/06/01/mac-zsh-an-zhuang-he-shi-yong/ http://www.cnblogs.com/ios ...
- 四大OLAP工具选型浅析
OLAP(在线分析处理)这个名词是在1993年由E.F.Codd提出来的,只是,眼下市场上的主流产品差点儿都是在1993年之前就已出来,有的甚至已有三十多年的历史了.OLAP产品不少,本文将主要涉及C ...
- jmeter之GUI运行原理
一.一语道破jmeter 大家都知道我们在应用jmeter的图形化界面来进行操作,保存后生成的是一个.jmx文件. 那么这个.jmx文件中都是些什么呢. <?xml ve ...
- MySQL的备份与还原
原文:MySQL的备份与还原 MySQL备份和还原,都是利用mysqldump.mysql和source命令来完成的. 1.Win32下MySQL的备份与还原 1.1 备份 开始菜单 | 运行 | c ...
- sql性能
---正在运行的 select a.username, a.sid,b.SQL_TEXT, b.SQL_FULLTEXT from v$session a, v$sqlarea b where a ...
- 一淘搜索网页抓取系统的分析与实现(3)—scrapy+webkit & mysql+django
图 scrapy+webkit: 如结构图③. scrapy不能实现对javascript的处理,所以须要webkit解决问题.开源的解决方式能够选择scrapinghub的scrapyjs或者功能更 ...
- window批量-6 rem
行动: 凝视命令,加大对批量处理相应的描述性信息 格公式: rem [comment] demo: bat @echo off pause rem 这是对pause的解释 echo xxx pause ...
- VMWare Workstation:局域网PC连接虚拟机里的远程桌面或端口
很简单.做一个理解: 1.NAT 2.VM的网卡,相当于路由器 环境: 物理路由器:192.168.0.1 PC1(win):192.168.0.2 PC2(win):192.168.0.3 PC2里 ...
- Invent 2014回顾
AWS re:Invent 2014回顾 亚马逊在2014年11月11-14日的拉斯维加斯举行了一年一度的re:Invent大会.在今年的大会上,亚马逊一股脑发布和更新了很多服务.现在就由我来带领 ...