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系统数据(日文数据)中下载则会出现垃圾
这是垃圾数据的屏幕截图
答案 0 :(得分:0)
在此问题中,主要问题是不支持日语字符的UTF-8编码,因此您必须使用shift_jis编码创建文件。
filename =“ filename_123.csv”
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'}“