编辑后C#保存DataGridView(覆盖导入的Excel文件)

问题描述 投票:0回答:1

我有一个小形式有3 ButtonsBrowseupdateExcelsaveExcel),ComboBoxcomboBox1)和DataGridViewdataGridView1

第一个按钮允许您选择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中选择一个值后,您可以通过单击使用此代码的DataGridView按钮根据需要更新updateExcel

    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对话框保存加载的文件。

我尝试使用SaveFileDialoguesaveFileDialog1)来做到这一点:

    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个回答
0
投票

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

    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);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.