Reputation: 3622
So, I have found several other SO questions with related answers and tried most of them but nothing seems to be working for me. May be because my case is much complicated than the previous ones.
Here is my csv file which has several special characters [, ' "]
ID,Name,Executable,Host,Timeout,Tags,Visible,
65,one test,SomePROXY=http://10.10.10.10 /abc/cde/efg/some.py -u tom -g 'some-abc-test' --passparams "one,two",example.com,1800,nothing,true,
My Expected excel format is:
ID: 65
Name: one test
Executable: SomePROXY=http://10.10.10.10 /abc/cde/efg/some.py -u tom -g 'some-abc-test' --passparams "one,two"
Host: example.com
Timeout: 1800
Tags: nothing
Visible: true
But due to comma in Executable field, "two" comes in new column. I tried quoting everything under """ (as suggested in other answers) but that doesn't seems worked.
Can anyone please help me here.
Here is my code, which I am trying to use here (for ag-grid table )
$scope.export_data_csv = function(){
var LINE_SEPARATOR = '\r\n';
var COLUMN_SEPARATOR = ',';
var fileName = 'export.csv';
let csvString = '';
let columnsToExport = $scope.ag_grid_options.api.columnController.getAllDisplayedColumns();
// adding column headers.
columnsToExport.map((column) => {
csvString+= column.colDef.headerName;
csvString+= COLUMN_SEPARATOR;
});
csvString+= LINE_SEPARATOR;
// adding content of data currently loaded in the grid.
$scope.ag_grid_options.api.forEachNode( function(node) {
node.columnController.allDisplayedColumns.map((column) => {
let cellContent = node.valueService.getValue(column, node);
if(typeof(cellContent) == 'object'){
cellContent = cellContent.join("; ")
}
csvString+= (cellContent != null) ? cellContent : "";
csvString+= COLUMN_SEPARATOR;
});
csvString+= LINE_SEPARATOR;
});
// for Excel, we need \ufeff at the start
var blobObj = new Blob(["\ufeff", csvString], {
type: "text/csv;charset=utf-8;"
});
// Internet Explorer
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blobObj, fileName);
}
else {
// Chrome
var downloadLink = document.createElement("a");
downloadLink.href = window.URL.createObjectURL(blobObj);
downloadLink.download = fileName;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
}
;
Upvotes: 1
Views: 1628
Reputation: 1130
You have to declare the whole value of 'Excecutable' as string and escape the double quotes inside with an additional double quote.
ID,Name,Executable,Host,Timeout,Tags,Visible,
65,one test,"SomePROXY=http://10.10.10.10 /abc/cde/efg/some.py -u tom -g 'some-abc-test' --passparams ""one,two""",example.com,1800,nothing,true,
Upvotes: 1