通用导出CSV格式文件

@RequestMapping("/doExport")
    public void doExport(Model model, @RequestParam(value = "single") String ids, HttpServletResponse response) {

        logger.info("输入____方法名称:doExport,参数:ids: " + ids);

        List<Map<String, Object>> dataList = null;
        String displayColNames = null;
        String matchColNames = null;
        String fileName = null;
        String content = null;

        dataList = this.phoneCatentryMgrService.doExport(ids);

        // 完成数据csv文件的封装
        displayColNames = "手机商品编码,手机类型,运营商,购机送费虚拟编码,存费送机虚拟编码";
        matchColNames = "PHONE_PARTNUM,PHONE_TYPE_NAME,TELECOM_OPR_NAME,GJSF_PARTNUM,CFSJ_PARTNUM";
        fileName = "phone_info_";

        content = CsvWriter.formatCsvData(dataList, displayColNames, matchColNames);

        try {
            CsvWriter.exportCsv(fileName, content, response);
        } catch (IOException e) {
            logException(logger, e);
        }

    }

public class CsvWriter {

	/** CSV文件列分隔符 */
	private static final String CSV_COLUMN_SEPARATOR = ",";

	/** CSV文件列分隔符 */
	private static final String CSV_RN = "\r\n";

	/**
	 * 
	 * 将检索数据输出的对应的csv列中
	 * */
	public static String formatCsvData(List<Map<String, Object>> data,
			String displayColNames, String matchColNames) {

		StringBuffer buf = new StringBuffer();

		String[] displayColNamesArr = null;
		String[] matchColNamesMapArr = null;

		displayColNamesArr = displayColNames.split(",");
		matchColNamesMapArr = matchColNames.split(",");

		// 输出列头
		for (int i = 0; i < displayColNamesArr.length; i++) {
			buf.append(displayColNamesArr[i]).append(CSV_COLUMN_SEPARATOR);
		}
		buf.append(CSV_RN);

		if (null != data) {
			// 输出数据
			for (int i = 0; i < data.size(); i++) {

				for (int j = 0; j < matchColNamesMapArr.length; j++) {
					buf.append(data.get(i).get(matchColNamesMapArr[j])).append(
							CSV_COLUMN_SEPARATOR);
				}
				buf.append(CSV_RN);
			}
		}
		return buf.toString();
	}
 	public static void exportCsv(String fileName, String content,
			HttpServletResponse response) throws IOException {

		// 设置文件后缀
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhh24mmss");
		String fn = fileName.concat(sdf.format(new Date()).toString() + ".csv");

		// 读取字符编码
		String csvEncoding = PropertiesUtil.getProperty("CSV_ENCODING");

		// 设置响应
		response.setCharacterEncoding(csvEncoding);
		response.setContentType("text/csv; charset=" + csvEncoding);
		response.setHeader("Pragma", "public");
		response.setHeader("Cache-Control", "max-age=30");
		response.setHeader("Content-Disposition", "attachment; filename="
				+ new String(fn.getBytes(), csvEncoding));

		// 写出响应
		OutputStream os = response.getOutputStream();
		os.write(content.getBytes("GBK"));
		os.flush();
		os.close();
	}

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值