将DataGridView内容附加到Excel文件 [英] Append DataGridView content to Excel file

查看:72
本文介绍了将DataGridView内容附加到Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows窗体中,我在TabControl中放置了3个DataGridView。我现在所能做的就是将DataGridView内容导出到一个包含3张的新excel文件。但是我还要求将每个DataGridView的内容附加到包含3张的Excel主文件中。关于如何做到这一点的任何想法?



我试过做DataGridView的复制粘贴。它工作但是当它将DataGridView的内容粘贴到masterfile时它放置了第1列DataGridView到excel文件中的第二列,将excel中的第一列留空,它不会将DataGridView内容粘贴到我想要粘贴的工作表上。



以下是我用来复制DataGridView内容的代码:



In my Windows Form I have 3 DataGridView placed within a TabControl. All I can do as of now is to export the DataGridView content to a new excel file with 3 sheets. But then I am also ask to append the content of each DataGridView to an Excel master file with 3 sheets as well. Any Idea on how to do this?

Ive tried doing copy paste of DataGridView.It works but when it paste the content of DataGridView to the masterfile it places the 1st column of DataGridView to the 2nd column in the excel file leaving the 1st column in excel empty and it does not paste the DataGridView content to the sheet I want it to be pasted.

Here is the code I use to copy the DataGridView content:

<pre>private void copyAlltoClipboard2()
        {
            dataGridView4.SelectAll();
            DataObject dataObj = dataGridView4.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }

这个用于粘贴:



This one is for pasting :

string path = "C:\\Users\\Administrator\\Desktop\\SAMPLE EXCEL\\MH.xlsx";
                        mWorkBook = oXL.Workbooks.Open(path, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                        mWorkSheets = mWorkBook.Worksheets;
                        mWSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkBook.Sheets["BBH"];
                        copyAlltoClipboard2();

                        Excel.Range last = mWSheet3.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
                        Excel.Range range = mWSheet3.get_Range("A1", last);

                        mWSheet3.PasteSpecial(range, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);

推荐答案

查看链接并将EnableEventValidation =false设置为表格



http://www.aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx
see the link & also set EnableEventValidation="false" to the form

http://www.aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx


这篇关于将DataGridView内容附加到Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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