如何将三个datagridviews导出到excel中的同一个工作表中? [英] How can I export three datagridviews into the same sheet in excel?

查看:101
本文介绍了如何将三个datagridviews导出到excel中的同一个工作表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好,我希望你能帮助我。我试图在同一张表格中导出三个DataGridViews在Excel中我无法做到。



这是我用来导出一个DataGridView的代码:



Hello everybody I hope you can help me. Im trying to Export Three DataGridViews In a Same Sheet In Excel but I can't do it.

Here is a code that I use to Export One DataGridView:

private void CopyGridToClipboard(DataGridView grid)
        {
            //Exclude row headers
            grid.RowHeadersVisible = false;

            //Exclude column headers
            grid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
            grid.SelectAll();
            DataObject dataObj = grid.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);

            //Set the visibility of row headers back
            grid.RowHeadersVisible = true;
        }










private void Export_Click(object sender, EventArgs e)
        {
            //Copy grid to clipboard
            this.CopyGridToClipboard(dataGridView1);
            //Open the excel application and add a workbook
            Microsoft.Office.Interop.Excel.Application application;
            Microsoft.Office.Interop.Excel.Workbook book;
            Microsoft.Office.Interop.Excel.Worksheet sheet;
            application = new Microsoft.Office.Interop.Excel.Application();
            application.Visible = true;
            book = application.Workbooks.Open("C:\\Doc1.xlsx");
            sheet = (Microsoft.Office.Interop.Excel.Worksheet)
            book.Worksheets.get_Item(1);

                   
            //Paste grid into Cell[9,2]
            Microsoft.Office.Interop.Excel.Range gridRange = Microsoft.Office.Interop.Excel.Range)sheet.Cells[9, 2];
            
            gridRange.Select();
            sheet.PasteSpecial(gridRange);
        }





我的尝试:



我不知道如何复制/粘贴三个datagridviews,也许你有另一种选择。谢谢。



[更新]

我想在Excel工作表中复制三个数据网格。



单元格中的第一个DataGrid [9,1]

单元格中的第二个DataGrid [20,1]

单元格中的第三个DataGrid [30] ,1]



What I have tried:

I don't know how make a copy/paste three datagridviews, maybe you have another option. Thanks.

[Update]
I want to copy the three datagrids in a Excel sheet.

First DataGrid in the cells [9,1]
Second DataGrid in the cells [20,1]
Third DataGrid in the cells [30,1]

推荐答案

应该尝试类似的事情:

Should try something like that :
private void Export_Click(object sender, EventArgs e)
        {
            //Copy grid to clipboard
            this.CopyGridToClipboard(dataGridView1);
            //Open the excel application and add a workbook
            Microsoft.Office.Interop.Excel.Application application;
            Microsoft.Office.Interop.Excel.Workbook book;
            Microsoft.Office.Interop.Excel.Worksheet sheet;
            application = new Microsoft.Office.Interop.Excel.Application();
            application.Visible = true;
            book = application.Workbooks.Open("C:\\Doc1.xlsx");
            sheet = (Microsoft.Office.Interop.Excel.Worksheet)
            book.Worksheets.get_Item(1);

                   
            //Paste grid into Cell[9,2]
            Microsoft.Office.Interop.Excel.Range gridRange = Microsoft.Office.Interop.Excel.Range)sheet.Cells[9, 1];
            
            gridRange.Select();
            sheet.PasteSpecial(gridRange);

            //Copy grid to clipboard
            this.CopyGridToClipboard(dataGridView1);
            //Paste grid into Cell[9,2]
            Microsoft.Office.Interop.Excel.Range gridRange = Microsoft.Office.Interop.Excel.Range)sheet.Cells[20, 1];
            gridRange.Select();
            sheet.PasteSpecial(gridRange);

            //Copy grid to clipboard
            this.CopyGridToClipboard(dataGridView1);
            //Paste grid into Cell[9,2]
            Microsoft.Office.Interop.Excel.Range gridRange = Microsoft.Office.Interop.Excel.Range)sheet.Cells[30, 1];
            gridRange.Select();
            sheet.PasteSpecial(gridRange);
        }



未经测试:)



可能的错误:孤独的非常可疑。


not tested :)

possible bug : the lonely ) is highly suspect.

Microsoft.Office.Interop.Excel.Range gridRange = Microsoft.Office.Interop.Excel.Range)sheet.Cells[20, 1];


这篇关于如何将三个datagridviews导出到excel中的同一个工作表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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