본문 바로가기

IT

[Spring 강좌]CSV 생성 후 다운로드 하기

반응형

CSV 생성 후 다운로드 하기

@RequestMapping("/csvDownload")
public ResponseEntity<String> getTotalList() {
 
   
 
   //CSV는 UTF-8로 하면 깨지고, MS949 로 세팅해야 된다.
   //import org.springframework.http.HttpHeaders; 
   HttpHeaders header = new HttpHeaders();
   header.add("Content-Type", "text/csv; charset=MS949");
   header.add("Content-Disposition", "attachment; filename=\"" + "total.csv" + "\""); 
   
  
  //내용에 콤마가 있을경우 쌍따움표로 감싼다.
   String csvData = "가, "나,1,2,3" , 다, \n 1, 2, 3";
   return new ResponseEntity<String>(csvData , header, HttpStatus.CREATED);
}
반응형