1
如何正確導出我的數據網格爲CSV?爲什麼行在不同的列?從DataGrid CSV導出 - 錯行
ExportToCsv函數返回:
列標題:
「SR#; 8D報告請求;狀態(ASSIST);在R&d; BTQ數;優先級;目標日期,執行日期,狀態( BTQ)「
行:錯誤!
「1-3271406718;是;是; BTQ00153254; 6 - 增強; 2014年2月22日; 2014年9月9日;完成;工程等。」
如何應該是:
列標題:
「SR#; 8D報告請求;狀態(ASSIST);在R&d; BTQ數;優先級;目標日期;實施日期;狀態(BTQ)」
行:
「1-3271406718; yes; Eng。等待;是; BTQ00153254; 6 - 增強; 2014年2月22日; 2014年9月9日;已完成」
這裏我的代碼:
string CsvFpath = saveDLG.FileName;
StreamWriter csvFileWriter = new StreamWriter(CsvFpath, false);
string columnHeaderText = "";
int countColumn = dgvView.Columns.Count - 1;
if (countColumn >= 0)
{
columnHeaderText = (dgvView.Columns[0].Header).ToString();
}
//Writing column headers
for (int i = 1; i <= countColumn; i++)
{
columnHeaderText = columnHeaderText + ';' + (dgvView.Columns[i].Header).ToString();
}
csvFileWriter.WriteLine(columnHeaderText);
// Writing values row by row
for (int i = 0; i <= dgvView.Items.Count - 2; i++)
{
string dataFromGrid = "";
for (int j = 0; j <= dgvView.Columns.Count - 1; j++)
{
if (j == 0)
{
dataFromGrid = ((DataRowView)dgvView.Items[i]).Row.ItemArray[j].ToString();
}
else
{
dataFromGrid = dataFromGrid + ';' + ((DataRowView)dgvView.Items[i]).Row.ItemArray[j].ToString();
}
}
csvFileWriter.WriteLine(dataFromGrid);
}
csvFileWriter.Flush();
csvFileWriter.Close();
請實際上提供您的CSV的例子爲文本(即,什麼是你的代碼生成)拉澤而不是Excel對該文件的解釋,並將其與問題相關聯,而不是鏈接到圖像。 – JenB
@JenB是這樣的嗎? – Purger86
是的,好多了。我不是C#程序員,所以實際上不能給你一個答案,但在討論csv時,你需要堅持文本格式,因爲在Excel中打開時會隱藏分隔符(例如;或),並在打開文件時作出其他解釋。 – JenB