按步骤走
模板存在地址(很重要)你可以放在你自己的任何位置
1、在pom.xml中添加以下maven包
<!--excel工具
-->
<dependency
>
<groupId
>org
.apache
.poi
</groupId
>
<artifactId
>poi
</artifactId
>
<version
>4.0.1</version
>
</dependency
>
<dependency
>
<groupId
>org
.apache
.poi
</groupId
>
<artifactId
>poi
-ooxml
</artifactId
>
<version
>4.0.1</version
>
</dependency
>
2、创建ExportUtils工具类
package com
.soa
.ump
.server
.message
;
import org
.apache
.poi
.xssf
.usermodel
.XSSFCell
;
import org
.apache
.poi
.xssf
.usermodel
.XSSFRow
;
import org
.apache
.poi
.xssf
.usermodel
.XSSFSheet
;
import org
.apache
.poi
.xssf
.usermodel
.XSSFWorkbook
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Component
;
import javax
.servlet
.http
.HttpServletResponse
;
import java
.io
.*;
import java
.math
.BigDecimal
;
import java
.net
.URLEncoder
;
import java
.util
.Arrays
;
import java
.util
.List
;
import java
.util
.Map
;
@Component
public class ExportUtils {
@Autowired
public ExportConfig exportConfig
;
public void exportExcel(HttpServletResponse response
, String title
, String templateName
, String newFileName
, List
<Map
<String
, Object
>> mapList
, Integer
[] digitPosition
, Integer h
) {
FileInputStream
in = null;
try {
in = new FileInputStream(new File(this.exportConfig
.getCatalogue() + templateName
));
XSSFWorkbook workbook
= new XSSFWorkbook(in);
CellStyle redStyle
= workbook
.createCellStyle();
HSSFFont redFont
= workbook
.createFont();
redFont
.setColor(Font
.COLOR_RED);
redFont
.setFontHeightInPoints((short
) 10);
redStyle
.setFont(redFont
);
XSSFSheet sheet
= workbook
.getSheetAt(0);
if (sheet
!= null) {
XSSFRow row
= sheet
.getRow(0);
if (row
== null) {
row
= sheet
.createRow(0);
}
XSSFCell cell
= row
.getCell(0);
if (cell
== null) {
cell
= row
.createCell(0);
}
cell
.setCellValue(title
);
List
<Integer
> integers
= Arrays
.asList(digitPosition
);
for (int i
= 0; i
< mapList
.size(); i
++) {
Map
<String
, Object
> map
= mapList
.get(i
);
Object
[] values
= map
.values().toArray();
row
= sheet
.createRow(i
+ h
);
for (int k
= 0; k
< values
.length
; k
++) {
double height
= 400 ;
row
.setHeight((short
)height
);
cell
= row
.getCell(k
) == null ? row
.createCell(k
) : row
.getCell(k
);
cell
.setCellStyle(redStyle
);
if (integers
.contains(k
)) {
BigDecimal money
= values
[k
] == null ? BigDecimal
.ZERO : ((BigDecimal
) values
[k
]);
cell
.setCellValue(money
.doubleValue());
} else {
cell
.setCellValue(values
[k
] == null ? "" : values
[k
].toString());
}
}
}
}
String pageName
= newFileName
+ System
.currentTimeMillis() + ".xlsx";
setResponseHeader(response
, pageName
);
workbook
.write(response
.getOutputStream());
response
.getOutputStream().flush();
response
.getOutputStream().close();
} catch (Exception e
) {
e
.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e
) {
e
.printStackTrace();
}
}
}
protected void setResponseHeader(HttpServletResponse response
, String fileName
) throws Exception
{
response
.setContentType("application/vnd.ms-excel");
response
.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder
.encode(fileName
, "UTF-8") + "\"");
response
.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response
.setHeader("Pragma", "public");
response
.setDateHeader("Expires", 0);
}
private void createRowAndCell(Object obj
, XSSFRow row
, XSSFCell cell
, int index
) {
cell
= row
.getCell(index
) == null ? row
.createCell(index
) : row
.getCell(index
);
cell
.setCellValue(obj
== null ? "" : obj
.toString());
}
}
3、创建ExportConfig(和第二步是同目录)
package com
.soa
.ump
.server
.message
;
import org
.springframework
.boot
.context
.properties
.ConfigurationProperties
;
import org
.springframework
.stereotype
.Component
;
@Component
@
ConfigurationProperties(prefix
= "export")
public class ExportConfig {
private String catalogue
;
public String
getCatalogue() {
return catalogue
;
}
public void setCatalogue(String catalogue
) {
this.catalogue
= catalogue
;
}
}
4、html页面 写按钮
<permission value
="sys:client:export">
<button type
="button" lay
-submit
="" class="layui-btn layui-btn-warm" id
="exportExcel" name
="exportExcel">
<i
class="layui-icon">
</i
>导出Excel
</button
>
</permission
>
5、js
$("#exportExcel").bind("click", function () {
excel("searchId", "back/soaexport/userInfoExcel");
});
function excel(searchId
, urls
) {
urls
= requestLink
+ urls
;
if (searchId
!= "") {
$("#" + searchId
).find("input,select").each(function () {
var id
= this.id
;
var value
= this.value
;
if (id
.length
> 0) {
if (value
!= "" && value
!= null) {
if (urls
.indexOf("?") > -1) {
urls
+= "&" + id
+ "=" + value
;
} else {
urls
+= "?" + id
+ "=" + value
;
}
}
}
});
}
window
.location
.href
= urls
;
}
6、控制器调用
package com
.soa
.ump
.server
.controllerBack
;
import com
.soa
.bus
.rpc
.api
.*;
import com
.soa
.common
.web
.BaseController
;
import com
.soa
.ump
.server
.message
.ExportUtils
;
import io
.swagger
.annotations
.Api
;
import io
.swagger
.annotations
.ApiImplicitParam
;
import io
.swagger
.annotations
.ApiImplicitParams
;
import io
.swagger
.annotations
.ApiOperation
;
import org
.apache
.shiro
.authz
.annotation
.RequiresPermissions
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.web
.bind
.annotation
.GetMapping
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import org
.springframework
.web
.bind
.annotation
.RestController
;
import javax
.servlet
.http
.HttpServletRequest
;
import javax
.servlet
.http
.HttpServletResponse
;
import java
.util
.*;
@RestController
@
RequestMapping("/back/soaexport")
@
Api(value
= "ExportController", description
= "导出管理API")
public class ExportController extends BaseController {
private final String
USER_INFO_TEMPLATE = "user_info_template.xlsx";
@Autowired
private ExportUtils exportUtils
;
@Autowired
private IHjClientService hjClientService
;
@
ApiOperation(value
= "客户信息导出", notes
= "客户信息导出")
@
GetMapping("/userInfoExcel")
@
RequiresPermissions("sys:client:export")
public String
userInfoExcel(HttpServletRequest request
, HttpServletResponse response
) {
String templateName
= this.USER_INFO_TEMPLATE;
String title
= "客户信息";
String newFileName
= "客户信息";
Integer
[] digitPosition
= new Integer[]{};
Map cmap
= new HashMap();
List
<Map
<String
, Object
>> mapList
= this.hjClientService
.findExcelList(cmap
);
this.exportUtils
.exportExcel(response
, title
, templateName
, newFileName
, mapList
, digitPosition
, 2);
return "succeed";
}
}
7、业务层处理数据
@Override
public List
<Map
<String
, Object
>> findExcelList(Map cmap
) {
cmap
.put("isDel", "N");
List
<HjClient
> rb
= this.hjClientMapper
.find(cmap
);
List
<Map
<String
, Object
>> relist
= new ArrayList<>();
for (HjClient hjClient
: rb
) {
Map map
= new LinkedHashMap();
map
.put("name", hjClient
.getName());
map
.put("unifyCode", hjClient
.getUnifyCode());
map
.put("registeredCapital", hjClient
.getRegisteredCapital());
map
.put("firmType", DicParamUtil
.getDicCodeByType("FIRM_TYPE", hjClient
.getFirmType()));
map
.put("address", hjClient
.getAddress());
map
.put("representative", hjClient
.getRepresentative());
map
.put("startTime", hjClient
.getStartTime());
map
.put("stupTime", hjClient
.getStupTime());
map
.put("scope", hjClient
.getScope());
map
.put("rating", hjClient
.getRating());
map
.put("agent", hjClient
.getAgent());
map
.put("remark", hjClient
.getRemark());
relist
.add(map
);
}
return relist
;
}
8、成品