\r\n CSV 文件中的字符未被视为换行符
\r\n characters in CSV file not treated as line breaks
抱歉,这肯定是个愚蠢的问题...
我正在尝试创建一个如下所示的 CSV 文件:
Header1,Header2,Header3,
"Value1","Value2","Value3"
相反,我得到了一个如下所示的 CSV 文件:
Header1,Header2,Header3,\r\n"Value1","Value2","Value3"
如何让我的 CRLF 字符在我的输出中实际产生换行符?
我正在做的是 ajax 调用从存储过程生成数据表的 WebMethod。然后将该数据表解析为 CSV,如下所示:
if (!createHeaders)//I deal with the headers elsewhere in the code
{
foreach(DataColumn col in table.Columns){
result += col.ColumnName + ",";
};
result += Environment.NewLine;
}
for (int rowNum = 0; rowNum < table.Rows.Count; rowNum++)
{
for (int colNum = 0; colNum < table.Columns.Count; colNum++)
{
result += "\"" + (table.Rows[rowNum][colNum]).ToString();
result += "\",";
};
result += Environment.NewLine;
};
return result;
}
然后将该字符串传递回 ajax 查询的成功函数,在那里它会经历更多的转换...
function getExportFile(sType) {
var alertType = null
$.ajax({
type: "POST",
url: "./Services/DataLookups.asmx/getExcelExport",
data: JSON.stringify({ sType: sType }),
processData: false,
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (response) {
response = replaceAll(response,"\\"", "\"")
response = response.replace("{\"d\":\"", "")
response = response.replace("\"}", "")
download(sType + ".csv",response)
}
});
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click()
document.body.removeChild(element);
}
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\])/g, "\");
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
在字符串传回 javascript 之前的调试中,如果我只输入 ?response,我会得到不正确的在线响应。但是,当我键入 ?resonse,nq 时,换行符被识别,一切看起来都应该是这样。
另外,我确定我在这里 wrong/stupidly 做了很多事情。指出这些实例也表示赞赏。
您的 header 应该有 data:Application/octet-stream,
作为 MIME-type 而不是 data:text/plain;charset=utf-8,
,原因是根据 HTTP 规范,当内容类型未知时,收件人应将其视为类型 application/octet-stream.
由于您已经在使用 encodeURIComponent()
,这似乎是唯一剩下的问题。
@AryKay 的回答肯定有帮助,但还有一个问题。 Ajax 调用返回的字符串将 \r\n
转义为文字。 运行一个
Response = response.replace (/\r\n/g, "\r\n")
在传递给 encodeURIComponent
之前,它 运行 就像一个魅力。
抱歉,这肯定是个愚蠢的问题... 我正在尝试创建一个如下所示的 CSV 文件:
Header1,Header2,Header3,
"Value1","Value2","Value3"
相反,我得到了一个如下所示的 CSV 文件:
Header1,Header2,Header3,\r\n"Value1","Value2","Value3"
如何让我的 CRLF 字符在我的输出中实际产生换行符?
我正在做的是 ajax 调用从存储过程生成数据表的 WebMethod。然后将该数据表解析为 CSV,如下所示:
if (!createHeaders)//I deal with the headers elsewhere in the code
{
foreach(DataColumn col in table.Columns){
result += col.ColumnName + ",";
};
result += Environment.NewLine;
}
for (int rowNum = 0; rowNum < table.Rows.Count; rowNum++)
{
for (int colNum = 0; colNum < table.Columns.Count; colNum++)
{
result += "\"" + (table.Rows[rowNum][colNum]).ToString();
result += "\",";
};
result += Environment.NewLine;
};
return result;
}
然后将该字符串传递回 ajax 查询的成功函数,在那里它会经历更多的转换...
function getExportFile(sType) {
var alertType = null
$.ajax({
type: "POST",
url: "./Services/DataLookups.asmx/getExcelExport",
data: JSON.stringify({ sType: sType }),
processData: false,
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (response) {
response = replaceAll(response,"\\"", "\"")
response = response.replace("{\"d\":\"", "")
response = response.replace("\"}", "")
download(sType + ".csv",response)
}
});
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click()
document.body.removeChild(element);
}
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\])/g, "\");
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
在字符串传回 javascript 之前的调试中,如果我只输入 ?response,我会得到不正确的在线响应。但是,当我键入 ?resonse,nq 时,换行符被识别,一切看起来都应该是这样。
另外,我确定我在这里 wrong/stupidly 做了很多事情。指出这些实例也表示赞赏。
您的 header 应该有 data:Application/octet-stream,
作为 MIME-type 而不是 data:text/plain;charset=utf-8,
,原因是根据 HTTP 规范,当内容类型未知时,收件人应将其视为类型 application/octet-stream.
由于您已经在使用 encodeURIComponent()
,这似乎是唯一剩下的问题。
@AryKay 的回答肯定有帮助,但还有一个问题。 Ajax 调用返回的字符串将 \r\n
转义为文字。 运行一个
Response = response.replace (/\r\n/g, "\r\n")
在传递给 encodeURIComponent
之前,它 运行 就像一个魅力。