1

我在将值写入新的 CSV 文件时遇到了一个主要问题。

我收到一个 csv 文件,我从中解析所有值。这很好用,我已经提出了这个并将其放入 datagridview 中。

我们要对这个文件做的第一件事是重新排列它。就目前而言,有两组标题:一组在顶部,一组在左侧。基本上,我们希望它反过来:将左侧的标题放在顶部,顶部的标题沿着文件向下移动。

但是,现在发生的情况是,以下代码生成了一个 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;

有什么帮助吗?

谢谢!

4

2 回答 2

1

更改文件以便“字符串 writeVal = ...”

就是现在

对象 writeVal = dr[0];

于 2009-11-18T12:07:30.320 回答
0

我相信您正在寻找数据转置。请参阅此以快速转置数据表。

于 2009-11-18T12:41:26.413 回答