下载列表,在没有过滤之前下载列表所有数据,点击过滤之后,下载过滤之后对数据,生成csv文件。
二、思路:先根据条件(是否过滤了数据)筛选出数据,将数据导入csv文件,生成文件并返回。
三、代码实现:1、controller层
@ApiOperation(value = "downloadCSV", notes = "download solution")
@CactusAction(url = "api/downloadCSV", method = HttpMethod.POST)
@PostMapping("/downloadCSV")
public void downloadAndJudgeFilter(HttpServletResponse response, HttpServletRequest request
,@ApiIgnore CactusContext context, @Validated @RequestBody OrderSolutionDownloadDTO downloadDTO) throws IOException {
//-------传入参数根据自己的传 CactusContext为获取到用户信息-------OrderSolutionDownloadDTO为查询的参数dto--------
//-------这一块------ 根据自己的条件筛选出需要导出的数据 ---------------
JSONObject criteria = null;
if(downloadDTO.getFilterId() > 0){
Filters filter = filtersService.getOne(Wrappers.lambdaQuery().eq(Filters::getAccountId, context.getAccountId())
.eq(Filters::getCreatorId, context.getUserId()).eq(Filters::getId, downloadDTO.getFilterId()));
if (Objects.nonNull(filter)){
criteria = JSONObject.parseObject(filter.getCriteria());
}
}else{
criteria = JSONObject.parseObject(downloadDTO.getF());
}
List solutionDocuments = returnAndRefundListService.downloadAndJudgeFilter(context, criteria);
// --------------数据为List集合solutionDocuments------------------------------
// ----------将数据传入ExportCsvUtils中
ExportCsvUtils.exportCsv(response,request,solutionDocuments);
}
2、ExportCsvUtils
import com.shulex.cloud.platform.ticket.consts.SolutionListConstants;
import com.shulex.cloud.platform.ticket.es.document.SolutionDocument;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
public class ExportCsvUtils {
private static final String CSV_COLUMN_SEPARATOR = ",";
private static final String CSV_RN = "rn";
public static void exportCsv(HttpServletResponse response, HttpServletRequest request
, List list){
// 设置表格头
Object[] head = {SolutionListConstants.TICKET_NUMBER,SolutionListConstants.TICKET_CREATE_DATE,
SolutionListConstants.CUSTOMER_EMAIL,SolutionListConstants.CUSTOMER_NAME,
SolutionListConstants.PLATFORM,SolutionListConstants.MARKET,SolutionListConstants.SELLER,
SolutionListConstants.ORDER_PURCHASE_DATE,SolutionListConstants.ORDER_NUMBER,
SolutionListConstants.PRODUCT_CATEGORY,SolutionListConstants.SKU,SolutionListConstants.ITEMS,
SolutionListConstants.SKU_QUANTITY,SolutionListConstants.SKU_AMOUNT,
SolutionListConstants.SOLUTION_CREATE_DATE,SolutionListConstants.SOLUTION_TYPE,
SolutionListConstants.SOLUTION_QUANTITY,SolutionListConstants.SOLUTION_AMOUNT,
SolutionListConstants.CURRENCY,SolutionListConstants.SOLUTION_NOTE};
List
3、SolutionListConstants常量类
public class SolutionListConstants {
private SolutionListConstants() {
}
public static final String TICKET_NUMBER = "Ticket Number";
public static final String TICKET_CREATE_DATE = "Ticket Create Date";
public static final String CUSTOMER_EMAIL = "Customer Email";
public static final String CUSTOMER_NAME = "Customer Name";
public static final String PLATFORM = "Platform";
public static final String MARKET = "Market";
public static final String SELLER = "Seller";
public static final String ORDER_PURCHASE_DATE = "Order Purchase Date";
public static final String ORDER_NUMBER = "Order Number";
public static final String PRODUCT_CATEGORY = "Product Category";
public static final String SKU = "SKU";
public static final String ITEMS = "Items";
public static final String SKU_QUANTITY = "SKU Quantity";
public static final String SKU_AMOUNT = "SKU Amount";
public static final String SOLUTION_CREATE_DATE = "Solution Create Date";
public static final String SOLUTION_TYPE = "Solution Type";
public static final String SOLUTION_QUANTITY = "Solution Quantity";
public static final String SOLUTION_AMOUNT = "Solution Amount";
public static final String CURRENCY = "Currency";
public static final String SOLUTION_NOTE = "Solution Note";
}
四、总结
由于需要传入参数,使用的是post请求,但前端弄了一天下载的数据会出现中文乱码(后端提供的这个接口通过postman测试下载的文件是完好的)遇到这个问题,不要再使用这个方法,请查看我使用的easyExcel生成excel文件的方法,强行将本文中导出的csv文件格式改成.xlsx会导致数据全部在一行,出现问题。



