下载csv文件、excel打开不乱码

如下:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

@Controller
public class TestController {

    @RequestMapping(value = "/test")
    public void erst(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String reportName = "export.csv";
        response.setContentType("application/octet-stream; charset=UTF-8");
        response.setHeader("Content-Encoding", "UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + reportName);


        OutputStream outputStream = null;
        outputStream = response.getOutputStream();
        outputStream.write(0xEF);   // 1st byte of BOM
        outputStream.write(0xBB);
        outputStream.write(0xBF);   // last byte of BOM
        // now get a PrintWriter to stream the chars.



        PrintWriter os = new PrintWriter(new OutputStreamWriter(outputStream,"UTF-8"));
        
        StringBuffer sb = new StringBuffer();        
        sb.append("Sender Number,Campaign Title,Requested Time手动阀手动阀,Sent Time,Size,Status,Recipients");
        sb.append("\n");sb.append("85245699654" + ",");
        
        os.print(sb);
        os.flush();
        os.close();


    }

}

 

 

版权声明:
作者:黯然gg
链接:https://3zi.cn/archives/695
来源:阿龙爱吃肉
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭
目 录