Java导出csv表格
CSV简介
逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文
本形式存储表格数据(数字和文本)。
示例代码
import java.io.BufferedOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.springbooks.bootTest.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { //BufferedOutputStream @GetMapping("/csv") public void csv(HttpServletResponse resp) throws IOException { resp.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); resp.setHeader("Content-Disposition", "attachment;filename=111.csv"); resp.setCharacterEncoding("UTF-8"); ServletOutputStream os = resp.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(os); String[] titles = new String[]{"id","name","age"}; List<Map<String,String>> list = new ArrayList<>(); for(int i=0;i<10;i++) { Map<String,String> map = new HashMap<>(); map.put("id", String.valueOf(i)); map.put("name", "张三"); map.put("age", String.valueOf(Math.random())); list.add(map); } //添加BOM头,解决中文乱码 bos.write(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }); //bos.write(b); //写表格头 for(String title: titles) { bos.write(title.getBytes("UTF-8")); bos.write(",".getBytes("UTF-8")); } bos.write("\r\n".getBytes("UTF-8")); //写 表格体 for(Map<String,String> line: list) { bos.write(line.get("id").getBytes("UTF-8")); bos.write(",".getBytes("UTF-8")); bos.write(line.get("name").getBytes("UTF-8")); bos.write(",".getBytes("UTF-8")); bos.write(line.get("age").getBytes("UTF-8")); bos.write(",".getBytes("UTF-8")); bos.write("\r\n".getBytes("UTF-8")); } os.flush(); bos.close(); os.close(); } //PrintWriter @GetMapping("/csv2") public void csv2(HttpServletResponse resp) throws IOException { resp.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); resp.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(Math.random()+".csv", "iso8859-1")); resp.setCharacterEncoding("UTF-8"); PrintWriter output = resp.getWriter(); String[] titles = new String[]{"id","name","age"}; List<Map<String,String>> list = new ArrayList<>(); for(int i=0;i<10;i++) { Map<String,String> map = new HashMap<>(); map.put("id", String.valueOf(i)); map.put("name", "张三"); map.put("age", String.valueOf(Math.random())); list.add(map); } //添加BOM头,解决中文乱码 String bom = new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }); output.print(bom); //写表格头 for(String title: titles) { output.print(title); output.print(","); } output.println(""); //写 表格体 for(Map<String,String> line: list) { output.print(line.get("id")); output.print(","); output.print(line.get("name")); output.print(","); output.print(line.get("age")); output.println(","); } output.flush(); output.close(); } }