package com. meritdata. cloud. cosp. profilemanagement. util;
import java. io. *;
import java. util. zip. ZipEntry;
import java. util. zip. ZipOutputStream;
import lombok. SneakyThrows;
import lombok. extern. slf4j. Slf4j;
import org. springframework. util. StringUtils;
@Slf4j
public class CompressedFileUtil {
public static void compressedFile ( String resourcesPath, String targetPath) {
try {
compressedFile ( resourcesPath, targetPath, "" ) ;
} catch ( IOException e) {
log. error ( "IO流异常" , e) ;
}
}
public static void compressedFile ( String resourcesPath, String targetPath, String fileName) throws IOException {
File resourcesFile = new File ( resourcesPath) ;
File targetFile = new File ( targetPath) ;
if ( ! targetFile. exists ( ) ) {
targetFile. mkdirs ( ) ;
}
String targetName = ( StringUtils. isEmpty ( fileName) ? resourcesFile. getName ( ) : fileName) + ( fileName. indexOf ( ".zip" ) == - 1 ? ".zip" : "" ) ;
ZipOutputStream out = null;
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream ( new File ( targetPath, targetName) ) ;
out = new ZipOutputStream ( new BufferedOutputStream ( outputStream) ) ;
createCompressedFile ( out, resourcesFile, "" ) ;
} catch ( FileNotFoundException e) {
log. error ( "文件找不到" , e) ;
} finally {
if ( out != null ) {
out. close ( ) ;
}
if ( outputStream!= null) {
outputStream. close ( ) ;
}
}
}
@SneakyThrows
public static void createCompressedFile ( ZipOutputStream out, File file, String dir) {
if ( file. isDirectory ( ) ) {
File[ ] files = file. listFiles ( ) ;
try {
out. putNextEntry ( new ZipEntry ( dir + "/" ) ) ;
} catch ( IOException e) {
log. error ( "io流异常" , e) ;
}
dir = dir. length ( ) == 0 ? "" : dir + "/" ;
for ( int i = 0 ; i < files. length; i++ ) {
createCompressedFile ( out, files[ i] , dir + files[ i] . getName ( ) ) ;
}
} else {
FileInputStream fis = null ;
try {
fis = new FileInputStream ( file) ;
out. putNextEntry ( new ZipEntry ( dir) ) ;
int j = 0 ;
byte [ ] buffer = new byte [ 1024 ] ;
while ( ( j = fis. read ( buffer) ) > 0 ) {
out. write ( buffer, 0 , j) ;
}
} catch ( IOException e) {
log. error ( "io流异常" , e) ;
} finally {
if ( fis != null) {
fis. close ( ) ;
}
}
}
}
}
@ApiOperation ( "导出数据" )
@RequestMapping ( value = "customerData" , method = RequestMethod. GET)
public ResultBody export ( ) {
List< Map< String, Object> > data = new ArrayList < Map< String, Object> > ( ) ;
LinkedHashMap< String, Object> keyMap = new LinkedHashMap < > ( ) ;
keyMap. put ( "id" , "编号" ) ;
keyMap. put ( "age" , "年龄" ) ;
keyMap. put ( "asset" , "资产" ) ;
keyMap. put ( "commission" , "佣金" ) ;
keyMap. put ( "createTime" , "创建时间" ) ;
keyMap. put ( "custId" , "用户编号" ) ;
keyMap. put ( "custName" , "姓名" ) ;
keyMap. put ( "sales" , "出版社" ) ;
keyMap. put ( "sex" , "性别" ) ;
keyMap. put ( "states" , "状态" ) ;
keyMap. put ( "tel" , "电话" ) ;
List< CustGroupRel> all = custGroupRelRepository. findAll ( ) ;
for ( CustGroupRel a : all) {
Map< String, Object> map = new LinkedHashMap < String, Object> ( ) ;
map. put ( "id" , a. getId ( ) ) ;
map. put ( "age" , a. getAge ( ) ) ;
map. put ( "asset" , a. getAsset ( ) ) ;
map. put ( "commission" , a. getCommission ( ) ) ;
map. put ( "createTime" , a. getCreateTime ( ) ) ;
map. put ( "custId" , a. getCustId ( ) ) ;
map. put ( "custName" , a. getCustName ( ) ) ;
map. put ( "sales" , a. getSales ( ) ) ;
map. put ( "sex" , a. getSex ( ) ) ;
map. put ( "states" , a. getStates ( ) ) ;
map. put ( "tel" , a. getTel ( ) ) ;
data. add ( map) ;
}
int maxFileSize = 500000 ;
int maxSheetSize = 10000 ;
String title = "客户表信息" ;
long start = System. currentTimeMillis ( ) ;
ExportExcelUtils. exportExcel ( data, keyMap, maxFileSize, maxSheetSize, title, "客户表信息" , path) ;
long end = System. currentTimeMillis ( ) ;
System. out. println ( "耗时:" + ( end - start) / 1000 + "秒" ) ;
Date date = new Date ( ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
String datestr = sdf. format ( date) ;
String fileName = datestr + "用户表" ;
try {
CompressedFileUtil. compressedFile ( path, zip, fileName) ;
System. out. println ( "压缩文件已经生成..." ) ;
} catch ( Exception e) {
System. out. println ( "压缩文件生成失败..." ) ;
log. error ( "压缩失败" , e) ;
}
return ResultBody. success ( "导出成功" + path + title + " 共耗时" + ( end - start) / 1000 + "秒" ) ;
}
package com. meritdata. cloud. cosp. profilemanagement. entity;
import io. swagger. annotations. ApiModel;
import io. swagger. annotations. ApiModelProperty;
import lombok. Data;
import javax. persistence. Column;
import javax. persistence. Entity;
import javax. persistence. Id;
import javax. persistence. Table;
import java. io. Serializable;
import java. util. Date;
@Data
@Table ( name = "cosp_index_column_config" )
@Entity
@ApiModel ( value= "配置" , description= "配置" )
public class CospIndex implements Serializable {
@Id
@Column ( name = "id" )
@ApiModelProperty ( value = "id" , name = "id" )
private String id;
@ApiModelProperty ( value = "columnCode" , name = "编码" )
@Column ( name = "column_code" )
private String columnCode;
@ApiModelProperty ( value = "columnName" , name = "名字" )
@Column ( name = "column_name" )
private String columnName;
@ApiModelProperty ( value = "userId" , name = "用户id" )
@Column ( name = "user_id" )
private String userId;
@ApiModelProperty ( value = "sortno" , name = "排序号" )
@Column ( name = "sortno" )
private Integer sortno;
@ApiModelProperty ( value = "createTime" , name = "创建时间" )
@Column ( name = "create_date" )
private Date createTime;
}
```java
package com. meritdata. cloud. cosp. profilemanagement. util;
import lombok. extern. slf4j. Slf4j;
import org. apache. poi. hssf. usermodel. *;
import org. apache. poi. hssf. util. HSSFColor;
import org. apache. poi. ss. usermodel. BorderStyle;
import org. apache. poi. ss. usermodel. HorizontalAlignment;
import org. apache. poi. ss. usermodel. IndexedColors;
import java. io. *;
import java. util. *;
@Slf4j
public class ExportExcelUtils {
public static void exportExcel ( HSSFWorkbook workbook, int sheetNum,
String sheetTitle, LinkedHashMap< String, Object> keyMap, List< Map< String, Object> > result,
OutputStream out) throws Exception {
HSSFSheet sheet = workbook. createSheet ( ) ;
workbook. setSheetName ( sheetNum, sheetTitle) ;
sheet. setDefaultColumnWidth ( ( short ) 20 ) ;
HSSFCellStyle style = workbook. createCellStyle ( ) ;
style. setFillForegroundColor ( HSSFColor. HSSFColorPredefined. BLACK. getIndex ( ) ) ;
style. setFillForegroundColor ( IndexedColors. WHITE. index) ;
style. setBorderBottom ( BorderStyle. THIN) ;
style. setBorderLeft ( BorderStyle. THIN) ;
style. setBorderRight ( BorderStyle. THIN) ;
style. setBorderTop ( BorderStyle. THIN) ;
style. setAlignment ( HorizontalAlignment. CENTER) ;
HSSFFont font = workbook. createFont ( ) ;
font. setFontHeightInPoints ( ( short ) 12 ) ;
style. setFont ( font) ;
style. setWrapText ( true ) ;
HSSFRow row = sheet. createRow ( 0 ) ;
int cellNum = 0 ;
for ( Map. Entry< String, Object> map : keyMap. entrySet ( ) ) {
HSSFCell cell = row. createCell ( cellNum) ;
cell. setCellStyle ( style) ;
HSSFRichTextString text = new HSSFRichTextString ( map. getValue ( ) . toString ( ) ) ;
cell. setCellValue ( text. toString ( ) ) ;
cellNum++ ;
}
if ( result != null) {
int index = 1 ;
for ( Map< String, Object> maps : result) {
row = sheet. createRow ( index) ;
int cellIndex = 0 ;
for ( Map. Entry< String, Object> map : keyMap. entrySet ( ) ) {
HSSFCell cell = row. createCell ( cellIndex) ;
Object obj = maps. get ( map. getKey ( ) ) ;
if ( obj != null) {
cell. setCellValue ( obj. toString ( ) ) ;
} else {
cell. setCellValue ( "" ) ;
}
cellIndex++ ;
}
index++ ;
}
}
}
public static boolean exportExcel ( List< Map< String, Object> > data, LinkedHashMap< String, Object> keyMap, int maxFileSize, int maxSheetSize, String sheetName, String fileName, String path) {
boolean result = false ;
OutputStream out = null;
HSSFWorkbook workbook = null;
if ( data != null && data. size ( ) > 0 ) {
int zongSize = data. size ( ) ;
int zongFileCount = 0 ;
if ( zongSize % maxFileSize == 0 ) {
zongFileCount = zongSize / maxFileSize;
} else {
zongFileCount = zongSize / maxFileSize + 1 ;
}
try {
for ( int i = 0 ; i < zongFileCount; i++ ) {
int fileLimit = 0 ;
if ( ( i * maxFileSize) + maxFileSize > zongSize) {
fileLimit = zongSize;
} else {
fileLimit = ( i * maxFileSize) + maxFileSize;
}
int fileStart = i * maxFileSize;
File file = new File ( path) ;
if ( ! file. exists ( ) ) {
file. mkdirs ( ) ;
}
out = new FileOutputStream ( path + File. separator + fileName + "(" + ( i + 1 ) + ")" + ".xls" ) ;
List< Map< String, Object> > fileData = new ArrayList < Map< String, Object> > ( ) ;
for ( int j = fileStart; j < fileLimit; j++ ) {
fileData. add ( data. get ( j) ) ;
}
int fileDataSize = fileData. size ( ) ;
int sheetCount = 0 ;
if ( fileDataSize % maxSheetSize == 0 ) {
sheetCount = fileDataSize / maxSheetSize;
} else {
sheetCount = fileDataSize / maxSheetSize + 1 ;
}
workbook = new HSSFWorkbook ( ) ;
for ( int j = 0 ; j < sheetCount; j++ ) {
int sheetStart = j * maxSheetSize;
int sheetLimt = 0 ;
if ( j * maxSheetSize + maxSheetSize > fileDataSize) {
sheetLimt = fileDataSize;
} else {
sheetLimt = j * maxSheetSize + maxSheetSize;
}
List< Map< String, Object> > sheetData = new ArrayList < Map< String, Object> > ( ) ;
for ( int k = sheetStart; k < sheetLimt; k++ ) {
sheetData. add ( fileData. get ( k) ) ;
}
exportExcel ( workbook, j, sheetName + ( j + 1 ) , keyMap, sheetData, out) ;
}
workbook. write ( out) ;
result = true ;
}
} catch ( FileNotFoundException e) {
result = false ;
log. info ( "文件找不到" , e) ;
} catch ( IOException e) {
result = false ;
log. info ( "io异常" , e) ;
} catch ( Exception e) {
result = false ;
log. info ( "错误" , e) ;
} finally {
try {
if ( out != null) {
out. close ( ) ;
}
if ( workbook != null) {
workbook. close ( ) ;
}
} catch ( IOException e) {
result = false ;
log. info ( "io异常" , e) ;
}
}
}
return result;
}
}