|
@@ -34,6 +34,10 @@ import jakarta.annotation.Resource;
|
|
|
import jakarta.validation.Valid;
|
|
|
import jakarta.validation.constraints.NotEmpty;
|
|
|
import java.util.List;
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.HashMap;
|
|
|
|
|
|
/**
|
|
|
* ${functionName}控制器
|
|
@@ -93,6 +97,51 @@ public class ${className}Controller {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 导出${functionName}
|
|
|
+ *
|
|
|
+ * @author ${authorName}
|
|
|
+ * @date ${genTime}
|
|
|
+ */
|
|
|
+ @Operation(summary = "导出${functionName}")
|
|
|
+ @CommonLog("导出${functionName}")
|
|
|
+ @SaCheckPermission("/${moduleName}/${busName}/export")
|
|
|
+ @PostMapping("/${moduleName}/${busName}/export")
|
|
|
+ public void export(@RequestBody @Valid ${className}PageParam ${classNameFirstLower}PageParam,, HttpServletResponse response) {
|
|
|
+ Page<${className}> page = ${classNameFirstLower}Service.page(${classNameFirstLower}PageParam);
|
|
|
+ List<${className}> records = page.getRecords();
|
|
|
+ exportExcel(response,${className}.class,records,${functionName});
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出表格数据
|
|
|
+ * @param cl 表格字段实体类
|
|
|
+ * @param data 查询数据
|
|
|
+ * @param sheetName 表格名称
|
|
|
+ */
|
|
|
+ public void exportExcel(HttpServletResponse response, Class cl, List data, String sheetName){
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
+ try {
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ String fileName = URLEncoder.encode(sheetName, "UTF-8").replaceAll("\\+", "%20");
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
|
|
+ EasyExcel.write(response.getOutputStream(), cl).autoCloseStream(Boolean.FALSE).sheet(sheetName).doWrite(data);
|
|
|
+ } catch (IOException e) {
|
|
|
+ response.reset();
|
|
|
+ response.setContentType("application/json");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ map.put("status", "failure");
|
|
|
+ map.put("message", "下载文件失败" + e.getMessage());
|
|
|
+ try {
|
|
|
+ response.getWriter().println(JSON.toJSONString(map));
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 删除${functionName}
|
|
|
*
|
|
|
* @author ${authorName}
|