导出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("\r\n");
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("\r\n");
}

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();
...

猜你喜欢

转载自www.cnblogs.com/yzf666/p/9669400.html