package com.cx.utils;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ExcelUtil {
private static Sheet initSheet;
static {
initSheet = new Sheet(1, 0);
initSheet.setSheetName("sheet");
//设置自适应宽度
initSheet.setAutoWidth(Boolean.TRUE);
}
/**
* 读取少于1000行数据
*
* @param filePath 文件绝对路径
* @return
*/
public static List<Object> readLessThan1000Row(String filePath) {
return readLessThan1000RowBySheet(filePath, null);
}
/**
* 读小于1000行数据, 带样式
* filePath 文件绝对路径
* initSheet :
* sheetNo: sheet页码,默认为1
* headLineMun: 从第几行开始读取数据,默认为0, 表示从第一行开始读取
* clazz: 返回数据List<Object> 中Object的类名
*/
public static List<Object> readLessThan1000RowBySheet(String filePath, Sheet sheet) {
if (!StringUtils.hasText(filePath)) {
return null;
}
sheet = sheet != null ? sheet : initSheet;
InputStream fileStream = null;
try {
fileStream = new FileInputStream(filePath);
return EasyExcelFactory.read(fileStream, sheet);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (fileStream != null) {
fileStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 读大于1000行数据
*
* @param filePath 文件觉得路径
* @return
*/
public static List<Object> readMoreThan1000Row(String filePath) {
return readMoreThan1000RowBySheet(filePath, null);
}
/**
* 读大于1000行数据, 带样式
*
* @param filePath 文件觉得路径
* @return
*/
public static List<Object> readMoreThan1000RowBySheet(String filePath, Sheet sheet) {
if (!StringUtils.hasText(filePath)) {
return null;
}
sheet = sheet != null ? sheet : initSheet;
InputStream fileStream = null;
try {
fileStream = new FileInputStream(filePath);
ExcelListener excelListener = new ExcelListener();
EasyExcelFactory.readBySax(fileStream, sheet, excelListener);
return excelListener.getDatas();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (fileStream != null) {
fileStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 生成excle
*
* @param filePath 绝对路径, 如:/home/chenmingjian/Downloads/aaa.xlsx
* @param data 数据源
* @param head 表头
*/
public static void writeBySimple(String filePath, List<List<Object>> data, List<String> head) {
writeSimpleBySheet(filePath, data, head, null);
}
/**
* 生成excle
*
* @param filePath 绝对路径, 如:/home/chenmingjian/Downloads/aaa.xlsx
* @param data 数据源
* @param sheet excle页面样式
* @param head 表头
*/
public static void writeSimpleBySheet(String filePath, List<List<Object>> data, List<String> head, Sheet sheet) {
sheet = (sheet != null) ? sheet : initSheet;
if (head != null) {
List<List<String>> list = new ArrayList<>();
head.forEach(h -> list.add(Collections.singletonList(h)));
sheet.setHead(list);
}
OutputStream outputStream = null;
ExcelWriter writer = null;
try {
outputStream = new FileOutputStream(filePath);
writer = EasyExcelFactory.getWriter(outputStream);
writer.write1(data, sheet);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.finish();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 生成excle
*
* @param filePath 绝对路径, 如:/home/chenmingjian/Downloads/aaa.xlsx
* @param data 数据源
*/
public static void writeWithTemplate(String filePath, List<? extends BaseRowModel> data) {
writeWithTemplateAndSheet(filePath, data, null);
}
/**
* 生成excle
*
* @param filePath 绝对路径, 如:/home/chenmingjian/Downloads/aaa.xlsx
* @param data 数据源
* @param sheet excle页面样式
*/
public static void writeWithTemplateAndSheet(String filePath, List<? extends BaseRowModel> data, Sheet sheet) {
if (CollectionUtils.isEmpty(data)) {
return;
}
sheet = (sheet != null) ? sheet : initSheet;
sheet.setClazz(data.get(0).getClass());
OutputStream outputStream = null;
ExcelWriter writer = null;
try {
outputStream = new FileOutputStream(filePath);
writer = EasyExcelFactory.getWriter(outputStream);
writer.write(data, sheet);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.finish();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 生成多Sheet的excle
* @param filePath 绝对路径, 如:/home/chenmingjian/Downloads/aaa.xlsx
* @param multipleSheelPropetys
*/
public static void writeWithMultipleSheel(String filePath, List<MultipleSheelPropety> multipleSheelPropetys) {
if (CollectionUtils.isEmpty(multipleSheelPropetys)) {
return;
}
OutputStream outputStream = null;
ExcelWriter writer = null;
try {
outputStream = new FileOutputStream(filePath);
writer = EasyExcelFactory.getWriter(outputStream);
for (MultipleSheelPropety multipleSheelPropety : multipleSheelPropetys) {
Sheet sheet = multipleSheelPropety.getSheet() != null ? multipleSheelPropety.getSheet() : initSheet;
if (!CollectionUtils.isEmpty(multipleSheelPropety.getData())) {
sheet.setClazz(multipleSheelPropety.getData().get(0).getClass());
}
sheet.setAutoWidth(true);
writer.write(multipleSheelPropety.getData(), sheet);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.finish();
}
没有合适的资源?快使用搜索试试~ 我知道了~
小程序商城数据收集.rar
共67个文件
class:24个
xml:13个
java:12个
需积分: 5 0 下载量 60 浏览量
2020-07-16
15:13:14
上传
评论
收藏 123.52MB RAR 举报
温馨提示
小程序商城数据收集.rar
资源推荐
资源详情
资源评论
收起资源包目录
java爬虫.rar (67个子文件)
BaJie
pom.xml 2KB
lwj
lwj.iml 1KB
pom.xml 3KB
target
maven-status
maven-compiler-plugin
compile
default-compile
inputFiles.lst 465B
createdFiles.lst 270B
testCompile
default-testCompile
inputFiles.lst 0B
lwj-1.0-SNAPSHOT.jar 36.24MB
lwj-1.0-SNAPSHOT.jar.original 17KB
classes
com
cx
lwj
LwjProtocol.class 2KB
GetGoodInfo.class 3KB
GetListGoods.class 3KB
GetSecondCateList.class 3KB
LwjTest.class 6KB
utils
ExcelUtil$MultipleSheelPropety.class 1KB
ExcelUtil.class 9KB
ExcelUtil$ExcelListener.class 1KB
maven-archiver
pom.properties 91B
generated-sources
annotations
src
test
java
main
resources
java
META-INF
MANIFEST.MF 57B
com
cx
lwj
GetSecondCateList.java 2KB
LwjProtocol.java 1KB
GetListGoods.java 2KB
LwjTest.java 6KB
GetGoodInfo.java 2KB
utils
ExcelUtil.java 10KB
src
test
java
main
resources
java
META-INF
com
.idea
uiDesigner.xml 9KB
misc.xml 656B
jarRepositories.xml 880B
modules.xml 261B
encodings.xml 530B
compiler.xml 933B
workspace.xml 12KB
.gitignore 0B
artifacts
yx_jar.xml 10KB
lwj_jar.xml 10KB
inspectionProfiles
Project_Default.xml 1KB
yx
pom.xml 3KB
target
maven-status
maven-compiler-plugin
compile
default-compile
inputFiles.lst 362B
createdFiles.lst 220B
testCompile
default-testCompile
inputFiles.lst 0B
yx-1.0-SNAPSHOT.jar 36.24MB
classes
com
cx
yx
GetToken.class 5KB
YxProtocol.class 2KB
GetTopicId.class 2KB
yxTest.class 7KB
GetJsonObj.class 3KB
utils
ExcelUtil$MultipleSheelPropety.class 1KB
ExcelUtil.class 9KB
ExcelUtil$ExcelListener.class 1KB
yx-1.0-SNAPSHOT.jar.original 14KB
maven-archiver
pom.properties 90B
generated-sources
annotations
src
test
java
main
resources
java
com
cx
yx
GetJsonObj.java 2KB
yxTest.java 6KB
YxProtocol.java 1KB
GetTopicId.java 1KB
GetToken.java 3KB
utils
ExcelUtil.java 10KB
out
production
lwj
META-INF
MANIFEST.MF 57B
com
cx
lwj
LwjProtocol.class 2KB
GetGoodInfo.class 3KB
GetListGoods.class 3KB
GetSecondCateList.class 3KB
LwjTest.class 6KB
utils
ExcelUtil$MultipleSheelPropety.class 1KB
ExcelUtil.class 9KB
ExcelUtil$ExcelListener.class 1KB
artifacts
yx_jar
yx.jar 36.14MB
lwj_jar
lwj.jar 36.14MB
共 67 条
- 1
资源评论
Canxue_java
- 粉丝: 3
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Python实现的自动化办公项目.zip
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功