Java之csv格式的Excel文件导出
2018-03-12 10:52
555 查看
public class CsvUtil { private static final Logger logger = LoggerFactory.getLogger(CsvUtil.class); /** CSV文件列分隔符 */ private static final String CSV_COLUMN_SEPARATOR = ","; /** CSV文件列分隔符 */ private static final String CSV_RN = "\r\n";
/**** @param dataList 集合数据* @param colNames 表头部数据* @param mapKey 查找的对应数据* @param1response 返回结果*/public static boolean doExport1(List<Map<String, Object>> dataList, String colNames[], String mapKey[], OutputStream os) { try { StringBuffer buf = new StringBuffer(); // 完成数据csv文件的封装 // 输出列头 for (int i = 0; i < colNames.length[/b]; i++) { buf.append(colNames[i]).append(CSV_COLUMN_SEPARATOR); } buf.append(CSV_RN); if (null != dataList) { // 输出数据 for (int i = 0; i < dataList.size(); i++) { for (int j = 0; j < mapKey.length; j++) { buf.append(dataList.get(i).get(mapKey[j])).append(CSV_COLUMN_SEPARATOR); } buf.append(CSV_RN); } } // 写出响应 os.write(buf.toString().getBytes("GBK")); os.flush(); return true; } catch (Exception e) { logger.error("doExport错误...", e); } return false; } /** * @throws UnsupportedEncodingException * * setHeader */ public static void responseSetProperties(String fileName, HttpServletResponse response) throws UnsupportedEncodingException { // 设置文件后缀 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String fn = fileName + sdf.format(new Date()).toString() + ".csv"; // 读取字符编码 String utf = "UTF-8"; // 设置响应 response.setContentType("application/ms-txt.numberformat:@"); response.setCharacterEncoding(utf); response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "max-age=30"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fn, utf)); }}public Result InBoundCountForDayExport(@RequestParam(required = true) String date, String lineChannel, HttpServletResponse response) {boolean flag = false;List<Map<String, Object>> dataList;List<InBoundCountForDay> list = reportService.InBoundCountForDayExport(date, lineChannel);if (list.size() == 0) {return ResultUtil.notFound();}String fileName = "整体报表";dataList = new ArrayList<>();String sTitles[] ={"日期","时间段","接起率","放弃率","服务水平","进线渠道"};String mapKeys[] ={"date","timeSlot","pickUpRate","giveUpRate","serviceLevel","lineChannel"};Map<String, Object> map;for (InBoundCountForDay inBoundCountForDay : list) {map = new HashMap<>();map.put("date", inBoundCountForDay.getDate());map.put("timeSlot", inBoundCountForDay.getTimeSlot());map.put("pickUpRate", inBoundCountForDay.getPickUpRate());map.put("giveUpRate", inBoundCountForDay.getGiveUpRate());map.put("serviceLevel", inBoundCountForDay.getServiceLevel());map.put("lineChannel", inBoundCountForDay.getLineChannel());dataList.add(map);}//输出流try (final OutputStream os = response.getOutputStream() ) {CsvUtil.responseSetProperties(fileName, response);CsvUtil.doExport1(dataList, sTitles, mapKeys, os);return null;} catch (Exception e) {e.printStackTrace();}if (flag == true) {return ResultUtil.sendSuccessMessage("文件导出成功!");} else {return ResultUtil.sendErrorMessage("文件导出失败!");}}
相关文章推荐
- JAVA以UTF-8导出CSV文件,用excel打开产生乱码的解决方法
- java 导入Excel 文件,兼容Excel 2003(后缀名:xls)及 2007(后缀名:xlsx)的文件,同时还支持csv格式的文件
- 导出CSV格式文件,用Excel打开乱码的解决办法
- JavaScript将页面表格数据导出为Excel、CSV格式文件(结合JQuery EasyUI的grid )
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
- EXCEL VBA中写了个宏把本EXCEL中的表数据批量导出为csv格式的文件
- js和Java导出Excel,csv文件
- Java 实现导出excel表 POI/Java实现不同excel格式(*.xls、*.xlsx)文件的读取
- java导出文件之Excel(CSV)下并以流形式输出
- 基于Java的CSV格式文件处理(excel逗号分隔符文件)
- 导出CSV格式文件,用Excel打开乱码的解决办法
- JAVA以UTF-8导出CSV文件,用excel打开产生乱码的解决方法
- 在Java中导出word、excel格式文件时JSP页面头的设置
- c#实现将Excel文件导出为csv和UTF8格式的txt文件
- php 导出成csv或是excel格式的js文件(带序号)
- Trac - 常见问题 - 导出的csv格式文件在Excel中处理unix时间戳
- java 导出excel 格式与文件扩展名指定的格式不一样
- git日志导出为csv格式文件、excel格式文件
- 网页前端导出CSV,Excel格式文件
- 导出CSV格式文件,用Excel打开乱码的解决办法