Advertisement

04.09.2008 at 03:41PM PDT, ID: 23309984
[x]
Attachment Details

Excel version I need to  transfer a datagrid to Excel in a win forms C# application

Asked by lancerxe in C# Programming Language

Hello experts:
I'm trying to add code to transfer a datagrid to excel from a win forms c# application.  Here's the code


For starters I get this error message:
Microsoft.Office.Interop;
Error1      The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)      


Excel.ApplicationClass excel = new ApplicationClass();
Error      1      The type or namespace name 'Excel' could not be found (are you missing a using directive or an assembly reference?)      

What version of Excel do I need todo this?
I looked at my Excel about and it said : 2002  SP3

ThanksStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
Microsoft.Office.Interop; 
private void button1_Click(object sender, EventArgs e)
        {
            Excel.ApplicationClass excel = new ApplicationClass();
 
            excel.Application.Workbooks.Add(true);
            DataTable table = DataSet2.Tables[0];
            int ColumnIndex = 0;
            foreach (Datacolumn col in table.Columns)
            {
                ColumnIndex++;
                excel.Cells[1, ColumnIndex] = col.ColumnName;
            }
            int rowIndex = 0;
            foreach (DataRow row in table.Row)
            {
                rowIndex++;
                ColumnIndex = 0;
                foreach (DataColumn col in table.Columns)
                {
                    ColumnIndex++;
                    excel.Cells[rowIndex + 1, ColumnIndex] = row.Cells[col.ColumnName].Text;
                }
            }
            excel.Visible = true;
            Worksheet worksheet = (Worksheet)excel.ActiveSheet;
            worksheet.Activate();
 
            //string strTitle = dataGridView1;
           // new DataGridExcelExporter(this.dataGridView1 , this.Page).Export(strTitle);
 
        }
 
 
[+][-]04.09.2008 at 06:09PM PDT, ID: 21320748

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C# Programming Language
Sign Up Now!
Solution Provided By: cottsak
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628