新しいCSVファイルへの値の書き込みに大きな問題があります。
すべての値を解析するcsvファイルを受け取ります。これは正常に機能します。これを表示して、datagridviewに配置しました。
このファイルで最初にやりたいことは、ファイルを再配置することです。現状では、2セットのヘッダーがあります。1つは上部に沿っており、もう1つは左側に沿っています。基本的には、逆の方法が必要です。左側のヘッダーを上部に配置し、上部のヘッダーをファイルの下に配置します。
ただし、次のコードは、正しい列数(行ではなく正しい)の1行(正しい)のCSVを作成しますが、テーブルの値はすべて「 1、1、1、1、1、1、1、1、1、1インチなど。
例えば:
私はそのようなファイルを取得します:
ファイル:、1、2、3、4、5
タイプa、9%、10%、11%、12%、13%
タイプb、9%、10%、11%、12%、13%
タイプc、9%、10%、11%、12%、13%
タイプd、9%、10%、11%、12%、13%
私はそれをそのようにしたい:
ファイル:、タイプa、タイプb、タイプc、タイプd
1、9%、9%、9%、9%
2、10%、10%、10%、10%
3、11%、11%、11%、11%
4、12%、12%、12%、12%
5、13%、13%、13%、13%
これが私がこれまでに持っているコードです:
if (!File.Exists(path))
return null;
string full = Path.GetFullPath(path);
string file = Path.GetFileName(full);
string dir = Path.GetDirectoryName(full);
//create the "database" connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=\"" + dir + "\\\";"
+ "Extended Properties=\"text;HDR=No;FMT=Delimited\"";
//create the database query
string query = "SELECT * FROM [" + file + "]";
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
//Get the CSV file to change position.
try
{
//fill the DataTable
dAdapter.Fill(dTable);
string activedir = dir;
//Now write in new format.
StreamWriter sw = new StreamWriter(File.Create(dir + "\\modified_" + file.ToString()));
int iRowCount = dTable.Rows.Count;
foreach(DataRow dr in dTable.Rows)
{
string writeVal = (string)dTable.Columns[0].ToString();
sw.Write(writeVal);
sw.Write(",");
}
sw.Write(sw.NewLine);
sw.Close();
dAdapter.Dispose();
}
catch (InvalidOperationException /*e*/)
{ }
string newPath = dir + "\\modified_" + file.ToString();
if (!File.Exists(newPath))
return null;
string f = Path.GetFullPath(newPath);
string fi = Path.GetFileName(f);
string di = Path.GetDirectoryName(f);
//create the "database" connection string
string conn = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=\"" + di + "\\\";"
+ "Extended Properties=\"text;HDR=No;FMT=Delimited\"";
//create the database query
string q = "SELECT * FROM [" + fi + "]";
//create a DataTable to hold the query results
DataTable dTe = new DataTable();
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dA = new OleDbDataAdapter(q, connString);
//Get the CSV file to change position.
try
{
//fill the DataTable
dA.Fill(dTe);
}
catch (InvalidOperationException /*e*/)
{ }
return dTe;
何か助けはありますか?
ありがとう!