简体   繁体   English

使用 Interop.Excel 将多个 DataGridView 剪贴板到 excel

[英]Clipboard multiple DataGridViews to excel using Interop.Excel

in this part im exporting a DataGridView data and pasting it in a new excel file, my problem is that i wanna export three more DataGridViews to the same excel file and sheet, any suggestion?在这部分中,我将导出 DataGridView 数据并将其粘贴到新的 excel 文件中,我的问题是我想将另外三个 DataGridView 导出到同一个 excel 文件和工作表中,有什么建议吗?

private void exportclipboard()
        {
            dataGridView1.SelectAll(); 
            dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
            dataGridView1.MultiSelect = true;
            dataGridView1.SelectAll();
            DataObject dataobj = dataGridView2.GetClipboardContent();
            if (dataobj != null)
                Clipboard.SetDataObject(dataobj);
        }
        private void btn_guardar_Click(object sender, EventArgs e)
        {
            int i = 0;
            if(i == 0)
            {
                exportclipboard();
                Excel.Application excel;
                Excel.Workbook workbook;
                Excel.Worksheet worksheet;

                object missvalue = System.Reflection.Missing.Value;
                excel = new Excel.Application();
                excel.Visible = true;
                workbook = excel.Workbooks.Add(missvalue);
                worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1); //number of sheet
                Excel.Range CR = (Excel.Range)worksheet.Cells[1, 1]; //This is the position where the data will be pasted
                CR.Select();
                worksheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
                i++;
            }
 
        }

I found a way to export four DataGridViews to excel with a saveDialog implement, but the loops are slow.我找到了一种使用 saveDialog 实现将四个 DataGridView 导出到 excel 的方法,但是循环很慢。 Any improvement is welcome.欢迎任何改进。

private void ExportarExcel()
        {
            // Creating a Excel object.
            Excel._Application excel = new Excel.Application();
            Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
            Excel._Worksheet worksheet = null;

            try
            {

                worksheet = workbook.ActiveSheet;

                worksheet.Name = "ExportedFromDatGrid";

                int cellRowIndex = 1;
                int cellColumnIndex = 1;

                //Loop through each row and read value from each column.
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {
                        // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                        if (cellRowIndex == 1)
                        {
                            worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Columns[j].HeaderText;
                        }
                        else
                        {
                            worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                        }
                        cellColumnIndex++;
                    }
                    cellColumnIndex = 1;
                    cellRowIndex++;
                }

                //DATAGRIDVIEW2
                int cellRowIndex1 = 1;
                int cellColumnIndex1 = 3;

                //Loop through each row and read value from each column.
                for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dataGridView2.Columns.Count; j++)
                    {
                        // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                        if (cellRowIndex1 == 1)
                        {
                            worksheet.Cells[cellRowIndex1, cellColumnIndex1] = dataGridView2.Columns[j].HeaderText;
                        }
                        else
                        {
                            worksheet.Cells[cellRowIndex1, cellColumnIndex1] = dataGridView2.Rows[i].Cells[j].Value.ToString();
                        }
                        cellColumnIndex1++;
                    }
                    cellColumnIndex1 = 3;
                    cellRowIndex1++;
                }

                //DATAGRIDVIEW3
                int cellRowIndex2 = 1;
                int cellColumnIndex2 = 5;

                //Loop through each row and read value from each column.
                for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dataGridView3.Columns.Count; j++)
                    {
                        // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                        if (cellRowIndex2 == 1)
                        {
                            worksheet.Cells[cellRowIndex2, cellColumnIndex2] = dataGridView3.Columns[j].HeaderText;
                        }
                        else
                        {
                            worksheet.Cells[cellRowIndex2, cellColumnIndex2] = dataGridView3.Rows[i].Cells[j].Value.ToString();
                        }
                        cellColumnIndex2++;
                    }
                    cellColumnIndex2 = 5;
                    cellRowIndex2++;
                }

                //DATAGRIDVIEW4
                int cellRowIndex3 = 1;
                int cellColumnIndex3 = 7;

                //Loop through each row and read value from each column.
                for (int i = 0; i < dataGridView4.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dataGridView4.Columns.Count; j++)
                    {
                        // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
                        if (cellRowIndex3 == 1)
                        {
                            worksheet.Cells[cellRowIndex3, cellColumnIndex3] = dataGridView4.Columns[j].HeaderText;
                        }
                        else
                        {
                            worksheet.Cells[cellRowIndex3, cellColumnIndex3] = dataGridView4.Rows[i].Cells[j].Value.ToString();
                        }
                        cellColumnIndex3++;
                    }
                    cellColumnIndex3 = 7;
                    cellRowIndex3++;
                }


                //Getting the location and file name of the excel to save from user.
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
                saveDialog.FilterIndex = 2;

                if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    workbook.SaveAs(saveDialog.FileName);
                    MessageBox.Show("Export Successful");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                excel.Quit();
                workbook = null;
                excel = null;
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM