Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法在没有数据库的WinForms C#应用程序中保存DataGridView表等?_C#_Winforms_Datagridview - Fatal编程技术网

有没有办法在没有数据库的WinForms C#应用程序中保存DataGridView表等?

有没有办法在没有数据库的WinForms C#应用程序中保存DataGridView表等?,c#,winforms,datagridview,C#,Winforms,Datagridview,我(显然)不熟悉WinForms的概念以及SQL等。 我正在写一个程序,作为一个原始的预算助理。 我有一个DataGridView,我在其中创建了一个表,所以我希望用户进行更改,并能够在下次启动应用程序时返回给他们。我现在真的不想使用数据库,决定让我的应用程序将数据保存到excel文件中。它保存了它,但我不知道如何打开文件以再次填充DataGridView。我尝试了这种方法,但得到了System.InvalidOperationException(Microsoft.ACE.OLEDB.12.0

我(显然)不熟悉WinForms的概念以及SQL等。 我正在写一个程序,作为一个原始的预算助理。 我有一个DataGridView,我在其中创建了一个表,所以我希望用户进行更改,并能够在下次启动应用程序时返回给他们。我现在真的不想使用数据库,决定让我的应用程序将数据保存到excel文件中。它保存了它,但我不知道如何打开文件以再次填充DataGridView。我尝试了这种方法,但得到了System.InvalidOperationException(Microsoft.ACE.OLEDB.12.0未在本地计算机上注册,或者类似的东西)。我用谷歌搜索了几乎所有的解决方案,但都没有用。我在应该打开excel文件的函数中不断遇到异常

        private void ToCsV(DataGridView dGV, string filename)
        {
            string stOutput = "";
            // Export titles:
            string sHeaders = "";

            for (int j = 0; j < dGV.Columns.Count; j++)
                sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
            stOutput += sHeaders + "\r\n";
            // Export data.
            for (int i = 0; i < dGV.RowCount - 1; i++)
            {
                string stLine = "";
                for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
                    stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
                stOutput += stLine + "\r\n";
            }
            Encoding utf16 = Encoding.GetEncoding(1254);
            byte[] output = utf16.GetBytes(stOutput);
            FileStream fs = new FileStream(filename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(output, 0, output.Length); //write the encoded file
            bw.Flush();
            bw.Close();
            fs.Close();
        }
        //button for saving
        private void savexmlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel Documents (*.xls)|*.xls";
            sfd.FileName = "export.xls";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //ToCsV(dataGridView1, @"c:\export.xls");
                ToCsV(DtgTable, sfd.FileName);
            }
        }

显然,这就是在我尝试打开xls文件“con.open();”后出现异常的原因。

刚刚按照此处的建议安装了缺少的组件:现在我越来越接近异常:System.Data.OleDb.OLEDBEException:“外部表不是预期的格式。”您可以尝试序列化作为替代方法。举个例子。如果您不介意的话,这是一个vb.net示例。请参阅注释和示例代码。不要介意最初指的是
.csv
文件,这是一样的。非常感谢,我会试着从中找出原因,你为什么不简单地将数据保存为csv文件,然后将其读回?将文件保存为Excel文件似乎是额外的工作,然后您需要使用库来读取它!将数据保存为简单的CSV文件,然后在应用程序启动时将其读回。您似乎将此操作变得比必须的复杂得多,特别是如果您希望避免使用任何类型的数据库或库来读取数据。
    private void OpenxmlToolStripMenuItem_Click_1(object sender, EventArgs e)
    {
        String name = "Items";
        String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                        "C:\\Sample.xlsx" +
                        ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

        OleDbConnection con = new OleDbConnection(constr);
        OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
        con.Open();

        OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
        DataTable data = new DataTable();
        sda.Fill(data);
        DtgTable.DataSource = data;
    }