Home > Back-end >  About springboot project front-end exported from the database query to the data to Excel
About springboot project front-end exported from the database query to the data to Excel

Time:01-27

Question is like this, I want to download when users click on export will be the results of the query to export,

And this is my js
 
Layer. Confirm (' determine export to Excel? '{
BTN: [' sure ', 'cancel']
}, function () {
$. Ajax ({
Type: "POST",
Url:/require/export2Excel,
ResponseType: Blob,
Data: {
RequireName: $(' # requireName). Val (),
RequireId: $(' # requireId). Val (),
FunctionName: $(' # functionName '). Val (),
FunctionModule: $(' # functionModule). Val (),
FunctionType: $(' # functionType). Val (),
AssignState: $(' # assignState). Val (),
SubmitState: $(' # submitState). Val (),
}, success: (wb)=& gt; {//wb for me after the workbook, I written respong excel file when no data directly, as this data, may have a problem here
Var blob=new blob ((wb));//create a blob object
Var a=document. The createElement method (' a ');//create a & lt; A> The tag
A.h ref=https://bbs.csdn.net/topics/URL.createObjectURL (blob);//the response is a blob
A. d. ownload="functional requirements tables. XLSX";
A.s tyle. Display='none';
Document. The body. The appendChild (a);
A.c lick ();
A.r emove ();
Layer. MSG (" export success!" );
}
})
});


Back-end code is
The controller
 
@ PostMapping (" export2Excel ")
@ ResponseBody
Public void export2Excel (@ RequestParam Map Params, HttpServletResponse response) {
Query Query=new Query (params, "");
List List=requireService. List (query);
ExcelUtils. WriteExcel (response, list, FunctionRequireDO. Class);
}


Utility class ExcelUtils
 
Public static & lt; T> Void writeExcel (HttpServletResponse response, List DataList Class CLS) {
The Field [] fields=CLS. GetDeclaredFields ();
List FieldList=Arrays. Stream (fields)
The filter (field - & gt; {
ExcelColumn annotation=field. GetAnnotation (ExcelColumn. Class);
If (the annotation!=null & amp; & The annotation. Col () & gt; 0 {
Field. SetAccessible (true);
return true;
}
return false;
}). Sorted (Comparator.com paring (field - & gt; {
Int col=0;
ExcelColumn annotation=field. GetAnnotation (ExcelColumn. Class);
If (the annotation!=null) {
Col=annotation. Col ();
}
Return col.
})). Collect (Collectors. ToList ());

Workbook wb=new XSSFWorkbook();
Sheet Sheet=wb. CreateSheet (" Sheet1 ");
AtomicInteger ai=new AtomicInteger ();
{
The Row Row=sheet. CreateRow (ai) getAndIncrement ());
AtomicInteger aj=new AtomicInteger ();
//write head
FieldList. ForEach (field - & gt; {
ExcelColumn annotation=field. GetAnnotation (ExcelColumn. Class);
String columnName="";
If (the annotation!=null) {
ColumnName=annotation. The value ();
}
The Cell Cell=row. CreateCell (aj. GetAndIncrement ());

CellStyle CellStyle=wb. CreateCellStyle ();
CellStyle. SetFillForegroundColor (IndexedColors. WHITE. GetIndex ());
CellStyle. SetFillPattern (cellStyle. SOLID_FOREGROUND);
CellStyle. SetAlignment (cellStyle. ALIGN_CENTER);

The Font Font=wb. CreateFont ();
The font. SetBoldweight (font. BOLDWEIGHT_NORMAL);
CellStyle. SetFont (font);
cell.setCellStyle(cellStyle);
Cell. SetCellValue (columnName);
});
}
If (CollectionUtils isNotEmpty (dataList)) {
DataList. ForEach (t - & gt; {
The Row row1=sheet. CreateRow (ai) getAndIncrement ());
AtomicInteger aj=new AtomicInteger ();
FieldList. ForEach (field - & gt; {
Class<?> Type=field. GetType ();
The Object valuehttps://bbs.csdn.net/topics/="";
Try {
Value=https://bbs.csdn.net/topics/field.get (t);
} the catch (Exception e) {
e.printStackTrace();
}
The Cell Cell=row1. CreateCell (aj. GetAndIncrement ());
If (the value!=null) {
If (type==Date. Class) {
Cell. SetCellValue (value. The toString ());
} else {
Cell. SetCellValue (value. The toString ());
}
Cell. SetCellValue (value. The toString ());
}
});
});
}
//freeze panes
Wb. GetSheet (" Sheet1 "). CreateFreezePane (0, 1, 0, 1);
//the browser downloads the excel
BuildExcelDocument (" functional requirements tables. XLSX, wb, response);
//generates excel file
//buildExcelFile (" C:/Users/Administrator/Desktop/" + fileName + ". XLSX ", wb);
}

/* *
* the browser downloads the excel
* @ param fileName
* @ param wb
* @ param response
*/

Private static void buildExcelDocument (String fileName, Workbook wb, HttpServletResponse response) {
Try {
//application/octet stream -
The response. SetContentType (MediaType. APPLICATION_OCTET_STREAM_VALUE);
The response. SetHeader (" the content-type ", "application/VND. - ms excel; charset=UTF-8");
The response. SetHeader (" the Content - the Disposition ", "legislation; Filename="+ URLEncoder. Encode (filename," utf-8 "));

The response. FlushBuffer ();
Wb. Write (response. GetOutputStream ());
} the catch (IOException e) {
e.printStackTrace();
}
}

I am now writing can be exported to an excel file, but open show


nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related