@Test public void testObjectWriteCsvFile() { String[] title = {"id" , "name"}; List<User> objs = new ArrayList<>(); User user = new User(); user.setId(1); user.setName("张三"); objs.add(user); user = new User(); user.setId(2); user.setName("李四"); objs.add(user); CsvUtil.writeCsvFile(fileName, title, objs, t -> { String[] bodyList = new String[2]; bodyList[0] = t.getId() + ""; bodyList[1] = t.getName(); return bodyList; }); }
public static void writeCsvFile(HttpServletResponse response, HttpServletRequest request, String filename, String[] title, List<String[]> bodyList) { response.setContentType("application/octet-stream;charset=GBK"); String fileName = FileUtilies.getFileName(filename, request); response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv"); try (CSVWriter writer = new CSVWriter(response.getWriter())) { write(writer, title, bodyList); } catch (IOException e) { throw new RuntimeException("write csv file of response fail ", e); } }
CsvUtil { public static void writeCsvFile(HttpServletResponse response, HttpServletRequest request, String filename, String[] title, List<String[]> bodyList) { response.setContentType("application/octet-stream;charset=GBK"); String fileName = FileUtilies.getFileName(filename, request); response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv"); try (CSVWriter writer = new CSVWriter(response.getWriter())) { write(writer, title, bodyList); } catch (IOException e) { throw new RuntimeException("write csv file of response fail ", e); } } }
CsvUtil { public static void writeCsvFile(HttpServletResponse response, HttpServletRequest request, String filename, String[] title, List<String[]> bodyList) { response.setContentType("application/octet-stream;charset=GBK"); String fileName = FileUtilies.getFileName(filename, request); response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv"); try (CSVWriter writer = new CSVWriter(response.getWriter())) { write(writer, title, bodyList); } catch (IOException e) { throw new RuntimeException("write csv file of response fail ", e); } }  }
CsvUtil { public static void writeCsvFile(HttpServletResponse response, HttpServletRequest request, String filename, String[] title, List<String[]> bodyList) { response.setContentType("application/octet-stream;charset=GBK"); String fileName = FileUtilies.getFileName(filename, request); response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv"); try (CSVWriter writer = new CSVWriter(response.getWriter())) { write(writer, title, bodyList); } catch (IOException e) { throw new RuntimeException("write csv file of response fail ", e); } }  static void writeCsvFile(HttpServletResponse response, HttpServletRequest request,
                                    String filename, String[] title, List<String[]> bodyList); static void writeCsvFile(HttpServletResponse response, HttpServletRequest request,
			String filename, String[] title, List<T> objs, Function<T, String[]> function); static void writeCsvFile(String filename, String[] title, List<String[]> bodyList); static void writeCsvFile(String filename, String[] title, List<T> objs,
										Function<T, String[]> function); }
CsvUtil { public static void writeCsvFile(HttpServletResponse response, HttpServletRequest request, String filename, String[] title, List<String[]> bodyList) { response.setContentType("application/octet-stream;charset=GBK"); String fileName = FileUtilies.getFileName(filename, request); response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv"); try (CSVWriter writer = new CSVWriter(response.getWriter())) { write(writer, title, bodyList); } catch (IOException e) { throw new RuntimeException("write csv file of response fail ", e); } }  static void writeCsvFile(HttpServletResponse response, HttpServletRequest request,
                                    String filename, String[] title, List<String[]> bodyList); static void writeCsvFile(HttpServletResponse response, HttpServletRequest request,
			String filename, String[] title, List<T> objs, Function<T, String[]> function); static void writeCsvFile(String filename, String[] title, List<String[]> bodyList); static void writeCsvFile(String filename, String[] title, List<T> objs,
										Function<T, String[]> function);  }