重複の可能性:
非常に単純なC#CSVリーダー
私のアプリケーションでは、ユーザーはdatagridviewとの間でファイルをエクスポートおよびcsvすることができます。これは問題なく機能しますが、いくつかの小さな問題があります。
ご存知のように、csvファイルはカンマ記号を使用してアイテムを区切ります。
たとえば、セルの1つにコンマ記号が付いている場合があります(2,5時間)。
csvをインポートすると、2、5の間のコンマ記号が区切り文字であると見なされます。これにより、セルに2が、次のセルに5が配置されます。
この問題を解決するにはどうすればよいですか。
これは、csvをインポートするための私のcsvです
dataGridView1.Rows.Clear();
try
{
if (openFileCsv.ShowDialog() == DialogResult.OK)
{
string csvPath = openFileCsv.FileName;
if (System.IO.File.Exists(csvPath))
{
System.IO.StreamReader fileReader = new System.IO.StreamReader(csvPath, false);
//Reading Data
while (fileReader.Peek() != -1)
{
fileRow = fileReader.ReadLine();
fileDataField = fileRow.Split(',');
dataGridView1.Rows.Add(fileDataField);
}
fileReader.Dispose();
fileReader.Close();
}
else
{
MessageBox.Show("CSV Bestand niet gevonden.");
}
}
DataLoaded = true;
}
これは、csvをエクスポートするための私のコードです。
if (saveFileCsv.ShowDialog() == DialogResult.OK)
{
string CsvFpath = saveFileCsv.FileName;
try
{
System.IO.StreamWriter csvFileWriter = new System.IO.StreamWriter(CsvFpath, false);
int countColumn = dataGridView1.ColumnCount - 1;
int iColCount = dataGridView1.Columns.Count;
foreach (DataGridViewRow dataRowObject in dataGridView1.Rows)
{
//Checking for New Row in DataGridView
if (!dataRowObject.IsNewRow)
{
string dataFromGrid = "";
dataFromGrid = dataRowObject.Cells[0].Value.ToString();
for (int i = 1; i <= countColumn; i++)
{
dataFromGrid = dataFromGrid + ',' + dataRowObject.Cells[i].Value.ToString();
}
//Writing Data Rows in File
csvFileWriter.WriteLine(dataFromGrid);
}
}
csvFileWriter.Flush();
csvFileWriter.Close();
}
catch (Exception exceptionObject)
{
MessageBox.Show(exceptionObject.ToString());
}