前端代码
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> </ title>
< script src = " ./js/jquery-3.6.0.min.js" > </ script>
</ head>
< body>
< div>
< button onclick = " exportFile ( ) " > 导出</ button>
</ div>
< script>
function exportFile ( ) {
var params = { } ;
var url = "http://localhost:9000/exportFile/export"
getExport ( url, function ( res ) {
downloadForBolb ( res, "学生表.xls" , 'application/vnd.ms-excel; charset=utf-8' )
} )
}
function getExport ( url, fn, errorCallback ) {
$. ajax ( {
type : "GET" ,
url : url,
xhrFields : {
responseType : "arraybuffer" ,
} ,
error : function ( XHR , textStatus, errorThrown) {
console. log ( "XHR=" + XHR + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown) ;
if ( errorCallback && typeof errorCallback == "function" ) {
alert ( "error" , "系统异常,请联系管理员!" ) ;
errorCallback ( { } ) ;
}
} ,
success : function ( data, textStatus ) {
if ( fn && typeof fn == "function" ) {
fn ( data) ;
}
} ,
headers : {
"Authorization" : window. sessionStorage. getItem ( "token" )
}
} ) ;
} ;
function downloadForBolb ( data, fileName, mineType ) {
var blob = new Blob ( [ data] , { type : mineType} ) ;
window. URL = window. URL || window. webkitURL;
var href = URL . createObjectURL ( blob) ;
var downA = document. createElement ( "a" ) ;
downA. href = href;
downA. download = fileName;
downA. click ( ) ;
window. URL . revokeObjectURL ( href) ;
} ;
</ script>
</ body>
</ html>
后台代码
< dependency>
< groupId> com. alibaba< / groupId>
< artifactId> easyexcel< / artifactId>
< version> 3.3 .1 < / version>
< / dependency>
import com. alibaba. excel. EasyExcel ;
import com. alibaba. excel. write. style. column. LongestMatchColumnWidthStyleStrategy ;
import org. apache. poi. hssf. usermodel. * ;
import org. springframework. web. multipart. MultipartFile ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ;
import java. net. URLEncoder ;
import java. util. List ;
public class ExcelUtil {
public static < T > void write ( HttpServletResponse response, String filename, String sheetName,
Class < T > head, List < T > data) throws IOException {
EasyExcel . write ( response. getOutputStream ( ) , head)
. autoCloseStream ( false )
. registerWriteHandler ( new LongestMatchColumnWidthStyleStrategy ( ) )
. sheet ( sheetName) . doWrite ( data) ;
response. addHeader ( "Content-Disposition" , "attachment;filename=" + URLEncoder . encode ( filename, "UTF-8" ) ) ;
response. setContentType ( "application/vnd.ms-excel;charset=UTF-8" ) ;
}
public static < T > List < T > read ( MultipartFile file, Class < T > head) throws IOException {
return EasyExcel . read ( file. getInputStream ( ) , head, null )
. autoCloseStream ( false )
. doReadAllSync ( ) ;
}
}
import com. alibaba. excel. annotation. ExcelProperty ;
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
@ExcelProperty ( value = "姓名" , index = 0 )
private String name;
@ExcelProperty ( value = "年龄" , index = 1 )
private int age;
@ExcelProperty ( value = "地址" , index = 2 )
private String address;
}
import com. boot. export. entity. Student ;
import com. boot. util. ExcelUtil ;
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. ArrayList ;
import java. util. List ;
import java. util. Map ;
@RestController
@RequestMapping ( "/exportFile" )
public class exportFile {
@RequestMapping ( "export" )
public void export ( Map < String , Object > params, HttpServletRequest request, HttpServletResponse response) {
try {
List < Student > list = new ArrayList < > ( ) ;
list. add ( new Student ( "刘大海" , 54 , "桃李村1号" ) ) ;
list. add ( new Student ( "王大富" , 34 , "桃李村4号" ) ) ;
list. add ( new Student ( "王婆婆" , 82 , "桃李村6号" ) ) ;
list. add ( new Student ( "李复" , 24 , "桃李村9号" ) ) ;
list. add ( new Student ( "陈月" , 14 , "桃李村3号" ) ) ;
list. add ( new Student ( "老村长" , 74 , "桃李村3号" ) ) ;
ExcelUtil . write ( response, "学生信息.xls" , "数据" , Student . class , list) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
}
}
}
Blob文件类型