设置charset=shift-jis

项目使用了vue admin element ui中封装好的下载csv文件的方法,直接修改new Blob怎么都不好用。

后来采用了拼数据上去,后更改charset.

1、安装encoding-japanese

2、修改vue admin element ui中的export2Excel.js 文件中的方法

export function export_json_to_excel({
  multiHeader = [],
  header,
  data,
  filename,
  merges = [],
  autoWidth = true,
  bookType = 'csv',
} = {}, wsName) {
  const csvFormat = (col => `"${col}"`);
    
 // 动态拼接数据,包括多级表头的情况
 
// 多级表头
let multiHeaderStr
= ""; if (multiHeader.length > 0) { for (let i = 0; i < multiHeader.length; i++) { multiHeaderStr += multiHeader[i].map(csvFormat).join(",") + (i == multiHeader.length - 1 ? "" : "\n"); } }

// 具体内容 let contentStr
= ""; for (let i = 0; i < data.length; i++) { contentStr += data[i].map(csvFormat).join(",") + "\n"; }

 // 不是多级表头 let headerStr
= ""; headerStr += header.map(csvFormat).join(","); let csvData; if (multiHeader.length > 0) { csvData = [ multiHeaderStr, headerStr, contentStr ].join('\n'); } else { csvData = [ headerStr, contentStr ].join('\n'); } const unicodeList = []; for (let i = 0; i < csvData.length; i += 1) { unicodeList.push(csvData.charCodeAt(i)); } // 変換処理の実施 const shiftJisCodeList = Encoding.convert(unicodeList, 'sjis', 'unicode'); const uInt8List = new Uint8Array(shiftJisCodeList); saveAs(new Blob([uInt8List], { type: 'text/csv' }), `${filename}.${bookType}`); }

 

posted @ 2021-07-02 17:09  小兔子09  阅读(580)  评论(0编辑  收藏  举报