简体   繁体   English

将Nvarchar数据类型导出为CSV

[英]Export Nvarchar Datatype to CSV

I'm attempting to export international characters from query to csv. 我正在尝试将国际字符从查询导出到csv。 Output to the browser works fine, as does writing to Excel using CFSPREADSHEET. 输出到浏览器的效果很好,使用CFSPREADSHEET写入Excel也是如此。

I have the following set up: 我有以下设置:

  • The datasource connection uses JDBC 数据源连接使用JDBC
  • Table fields are nvarchar 表字段是nvarchar

Rendering the file is as follows: 渲染文件如下:

<cfset filepath="c:/test.csv" />
<cfloop query="q">
  <cfset string = "" />
  <cfset string = listAppend(string, q.fieldname) />
  <cffile action="append" file="#filePath#" output="#string#" charset="windows-1252" />
</cfloop>
<cffile action="readbinary" file="#filePath#" variable="bin" charset="windows-1252"> 
<cffile action="delete" file="#filePath#" /> 
<cfheader name="Content-Disposition" value="attachment; filename=Output.csv" charset="windows-1252"> 
<cfcontent type="text/plain" reset="true" variable="#bin#" />

However, certain characters are not converting properly. 但是,某些字符不能正确转换。 For example: 例如:

  • Jørgen shows as Jørgen 约尔根(Jørgen)饰演约尔根(Jørgen)
  • Sälzler shows as Sälzler 萨尔兹勒(Sälzler)饰演萨尔兹勒(Sälzler)

Update - Adding charset="windows-1252" did solve a majority of the bad conversions including the above examples. 更新-添加charset="windows-1252"确实解决了大多数不良转换,包括上述示例。 The following still do not convert 以下仍不转换

  • Krčobić shows Kr?obi? Krčobić表演了Kr?obi?
  • Mirosław shows Miro?aw Mirosław显示Miro?aw
  • Bożek shows Bo?ek Bożek显示Bo?ek

Supposedly Excel has issues with UTF8 CSV files. 据说Excel的UTF8 CSV文件有问题。 One suggestion is to append a BOM to help it along, but results are mixed for different versions. 一种建议是附加一个BOM来帮助它,但是对于不同版本,结果是混合的。 See the full thread for details. 有关详细信息,请参见完整主题。

... build file string ....
<cfset FileWrite(filePath, binaryDecode("EFBBBF", "hex"))>
<cfset FileAppend(filePath, string, "utf-8")>

<cfheader name="Content-Disposition" value="attachment; filename=Output.csv" charset="utf-8"> 
<cfcontent type="text/plain" reset="true" file="#filePath#" deleteFile="false" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM