您的位置:首页 > 编程语言 > Java开发

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("文件导出失败!");}}
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: