道歉什麼肯定是一個笨蛋問題... 我想創建一個CSV文件看起來像這樣: r n的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, "\\$1");
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
在我將字符串傳遞迴javascript之前的調試中,如果我只鍵入?response,我會得到不正確的全部一行響應。然而,當我輸入?反應時,nq可以識別換行符,並且看起來應該是一切。
此外,我敢肯定,我在這裏做錯了/愚蠢的事情。指出這些實例也值得讚賞。
你在調試器或輸出文件中看到'\ r \ n'嗎? – user3185569
兩者。當我請求「?response,nq」時,它們在調試器中消失,但當我請求「?response」時出現, – nwhaught