编辑DataGridView并在excel文件中保存更新的单元格值(源) [英] Edit DataGridView and save updated cell value in excel file (source)

查看:104
本文介绍了编辑DataGridView并在excel文件中保存更新的单元格值(源)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个C#项目,我将数据导出到excel文件,然后使用sql查询导入该excel文件数据。有一列
我想稍后通过点击datagridview中的单元格进行编辑。在datagridview中写入该单元格中的任何值后,它还应该通过更新excel文件中的该单元格来更新excel文件。以下是一个示例: 

$ b时$ b我点击并编辑AppealDisposal下面的Cell,然后也应该在source excel文件中编辑这个单元格。怎么做?

推荐答案

您好BushraT,

Hi BushraT,

>>当我点击并编辑AppealDisposal下面的Cell时,也应在源excel文件中编辑此单元格。怎么做?

>>When I click and edit Cell below AppealDisposal then this cell should also be edited in source excel file. How to do that?

您可以在dataGridView1_CellEndEdit事件中将数据更新到Excel:

You can update the data to the Excel in the dataGridView1_CellEndEdit event:

        public static void ExportExcel(string fileName, DataGridView myDGV)
        {
            if (myDGV.Rows.Count > 0)
            {
                string saveFileName = "";
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.DefaultExt = "xls";
                saveDialog.Filter = "Excel File|*.xls";
                saveDialog.FileName = fileName;
                saveDialog.ShowDialog();
                saveFileName = saveDialog.FileName;
                if (saveFileName.IndexOf(":") < 0) return;   
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                if (xlApp == null)
                {
                    MessageBox.Show("can not create excel, your PC may not install the excel");
                    return;
                }

                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; 
                
                for (int i = 0; i < myDGV.ColumnCount; i++)
                {
                    worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
                }
                
                for (int r = 0; r < myDGV.Rows.Count; r++)
                {
                    for (int i = 0; i < myDGV.ColumnCount; i++)
                    {
                        worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
                    }
                    Application.DoEvents();
                }
                worksheet.Columns.EntireColumn.AutoFit();

                if (saveFileName != "")
                {
                    try
                    {
                        workbook.Saved = true;
                        workbook.SaveCopyAs(saveFileName);
                    }
                    catch (Exception ex)
                    { 
                        MessageBox.Show("export error, the file may be opened now\n" + ex.Message);
                    }

                }
                xlApp.Quit();
                GC.Collect();
                MessageBox.Show(fileName + "save successful", "prompt", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("the datagridview is empty", "prompt", MessageBoxButtons.OK);
            }
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string str = "MyDgv";
            ExportExcel(str, dataGridView1);
        }

最好的问候,

Stanly


这篇关于编辑DataGridView并在excel文件中保存更新的单元格值(源)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆