我在写作值的主要问题到一个新的CSV文件。

我接收从我解析的所有值的CSV文件。这工作得很好,我带来了这件事,并把它变成一个DataGridView。

我们希望这个文件做的第一件事就是重新安排。既然这样,有两套头:一个沿顶部,一个沿着左侧。基本上,我们希望它周围的其他方法:把左边的头到顶部,顶部的头能够作出努力下文件

现在发生了什么,不过,是下面的代码,使这(再次而非行,正确的)有一条线(即右)与列的正确量CSV ......但是,所有的价值观表是 “1,1,1,1,1,1,1,1,1,1” 等

EG:

我得到的文件像这样:

文件:, 1,2,3,4,5

键入,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;

任何帮助?

谢谢!

有帮助吗?

解决方案

改变的文件,使得 “串writeVal = ...”

现在

对象writeVal =博士[0];

其他提示

我相信你正在寻找数据调换。 见本的DataTable的快速转。

scroll top