csv文件当我在windows系统下载时,日文字符变得垃圾

时间:2018-04-06 06:37:02

标签: javascript angularjs csv character-encoding

HTML

<button class="btn btn-raised btn-inverse btn-sm" charset="shift_jis" add-bom="true" ng-csv="getSampleData" csv-header="['コード','勘定科目名','Main_code']" filename="{{ samplefilename }}.csv" field-separator="{{separator}}" decimal-separator="{{decimalSeparator}}" ><span translate> load.common.sample </span> </button>

控制器

$scope.csv = {
     content: null,
     header: true,
     headerVisible: true,
     separator: ',',
     separatorVisible: true,
     result: null,
     encodingVisible: true,
     uploadButtonLabel: "upload a csv file"
   };

$scope.samplefilename = "Sample Support Code List"

$scope.getSampleData = [{コード: 'code _1',勘定科目名: '勘定科目名',Main_code:'maincode1'},{コード: 'code_2',勘定科目名: 'name_2', Main_code: 'Miancode2'}];

在Ubuntu或Mac中下载csv文件时,日文字符看起来不错,而在windows系统数据(日文数据)中下载则会出现垃圾

这是垃圾数据的屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:0)

在此问题中,主要问题是不支持日语字符的UTF-8编码,因此您必须使用shift_jis编码创建文件。

filename =“ filename_123.csv”

此处main_array是标头数据和所有行数据的集合

CSV.open(“#{Rails.root} / public / FOLDER_NAME /#{filename}”,“ w”,编码:params [:encoding])做| csv |   main_array.each做| array |     csv <<数组   结束 结束

csv = CSV.read(“#{Rails.root} / public / FOLDER_NAME /#{filename}”,编码:params [:encoding])

csv = CSV.read(“#{Rails.root} / public / FOLDER_NAME /#{filename}”,编码:params [:encoding],标头:true)

File.write(“#{Rails.root} / public / FOLDER_NAME /#{filename}”,csv,编码:params [:encoding])

文件<<“#{filename}”

send_file“#{Rails.root} / public / FOLDER_NAME /#{filename}”,文件名:ERB :: Util.url_encode(“#{filename}”),:type =>“ application / csv; charset = #{params [:encoding] =='shift_jis'?'shift_jis':'UTF-8'}“