pom

        <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency> <dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>xlsx-streamer</artifactId>
<version>1.2.1</version>
</dependency>
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import com.chinaway.g7s.common.G7sException;
import com.chinaway.g7s.service.BaseService;
import com.chinaway.intelli.intelli_core.common.ErrorEnum;
import com.github.pagehelper.PageInfo;
import com.monitorjbl.xlsx.StreamingReader; /**
* excel导入导出工具类
* @author shihaiming
*
*/
public class ExcelUtil { private static Logger log = LoggerFactory.getLogger(ExcelUtil.class); /**
* @param filePath 文件保存绝对路径+文件名称
* @param params 分页查询数据的条件
* @param baseService 相关service
* @param cls 相关实体类
* @param titles excel表头
* @param headFields 导出的实体类的字段名
* @param isDownload false时不弹出下载文件,为true时浏览器会弹出下载文件
* @throws Exception
*/
public static void exportExcel(String filePath, Map<String, Object> params, BaseService baseService, Class cls, String[] titles, String[] headFields, boolean isDownload) throws Exception {
long startTime = System.currentTimeMillis(); // 开始时间
Sheet sheet = null; // 工作表对象
Row nRow = null; // 行对象
Cell nCell = null; // 列对象 int pageSize = 10000; //每次查询的数据量
params.put("pageSize", pageSize);
params.put("pageNum", 1);
List list = baseService.findByPage(params); PageInfo pageInfo = new PageInfo(list);
List dataList = pageInfo.getList();
List<Map<String, Object>> dataMap = convertListBeanToListMap(dataList, cls); // 内存中只创建500个对象,写临时文件,当超过500条,就将内存中不用的对象释放。
Workbook wb = new SXSSFWorkbook(500); // 关键语句
Font font = wb.createFont();
font.setBold(true);
CellStyle headCellStyle = wb.createCellStyle();
headCellStyle.setFont(font); int total = (int) pageInfo.getTotal();
//每个sheet保存多少行
int sheetNum = 1000000;
int rowNo = 0; // 总行号
int pageRowNo = 0; // 页行号 // 根据行数求数据提取次数
int export_times = total % pageSize > 0 ? total / pageSize + 1 :total / pageSize; if(export_times > 0){
for (int i = 1; i <= export_times; i++) {
for (int n = 0; n < dataMap.size(); n++) {
//打印300000条后切换到下个工作表,可根据需要自行拓展,2百万,3百万...数据一样操作,只要不超过1048576就可以
if(rowNo%sheetNum==0){
sheet = wb.createSheet("第"+(rowNo/sheetNum+1)+"个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(rowNo/sheetNum); //动态指定当前的工作表
pageRowNo = 0; //每当新建了工作表就将当前工作表的行号重置为0 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
} nRow = sheet.createRow(pageRowNo++); //新建行对象 // 打印每行, 列属性的个数
for(int j=0;j<headFields.length;j++){
nCell = nRow.createCell(j);
nCell.setCellValue(dataMap.get(n).get(headFields[j])==null?"":dataMap.get(n).get(headFields[j]).toString());
}
rowNo++;
} dataList.clear();
params.put("pageNum", i+1);
params.put("count", false);//导出时,只让第一次分页查询执行count,后面的查询不执行count,加快执行速度
list = baseService.findByPage(params);
pageInfo = new PageInfo(list);
dataList = pageInfo.getList();
dataMap = convertListBeanToListMap(dataList, cls); //list<Bean>转成list<Map>,方便通用各个模块
}
} else {
sheet = wb.createSheet("第"+(rowNo/sheetNum+1)+"个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(rowNo/sheetNum); //动态指定当前的工作表
pageRowNo = 0; //每当新建了工作表就将当前工作表的行号重置为0 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
nRow = sheet.createRow(pageRowNo++); //新建行对象
} long finishedTime = System.currentTimeMillis(); // 处理完成时间
log.info("exportExcel finished execute time: {}s", (finishedTime - startTime) / 1000); FileOutputStream fOut = new FileOutputStream(filePath);
if (fOut != null) {
try {
wb.write(fOut);
fOut.flush(); // 刷新缓冲区
} catch (Exception e) {
e.printStackTrace();
}
fOut.close();
}
if (wb != null){
try {
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
} long stopTime = System.currentTimeMillis(); // 写文件时间
log.info("exportExcel write xlsx file time: {}s", (stopTime - startTime) / 1000 ); //isDownload为true时,让浏览器弹出下载文件
if (isDownload) {
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
try {
File file = new File(filePath);
String fileName = file.getName();// 获取日志文件名称
InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
//然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
// 判断是否火狐浏览器
String agent = request.getHeader("User-Agent");
boolean isFirefox = (agent != null && agent.contains("Firefox"));
if (isFirefox) {
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
} else {
fileName = URLEncoder.encode(fileName, "UTF8");
}
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + file.length());
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);// 输出文件
os.flush();
os.close();
} catch (Exception e) {
log.error("exportExcel download出错: {}", e.getMessage());
}
}
} public static String exportExcelAndUpload(String fileName, Map<String, Object> params, BaseService baseService, Class cls, String[] titles, String[] headFields, boolean isDownload) throws Exception {
String path = null;
long startTime = System.currentTimeMillis(); // 开始时间
Sheet sheet = null; // 工作表对象
Row nRow = null; // 行对象
Cell nCell = null; // 列对象 int pageSize = 10000; //每次查询的数据量
params.put("pageSize", pageSize);
params.put("pageNum", 1);
List list = baseService.findByPage(params); PageInfo pageInfo = new PageInfo(list);
List dataList = pageInfo.getList();
List<Map<String, Object>> dataMap = convertListBeanToListMap(dataList, cls); // 内存中只创建500个对象,写临时文件,当超过500条,就将内存中不用的对象释放。
Workbook wb = new SXSSFWorkbook(500); // 关键语句
Font font = wb.createFont();
font.setBold(true);
CellStyle headCellStyle = wb.createCellStyle();
headCellStyle.setFont(font); int total = (int) pageInfo.getTotal();
//每个sheet保存多少行
int sheetNum = 1000000;
int rowNo = 0; // 总行号
int pageRowNo = 0; // 页行号 // 根据行数求数据提取次数
int export_times = total % pageSize > 0 ? total / pageSize + 1 :total / pageSize; if(export_times > 0){
for (int i = 1; i <= export_times; i++) {
for (int n = 0; n < dataMap.size(); n++) {
//打印300000条后切换到下个工作表,可根据需要自行拓展,2百万,3百万...数据一样操作,只要不超过1048576就可以
if(rowNo%sheetNum==0){
sheet = wb.createSheet("第"+(rowNo/sheetNum+1)+"个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(rowNo/sheetNum); //动态指定当前的工作表
pageRowNo = 0; //每当新建了工作表就将当前工作表的行号重置为0 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
} nRow = sheet.createRow(pageRowNo++); //新建行对象 // 打印每行, 列属性的个数
for(int j=0;j<headFields.length;j++){
nCell = nRow.createCell(j);
nCell.setCellValue(dataMap.get(n).get(headFields[j])==null?"":dataMap.get(n).get(headFields[j]).toString());
}
rowNo++;
} dataList.clear();
params.put("pageNum", i+1);
params.put("count", false);//导出时,只让第一次分页查询执行count,后面的查询不执行count,加快执行速度
list = baseService.findByPage(params);
pageInfo = new PageInfo(list);
dataList = pageInfo.getList();
dataMap = convertListBeanToListMap(dataList, cls); //list<Bean>转成list<Map>,方便通用各个模块
}
} else {
sheet = wb.createSheet("第"+(rowNo/sheetNum+1)+"个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(rowNo/sheetNum); //动态指定当前的工作表
pageRowNo = 0; //每当新建了工作表就将当前工作表的行号重置为0 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
nRow = sheet.createRow(pageRowNo++); //新建行对象
} long finishedTime = System.currentTimeMillis(); // 处理完成时间
log.info("exportExcel finished execute time: {}s", (finishedTime - startTime) / 1000); long stopTime = System.currentTimeMillis(); // 写文件时间
log.info("exportExcel write xlsx file time: {}s", (stopTime - startTime) / 1000 ); ByteArrayOutputStream bos = null;
InputStream is = null;
try {
bos = new ByteArrayOutputStream();
wb.write(bos);
byte[] barray = bos.toByteArray();
is = new ByteArrayInputStream(barray);
path = OSSUtil.upload(is, fileName, "xlsx");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (wb != null){
try {
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return path;
} public static String exportExcelAndUploadOSS(String fileName, Map<String, Object> params, BaseService baseService, Class cls, String[] titles, String[] headFields, boolean isDownload) throws Exception {
String path = null;
long startTime = System.currentTimeMillis(); // 开始时间
Sheet sheet = null; // 工作表对象
Row nRow = null; // 行对象
Cell nCell = null; // 列对象 int pageSize = 10000; //每次查询的数据量
params.put("pageSize", pageSize);
params.put("pageNum", 1);
List list = baseService.findByPage(params); PageInfo pageInfo = new PageInfo(list);
List dataList = pageInfo.getList();
List<Map<String, Object>> dataMap = convertListBeanToListMap(dataList, cls); // 内存中只创建500个对象,写临时文件,当超过500条,就将内存中不用的对象释放。
Workbook wb = new SXSSFWorkbook(500); // 关键语句
Font font = wb.createFont();
font.setBold(true);
CellStyle headCellStyle = wb.createCellStyle();
headCellStyle.setFont(font); int total = (int) pageInfo.getTotal();
//每个sheet保存多少行
int sheetNum = 1000000;
int rowNo = 0; // 总行号
int pageRowNo = 0; // 页行号 // 根据行数求数据提取次数
int export_times = total % pageSize > 0 ? total / pageSize + 1 :total / pageSize; if(export_times > 0){
for (int i = 1; i <= export_times; i++) {
for (int n = 0; n < dataMap.size(); n++) {
//打印300000条后切换到下个工作表,可根据需要自行拓展,2百万,3百万...数据一样操作,只要不超过1048576就可以
if(rowNo%sheetNum==0){
sheet = wb.createSheet("第"+(rowNo/sheetNum+1)+"个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(rowNo/sheetNum); //动态指定当前的工作表
pageRowNo = 0; //每当新建了工作表就将当前工作表的行号重置为0 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
} nRow = sheet.createRow(pageRowNo++); //新建行对象 // 打印每行, 列属性的个数
for(int j=0;j<headFields.length;j++){
nCell = nRow.createCell(j);
nCell.setCellValue(dataMap.get(n).get(headFields[j])==null?"":dataMap.get(n).get(headFields[j]).toString());
}
rowNo++;
} dataList.clear();
params.put("pageNum", i+1);
params.put("count", false);//导出时,只让第一次分页查询执行count,后面的查询不执行count,加快执行速度
list = baseService.findByPage(params);
pageInfo = new PageInfo(list);
dataList = pageInfo.getList();
dataMap = convertListBeanToListMap(dataList, cls); //list<Bean>转成list<Map>,方便通用各个模块
}
} else {
sheet = wb.createSheet("第"+(rowNo/sheetNum+1)+"个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(rowNo/sheetNum); //动态指定当前的工作表
pageRowNo = 0; //每当新建了工作表就将当前工作表的行号重置为0 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
nRow = sheet.createRow(pageRowNo++); //新建行对象
} long finishedTime = System.currentTimeMillis(); // 处理完成时间
log.info("exportExcel finished execute time: {}s", (finishedTime - startTime) / 1000); long stopTime = System.currentTimeMillis(); // 写文件时间
log.info("exportExcel write xlsx file time: {}s", (stopTime - startTime) / 1000 );
ByteArrayOutputStream bos = null;
InputStream is = null;
try {
bos = new ByteArrayOutputStream();
wb.write(bos);
byte[] barray = bos.toByteArray();
is = new ByteArrayInputStream(barray);
path = OSSUtil.upload(is, fileName, "xlsx");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (wb != null){
try {
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return path;
} public static String exportExcelAndUploadOSS(String fileName, Map<String, Object> params, BaseService baseService, String methodName, Class cls, String[] titles, String[] headFields, boolean isDownload) throws Exception {
String path = null;
long startTime = System.currentTimeMillis(); // 开始时间
Sheet sheet = null; // 工作表对象
Row nRow = null; // 行对象
Cell nCell = null; // 列对象 int pageSize = 10000; //每次查询的数据量
params.put("pageSize", pageSize);
params.put("pageNum", 1); Class clz = baseService.getClass();
Object obj = clz.newInstance();
//获取方法
Method m = clz.getDeclaredMethod(methodName, Map.class);
//调用方法
List list = (List) m.invoke(obj, params); // Method method2 = baseService.getClass().getMethod("methodName", Class.forName("java.util.Map"));
// List list = (List) method2.invoke( params);
// List list = baseService.findByPage(params); PageInfo pageInfo = new PageInfo(list);
List dataList = pageInfo.getList();
List<Map<String, Object>> dataMap = convertListBeanToListMap(dataList, cls); // 内存中只创建500个对象,写临时文件,当超过500条,就将内存中不用的对象释放。
Workbook wb = new SXSSFWorkbook(500); // 关键语句
Font font = wb.createFont();
font.setBold(true);
CellStyle headCellStyle = wb.createCellStyle();
headCellStyle.setFont(font); int total = (int) pageInfo.getTotal();
//每个sheet保存多少行
int sheetNum = 1000000;
int rowNo = 0; // 总行号
int pageRowNo = 0; // 页行号 // 根据行数求数据提取次数
int export_times = total % pageSize > 0 ? total / pageSize + 1 :total / pageSize; if(export_times > 0){
for (int i = 1; i <= export_times; i++) {
for (int n = 0; n < dataMap.size(); n++) {
//打印300000条后切换到下个工作表,可根据需要自行拓展,2百万,3百万...数据一样操作,只要不超过1048576就可以
if(rowNo%sheetNum==0){
sheet = wb.createSheet("第"+(rowNo/sheetNum+1)+"个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(rowNo/sheetNum); //动态指定当前的工作表
pageRowNo = 0; //每当新建了工作表就将当前工作表的行号重置为0 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
} nRow = sheet.createRow(pageRowNo++); //新建行对象 // 打印每行, 列属性的个数
for(int j=0;j<headFields.length;j++){
nCell = nRow.createCell(j);
nCell.setCellValue(dataMap.get(n).get(headFields[j])==null?"":dataMap.get(n).get(headFields[j]).toString());
}
rowNo++;
} dataList.clear();
params.put("pageNum", i+1);
params.put("count", false);//导出时,只让第一次分页查询执行count,后面的查询不执行count,加快执行速度
list = (List) m.invoke(obj, params);
pageInfo = new PageInfo(list);
dataList = pageInfo.getList();
dataMap = convertListBeanToListMap(dataList, cls); //list<Bean>转成list<Map>,方便通用各个模块
}
} else {
sheet = wb.createSheet("第"+(rowNo/sheetNum+1)+"个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(rowNo/sheetNum); //动态指定当前的工作表
pageRowNo = 0; //每当新建了工作表就将当前工作表的行号重置为0 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
nRow = sheet.createRow(pageRowNo++); //新建行对象
} long finishedTime = System.currentTimeMillis(); // 处理完成时间
log.info("exportExcel finished execute time: {}s", (finishedTime - startTime) / 1000); long stopTime = System.currentTimeMillis(); // 写文件时间
log.info("exportExcel write xlsx file time: {}s", (stopTime - startTime) / 1000 );
ByteArrayOutputStream bos = null;
InputStream is = null;
try {
bos = new ByteArrayOutputStream();
wb.write(bos);
byte[] barray = bos.toByteArray();
is = new ByteArrayInputStream(barray);
path = OSSUtil.upload(is, fileName, "xlsx");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (wb != null){
try {
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return path;
} public static void main(String[] args) throws Exception {
long beginTime = System.currentTimeMillis();
// List[] list = getDataFromExcel("G:"+ File.separator +"初始化业务员8 - 副本.xlsx", 0);
// List[] list = getDataFromExcelByCache("G:"+ File.separator +"初始化业务员8.xlsx", 0);
// List[] list = getDataFromExcelByCache("G:"+ File.separator +"初始化业务员8 - 副本.xlsx", 0);
long endTime = System.currentTimeMillis();
log.info("Cast time : " + (endTime - beginTime)); // if (list != null && list.length > 0){
// for (int i = 0; i < list.length; i++) {
// System.out.println(list[i]);
// }
// } } /**
* 读取出filePath中的所有数据信息
* @param filePath excel文件的绝对路径
* @param sheetIndex 第几个sheet
* @throws IOException
*
*/
public static List[] getDataFromExcelByCache(InputStream fis, int sheetIndex) throws Exception {
List[] data = null;
Workbook wookbook = null; try {
//获取一个绝对地址的流
// fis = new FileInputStream(filePath); wookbook = StreamingReader.builder()
.rowCacheSize(100) //缓存到内存中的行数,默认是10
.bufferSize(4096) //读取资源时,缓存到内存的字节大小,默认是1024
.open(fis); //打开资源,必须,可以是InputStream或者是File,注意:只能打开XLSX格式的文件
Sheet sheet = wookbook.getSheetAt(sheetIndex); //获得数据的总行数
int totalRowNum = sheet.getLastRowNum();
log.info("====总行数"+totalRowNum);
// if (totalRowNum > 200000) {
// throw new G7sException(ErrorEnum.ERROR_DEFAULT.getErrorCode(), "Excel读取数量不能大于200000行");
// } data = new List[totalRowNum];
List<Object> rowData = null;
int totalCell = 0;
//遍历所有的行
for (Row row : sheet) {
if(row.getRowNum() == 0) {
totalCell = row.getLastCellNum();
}
//遍历所有的列
if (row.getRowNum() > 0) {
rowData = new ArrayList<Object>();
for (int i = 0; i < totalCell; i++) {
if(row == null || row.getCell(i) == null) {
rowData.add("");
} else {
rowData.add(row.getCell(i).getStringCellValue());
}
}
data[row.getRowNum()-1] = rowData;
}
}
} catch (Exception e) {
log.error("excel读取报错:{}", e.getMessage());
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (wookbook != null) {
try {
wookbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return data;
} /**
* 读取出filePath中的所有数据信息
* @param filePath excel文件的绝对路径
* @param sheetIndex 第几个sheet
* @throws IOException
*
*/
public static List[] getDataFromExcel(String filePath, int sheetIndex) throws IOException {
List[] data = null;
//判断是否为excel类型文件
if(!filePath.endsWith(".xls")&&!filePath.endsWith(".xlsx")) {
throw new G7sException(ErrorEnum.ERROR_DEFAULT.getErrorCode(), "Excel格式无效");
} FileInputStream fis =null;
Workbook wookbook = null; try {
//获取一个绝对地址的流
fis = new FileInputStream(filePath); if(filePath.endsWith(".xls")) {
//2003版本的excel,用.xls结尾
wookbook = new HSSFWorkbook(fis);
} else if(filePath.endsWith(".xlsx")) {
//2007版本的excel,用.xlsx结尾
wookbook = new XSSFWorkbook(fis);
} //得到一个工作表
Sheet sheet = wookbook.getSheetAt(sheetIndex); //获得表头
Row rowHead = sheet.getRow(0); log.info("表头的数量:" + rowHead.getPhysicalNumberOfCells()); //获得数据的总行数
int totalRowNum = sheet.getLastRowNum();
log.info("总数量:"+totalRowNum);
// if (totalRowNum > 200000) {
// throw new G7sException(ErrorEnum.ERROR_DEFAULT.getErrorCode(), "Excel读取数量不能大于200000行");
// }
data = new List[totalRowNum];
List<Object> rowData = null;
//获得所有数据
for(int i = 1 ; i <= totalRowNum ; i++) {
//获得第i行对象
Row row = sheet.getRow(i);
// //获得获得第i行第0列的 String类型对象
// Cell cell = row.getCell((short)0);
// name = cell.getStringCellValue().toString();
//
// //获得一个数字类型的数据
// cell = row.getCell((short)1);
// latitude = cell.getStringCellValue(); rowData = new ArrayList<Object>();
for (int k = 0; k < row.getLastCellNum(); k++) {
Cell cell = row.getCell((short)k);
rowData.add(cell);
}
data[i-1] = rowData;
}
} catch (Exception e) {
log.error("excel读取报错:{}", e.getMessage());
} finally {
if (fis != null) {
fis.close();
}
if (wookbook != null) {
wookbook.close();
}
}
return data;
} /**
* 将 List<JavaBean>对象转化为List<Map>
* @param beanList
* @return
* @throws Exception
*/
public static <T> List<Map<String, Object>> convertListBeanToListMap(List<T> beanList, Class<T> T)
throws Exception {
List<Map<String, Object>> mapList = new ArrayList<>();
for (int i = 0, n = beanList.size(); i < n; i++) {
Object bean = beanList.get(i);
Map<String, Object> map = convertBeanToMap(bean);
mapList.add(map);
}
return mapList;
} /**
* 将一个 JavaBean 对象转化为一个 Map
* @param bean
* @return
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Map<String, Object> convertBeanToMap(Object bean)
throws IntrospectionException, IllegalAccessException, InvocationTargetException
{
Class<? extends Object> type = bean.getClass();
Map<String, Object> returnMap = new HashMap<>();
BeanInfo beanInfo = Introspector.getBeanInfo(type); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++)
{
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!"class".equals(propertyName))
{
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null)
{
returnMap.put(propertyName, result);
}
else
{
returnMap.put(propertyName, null);
}
}
}
return returnMap;
} public static void exportExcel(String filePath, List list, Class cls, String[] titles, String[] headFields, boolean isDownload) throws Exception {
long startTime = System.currentTimeMillis(); // 开始时间
Sheet sheet = null; // 工作表对象
Row nRow = null; // 行对象
Cell nCell = null; // 列对象 // 内存中只创建500个对象,写临时文件,当超过500条,就将内存中不用的对象释放。
Workbook wb = new SXSSFWorkbook(500); // 关键语句
Font font = wb.createFont();
font.setBold(true);
CellStyle headCellStyle = wb.createCellStyle();
headCellStyle.setFont(font); //每个sheet保存多少行
int sheetNum = 1000000;
int rowNo = 0; // 总行号
int pageRowNo = 0; // 页行号 if(list != null && list.size() > 0) {
PageInfo pageInfo = new PageInfo(list);
List dataList = pageInfo.getList();
List<Map<String, Object>> dataMap = convertListBeanToListMap(dataList, cls); for (int n = 0; n < dataMap.size(); n++) {
//打印300000条后切换到下个工作表,可根据需要自行拓展,2百万,3百万...数据一样操作,只要不超过1048576就可以
if(rowNo%sheetNum==0){
sheet = wb.createSheet("第"+(rowNo/sheetNum+1)+"个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(rowNo/sheetNum); //动态指定当前的工作表
pageRowNo = 0; //每当新建了工作表就将当前工作表的行号重置为0 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
} nRow = sheet.createRow(pageRowNo++); //新建行对象 // 打印每行, 列属性的个数
for(int j=0;j<headFields.length;j++){
nCell = nRow.createCell(j);
nCell.setCellValue(dataMap.get(n).get(headFields[j])==null?"":dataMap.get(n).get(headFields[j]).toString());
}
rowNo++;
}
dataList.clear();
long finishedTime = System.currentTimeMillis(); // 处理完成时间
log.info("exportExcel finished execute time: {}s", (finishedTime - startTime) / 1000); } else {
sheet = wb.createSheet("第1个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(0); //动态指定当前的工作表 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
nRow = sheet.createRow(pageRowNo++); //新建行对象
} FileOutputStream fOut = new FileOutputStream(filePath);
if (fOut != null) {
try {
wb.write(fOut);
fOut.flush(); // 刷新缓冲区
} catch (Exception e) {
e.printStackTrace();
}
fOut.close();
}
if (wb != null){
try {
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
} long stopTime = System.currentTimeMillis(); // 写文件时间
log.info("exportExcel write xlsx file time: {}s", (stopTime - startTime) / 1000 ); //isDownload为true时,让浏览器弹出下载文件
if (isDownload) {
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
try {
File file = new File(filePath);
String fileName = file.getName();// 获取日志文件名称
InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
//然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
// 判断是否火狐浏览器
String agent = request.getHeader("User-Agent");
boolean isFirefox = (agent != null && agent.contains("Firefox"));
if (isFirefox) {
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
} else {
fileName = URLEncoder.encode(fileName, "UTF8");
}
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + file.length());
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);// 输出文件
os.flush();
os.close();
} catch (Exception e) {
log.error("exportExcel download出错: {}", e.getMessage());
}
}
} public static String exportListExcelAndUpload(String fileName, List list, Class cls, String[] titles, String[] headFields, boolean isDownload) throws Exception {
long startTime = System.currentTimeMillis(); // 开始时间
Sheet sheet = null; // 工作表对象
Row nRow = null; // 行对象
Cell nCell = null; // 列对象
String path = null; // 内存中只创建500个对象,写临时文件,当超过500条,就将内存中不用的对象释放。
Workbook wb = new SXSSFWorkbook(500); // 关键语句
Font font = wb.createFont();
font.setBold(true);
CellStyle headCellStyle = wb.createCellStyle();
headCellStyle.setFont(font); //每个sheet保存多少行
int sheetNum = 1000000;
int rowNo = 0; // 总行号
int pageRowNo = 0; // 页行号 if(list != null && list.size() > 0) {
PageInfo pageInfo = new PageInfo(list);
List dataList = pageInfo.getList();
List<Map<String, Object>> dataMap = convertListBeanToListMap(dataList, cls); for (int n = 0; n < dataMap.size(); n++) {
//打印300000条后切换到下个工作表,可根据需要自行拓展,2百万,3百万...数据一样操作,只要不超过1048576就可以
if(rowNo%sheetNum==0){
sheet = wb.createSheet("第"+(rowNo/sheetNum+1)+"个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(rowNo/sheetNum); //动态指定当前的工作表
pageRowNo = 0; //每当新建了工作表就将当前工作表的行号重置为0 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
} nRow = sheet.createRow(pageRowNo++); //新建行对象 // 打印每行, 列属性的个数
for(int j=0;j<headFields.length;j++){
nCell = nRow.createCell(j);
nCell.setCellValue(dataMap.get(n).get(headFields[j])==null?"":dataMap.get(n).get(headFields[j]).toString());
}
rowNo++;
}
dataList.clear();
long finishedTime = System.currentTimeMillis(); // 处理完成时间
log.info("exportExcel finished execute time: {}s", (finishedTime - startTime) / 1000); } else {
sheet = wb.createSheet("第1个工作簿");//建立新的sheet对象
sheet = wb.getSheetAt(0); //动态指定当前的工作表 Row row = sheet.createRow(pageRowNo++); //新建行对象
Cell cell = row.createCell(0);
for (int k = 0; k < titles.length; k++) {
cell = row.createCell(k);
cell.setCellStyle(headCellStyle);
cell.setCellValue(new XSSFRichTextString(titles[k]));
}
nRow = sheet.createRow(pageRowNo++); //新建行对象
} long stopTime = System.currentTimeMillis(); // 写文件时间
log.info("exportListExcelAndUpload write xlsx file time: {}s", (stopTime - startTime) / 1000 ); ByteArrayOutputStream bos = null;
InputStream is = null;
try {
bos = new ByteArrayOutputStream();
wb.write(bos);
byte[] barray = bos.toByteArray();
is = new ByteArrayInputStream(barray);
path = OSSUtil.upload(is, fileName, "xlsx");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (wb != null){
try {
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return path;
} }

poi excel导入导出的更多相关文章

  1. Java POI Excel 导入导出

    这个东西很容易懂,不是特别难,难就难在一些复杂的计算和Excel格式的调整上. 近期写了一个小列子,放上来便于以后使用. POI.jar下载地址:http://mirror.bit.edu.cn/ap ...

  2. POI Excel 导入导出重点

    HSSF是指2007年以前的,XSSF是指2007年版本以上的 这个还是比较好用的,这些总结来自Apache的官方向导的点点滴滴 详细的请参考http://poi.apache.org/spreads ...

  3. 一个基于POI的通用excel导入导出工具类的简单实现及使用方法

    前言: 最近PM来了一个需求,简单来说就是在录入数据时一条一条插入到系统显得非常麻烦,让我实现一个直接通过excel导入的方法一次性录入所有数据.网上关于excel导入导出的例子很多,但大多相互借鉴. ...

  4. Java之POI的excel导入导出

    一.Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件.这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Offic ...

  5. 基于 POI 封装 ExcelUtil 精简的 Excel 导入导出

    注 本文是使用 org.apache.poi 进行一次简单的封装,适用于大部分 excel 导入导出功能.过程中可能会用到反射,如若有对于性能有极致强迫症的同学,看看就好. 序 由于 poi 本身只是 ...

  6. SpringBoot集成文件 - 集成POI之Excel导入导出

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能.本文主要介绍通过Spr ...

  7. java简易excel导入导出工具(封装POI)

    Octopus 如何导入excel 如何导出excel github项目地址 Octopus Octopus 是一个简单的java excel导入导出工具. 如何导入excel 下面是一个excel文 ...

  8. Octopus——excel导入导出工具

    Octopus Octopus是一个简易的Excel导入导出工具.目前主要就两个功能: 导入:将excel中一行数据转换为指定的java对象,并通过指定的正则表达式检查合法性. 导出:按照给定的xml ...

  9. 土制Excel导入导出及相关问题探讨

    转载请注明出处https://www.cnblogs.com/funnyzpc/p/10392085.html 新的一年,又一个开始,不见收获,却见年龄,好一个猪年,待我先来一首里尔克的诗: < ...

随机推荐

  1. pip --version问题

    安装pip之后,在cmd下输入 pip --version始终提示: Unknown option:versionDid not provide a command自己安装步骤没错,怎么想也不明白,无 ...

  2. 003_vim使用tip

    vim 使用tip 编写python程序 自动插入头信息: #!/usr/bin/env python # coding=utf-8 输入.或按TAB键会触发代码补全功能 :w保存代码之后会自动检查代 ...

  3. 四、vue语法补充

    1.自定义过滤器 格式: {{ msg | filters}} 2.computed 属性默认只有 getter ,不过在需要时你也可以提供一个 setter <!DOCTYPE html> ...

  4. MongoDB 进阶模式设计

    原文链接:http://www.mongoing.com/mongodb-advanced-pattern-design 12月12日上午,TJ在开源中国的年终盛典会上分享了文档模型设计的进阶技巧,就 ...

  5. [转]Kubernetes TLS bootstrapping 那点事

    这个写得确实专业, 转一下收藏. https://mritd.me/2018/01/07/kubernetes-tls-bootstrapping-note/ 阅读本文章前,请先阅读一下本文参考的相关 ...

  6. dojo/domReady! 中感叹号的作用

    废话 其实不算个技术问题,但实在是花了我不少时间,不记下来都对不起我这浪费掉的几十分钟. 问题 在dojo官网上看教程,跟着做点练习,看到Dojo DOM Functions那节,有一个练习是改变页面 ...

  7. phpinfo常见配置信息

    在开发过程中,经常碰到比如加载的是哪个配置文件.上传文件大小受限.PHP错误日志文件位置等问题需要快速查找出来并解决,因此总结记录出下面的相关配置. phpinfo - 输出关于 PHP 配置的信息 ...

  8. 001.LVM简介

    一 概念 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauelshagen在Linux 2.4内核上实现.LVM将一个或多个硬盘的分区在逻辑上集合,相 ...

  9. MVC+easyui,写个树

    前言:网上关于编写组织机构树的教程并不少,我第一次写树的时候也是在网上借鉴别人的技术,走了一些弯路写下了树.是因为这些教程都不是很全面,对于编程新手来说跳跃性太强.所以趁着闲暇时期,我用心的写个树,供 ...

  10. java数组反射实现动态的判断一个对象是否是数组并且对数组进行拆包输出

    public static Map<String, String> maptoMapString(Map<String, ?> map) { return map.entryS ...