한글이 계속 깨져서... 너무 힘들었다.
chatGPT로 해결책을 찾아봤지만...
내가 원하는 결과를 주지 못했다.
document.getElementById("csvDownload").addEventListener("click", function() {
const table = document.getElementById("csvTable");
const theadRow = table.querySelector("thead tr");
const rows = table.querySelectorAll("tbody tr");
const csvRows = [];
// Include the header row
const headerData = [];
theadRow.querySelectorAll("th").forEach(function(cell) {
headerData.push(cell.textContent);
});
csvRows.push(headerData.join(","));
// Include the data rows
rows.forEach(function(row) {
const rowData = [];
row.querySelectorAll("td").forEach(function(cell) {
rowData.push(cell.textContent);
});
csvRows.push(rowData.join(","));
});
const csvContent = csvRows.join("\n");
// 한글깨짐문제 '\ufeff' + data 이것 참조
const blob = new Blob(['\ufeff' + csvContent], { type: "text/csv;charset=utf-8;" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.setAttribute("download", "직원연차정보.csv");
document.body.appendChild(link);
link.click();
});
결국 위의 코드에서
const blob = new Blob(['\ufeff' + csvContent], { type: "text/csv;charset=utf-8;" });
이 부분이 중요하다.
이제 완성된 화면이다.
'IT tech Coding > javascript' 카테고리의 다른 글
자바스크립트, 특정 요소만, 특정페이지만 인쇄하기 (0) | 2024.05.01 |
---|---|
input에서 엔터치면 검색버튼 효과 만들기 (0) | 2024.02.20 |
토너먼트 코드 연구 (대진표 프로그램) (0) | 2023.08.15 |
[대진표 생성 프로그램] td요소의 class의 내용도 저장하는 코드로 만들고 싶다. (0) | 2023.08.14 |
svg파일의 면적계산 연구 프로젝트(1차) 자바스크립트로 계산이 가능할까? (0) | 2023.08.13 |