【问题标题】:C# save DataGridView after editing (overwrite Excel file that was imported)C#编辑后保存DataGridView(覆盖导入的Excel文件)
【发布时间】:2019-01-24 12:42:58
【问题描述】:

我有一个小表单,其中包含 3 个 Buttons (Browse & updateExcel & saveExcel)、一个 ComboBox (comboBox1) 和一个 DataGridView (dataGridView1)

第一个按钮让您选择一个 Excel 文件,然后将文件加载到DataGridView

private void Browse_Click(object sender, EventArgs e)
    {

        OpenFileDialog op = new OpenFileDialog();
        op.InitialDirectory = @"C:\";
        op.Title = "Browse Excel Files";
        op.CheckFileExists = true;
        op.CheckPathExists = true;
        op.DefaultExt = "xls";
        op.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.csv";
        op.FilterIndex = 2;
        op.RestoreDirectory = true;
        op.ReadOnlyChecked = true;
        op.ShowReadOnly = true;

        if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            if (File.Exists(op.FileName))
            {
                string[] Arr = null;
                Arr = op.FileName.Split('.');
                if (Arr.Length > 0)
                {
                    if (Arr[Arr.Length - 1] == "xls")
                        sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                        op.FileName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
                }
                else if (Arr[Arr.Length - 1] == "xlsx")
                {
                    sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + op.FileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES';";
                }
            }
            FillData();
        }
    }

这也使用了以下代码:

    public string sConnectionString;
    private void FillData()
    {
        if (sConnectionString.Length > 0)
        {
            OleDbConnection cn = new OleDbConnection(sConnectionString);
            {
                cn.Open();
                DataTable dt = new DataTable();
                OleDbDataAdapter Adpt = new OleDbDataAdapter("select * from [sheet1$]", cn);
                Adpt.Fill(dt);
                dataGridView1.DataSource = dt;
            }
            try { }
            catch (Exception ex)
            {
            }
        }
    }

然后,在您从ComboBox 中选择一个值后,您可以通过单击使用此代码的updateExcel 按钮来根据需要使用此值更新DataGridView

    private void updateExcel_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.RowCount - 1; i++)
        {
            dataGridView1[2, i].Value = ConsigneeCombo.Text;
        }
    }

然后我要做的是单击“保存”按钮 (saveExcel) 并使用 Are you sure you want to save? YesNo 标准 Windows 对话框保存加载的文件。

我已经尝试使用SaveFileDialogue (saveFileDialog1) 来做到这一点:

    private void saveExcel_Click(object sender, EventArgs e)
    {
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.InitialDirectory = @"C:\";
        saveFileDialog1.Title = "Save Excel File";
        saveFileDialog1.DefaultExt = "xls";
        saveFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.csv";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.ShowDialog();

        if (saveFileDialog1.FileName != "")
        {
            System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();
            fs.Close();
        }
    }

但这不起作用,因为它试图将文件另存为新文件(使用新选项卡名称等),并且它实际上无法识别文件类型。

【问题讨论】:

    标签: c# datagridview openfiledialog savefiledialog


    【解决方案1】:

    我使用以下代码解决了这个问题:

        private void saveExcel_Click(object sender, EventArgs e)
        {
    
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.InitialDirectory = @"C:\";
            sfd.Title = "Save Excel Files";
            sfd.CheckPathExists = true;
            sfd.DefaultExt = "xls";
            sfd.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.csv";
            sfd.FileName = "*.csv";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                ToCsV(dataGridView1, sfd.FileName);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 2016-06-11
      • 2013-06-26
      • 2011-11-29
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多