导入依赖
org.apache.commons commons-collections4 4.4
工具类
import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections4.CollectionUtils; public class CsvExportUtil { /** * CSV文件列分隔符 */ private static final String CSV_COLUMN_SEPARATOR = ","; /** * CSV文件行分隔符 */ private static final String CSV_ROW_SEPARATOR = "\r\n"; /** * @param dataList * 集合数据 * @param titles * 表头部数据 * @param keys * 表内容的键值 * @param os * 输出流 */ public static void doExport(List
在Mybatis查询出List
测试
import com.lixianhe.dao.BookMapper; import com.lixianhe.utils.CsvExportUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.util.List; import java.util.Map; @RestController @Slf4j public class TestController { @Autowired private BookMapper bookMapper; @GetMapping("/c/virtualMachine/export") public void export(HttpServletResponse response) { // 返回的数据必须是List