Recently writing background management system , To achieve background data export excel This function , Backstage use is spring boot, The front desk is vue+iview

maven rely on :
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId>
<version>3.15</version> <scope>compile</scope> </dependency>
Tools :
public class FileUtil { public static void createFile(HttpServletResponse
response, HSSFWorkbook workbook) { // Set file name String fileName =" Data information "; try { //
Capture data from memory buffers , Convert to byte array ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out); // Get data in memory buffer byte[] content = out.toByteArray(); //
Convert byte array to input stream InputStream in = new ByteArrayInputStream(content);
// By calling reset() Method can be relocated . response.reset(); //
If the file name is in English, it does not need to be encoded , Need to add Chinese name "iso-8859-1" Prevent random code
response.setHeader("Content-Disposition", "attachment; filename=" + new
String((fileName + ".xls").getBytes(), "iso-8859-1"));
response.addHeader("Content-Length", "" + content.length);
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
ServletOutputStream outputStream = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(in); BufferedOutputStream bos
= new BufferedOutputStream(outputStream); byte[] buff = new byte[8192]; int
bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead); } bis.close(); bos.close();
outputStream.flush(); outputStream.close(); } catch (IOException e) {
e.printStackTrace(); } } }
service Layer interface :
RestResult getExcel(HttpServletRequest request, HttpServletResponse response,
String keyword, Integer kind, Integer type, Integer status);
Implementation class :
public RestResult getExcel(HttpServletRequest request, HttpServletResponse
response, String keyword, Integer kind, Integer type, Integer status) {
List<WithdrawVo> withdrawVos = withDrawRecordMapper.getExcel(keyword, kind,
type, status); // Create Workbook HSSFWorkbook workbook = new HSSFWorkbook(); // Create table
HSSFSheet sheet = workbook.createSheet(" Withdrawal information "); // Create row HSSFRow row =
sheet.createRow(0); // Create cell styles HSSFCellStyle cellStyle =
workbook.createCellStyle(); // Header String[] head = {" account number ", " Nickname? ", " Type of withdrawal ", " Type of withdrawal ",
" Application status ", " Bank card No ", " Withdrawal amount ", " Gender ", " Self rebate balance ", " Promotion and distribution balance ", " Creation time "}; HSSFCell cell; //
Set header for (int iHead = 0; iHead < head.length; iHead++) { cell =
row.createCell(iHead); cell.setCellValue(head[iHead]);
cell.setCellStyle(cellStyle); } // Set table contents for (int iBody = 0; iBody <
withdrawVos.size(); iBody++) { row = sheet.createRow(iBody + 1); WithdrawVo u =
withdrawVos.get(iBody); String[] userArray = new String[11]; userArray[0] =
u.getAccount(); userArray[1] = u.getNickname(); String sex=(u.getSex() ==0
?" secrecy ":(u.getSex()==1?" male " : " female ")); String kinds=u.getKind()==1? " Self reward ":" Promotion Fund ";
userArray[2] = kinds; String cardType=u.getType()==1? " WeChat ":" bank card "; userArray[3]
= cardType; String statuss=(u.getStatus() ==0 ?" To be reviewed ":(u.getStatus()==1?" Approved " :
" Audit failed ")); userArray[4] = statuss; userArray[5] = u.getBankCard() + "";
userArray[6] = u.getMoney() + ""; userArray[7] = sex; userArray[8] =
u.getRebateBalance() + ""; userArray[9] = u.getSpreadBalance() + ""; Date
d1=u.getCreateTime(); String s1=String.format("%tY-%tm-%td
%tH:%tM:%tS",d1,d1,d1,d1,d1,d1); userArray[10] = s1 ; for (int iArray = 0;
iArray < userArray.length; iArray++) {
row.createCell(iArray).setCellValue(userArray[iArray]); } } // generate Excel file
FileUtil.createFile(response, workbook); return null; }
controller layer :
@RequestMapping(value="/excel",method = RequestMethod.GET) public RestResult
exportExcel(HttpServletRequest request, HttpServletResponse response ){ String
keyword = request.getParameter("keyword"); Integer
kind=Integer.valueOf(request.getParameter("kind")); Integer
type=Integer.valueOf(request.getParameter("type")); Integer
status=Integer.valueOf(request.getParameter("status")); return
withdrawService.getExcel(request,response,keyword,kind,type,status); }
give the result as follows :

Technology
©2019-2020 Toolsou All rights reserved,
C Language programming to find a student's grade java Realize the function of grabbing red packets Random forest R Language implementation QCustomPlot series (5)- Real time dynamic curve Software testing BUG describe use C++ I want to talk to you “ Prototype mode ” ( copy / copy constructor )JS How to operate China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour Zimi apologizes :33W Gan charger delayed due to force majeure 2021 year 2 Chinese programming language ranking