zoukankan      html  css  js  c++  java
  • 导出CSV格式

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.CollectionUtils;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;

    import javax.servlet.http.HttpServletResponse;
    import javax.validation.Valid;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.UnsupportedEncodingException;
    import java.util.List;
    ....
    ...

    ...

    List<MatchRecordBo> matchRecordBos = matchRecordService.findMatchRecordList(matchRecordQueryBean);
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("匹配用户(微信),信物商品,信物编号,投放信物用户,匹配时间,匹配指数,匹配结果,答题结果").append(" ");
    for (MatchRecordBo item : matchRecordBos) {
    stringBuffer.append(item.getUserName()).append(",");
    stringBuffer.append(item.getProductName()).append(",");
    stringBuffer.append(item.getProductSn()).append(",");
    stringBuffer.append(item.getLeaveUserName()).append(",");
    stringBuffer.append(item.getCreateAt()).append(",");
    stringBuffer.append(item.getMatchScore()).append(",");
    stringBuffer.append(item.getStatus() == 1 ? "失败" : "成功" ).append(",");
    stringBuffer.append(item.getStatus() == 4 ? "成功" : "失败" ).append(" ");
    }

    byte[] byt = new byte[0];
    try {
    byt = stringBuffer.toString().getBytes("gb2312");
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }

    // 设置响应
    response.setContentType("application/octet-stream");
    response.setHeader("Pragma", "public");
    response.setHeader("Cache-Control", "max-age=30");
    response.setHeader("Content-Disposition",
    "attachment;filename="" + new String("匹配记录列表.csv".getBytes("UTF-8"), "iso8859-1"));
    try {
    OutputStream out = response.getOutputStream();
    // 写入输出结果
    out.write(byt);
    out.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return stringBuffer.toString();
    ...
  • 相关阅读:
    P4782 【模板】2-SAT 问题 && 2-SAT问题
    C#连接MySql数据库
    C# 不同类型数组之间的转换
    C#之一维数组,冒泡排序,输入输出案例
    C++之结构体的另类使用
    visual c++ 中的stdafx.h头文件的作用
    C#Windows窗体组成基本思路和控件使用
    C++内存管理
    C++之线程信号量机制
    C#读取文件内容,包括ArrayList使用和类型转换
  • 原文地址:https://www.cnblogs.com/yzf666/p/9669400.html
Copyright © 2011-2022 走看看