Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guru's

i was trying to export Excel from my DataGrid,found this example in microsoft and implement in my button click.

But not sure how to call my data set here.

private void button1_Click(object sender, System.EventArgs e)
        {


            Excel.Application oXL;
            Excel._Workbook oWB;
            Excel._Worksheet oSheet;
            Excel.Range oRng;
            FillExcel();
            try
            {
                //Start Excel and get Application object.
                oXL = new Excel.Application();
                oXL.Visible = true;


                //Get a new workbook.
                oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
                oSheet = (Excel._Worksheet)oWB.ActiveSheet;

                //Add table headers going cell by cell.
                oSheet.Cells[1, 1] = "No";
                oSheet.Cells[1, 2] = "Message ID";
               

                //Format A1:F1 as bold, vertical alignment = center.
                oSheet.get_Range("A1", "C1").Font.Bold = true;
                oSheet.get_Range("A1", "C1").VerticalAlignment =
                    Excel.XlVAlign.xlVAlignCenter;

                // Create an array to multiple values at once.
                string[,] saNames = new string[5, 2];
                //this.dgtbcE_Name.MappingName = "MessageID";
                // saNames[0, 0] = dgtbcE_NamedgtbcE_Name.ToString;
                saNames[0, 1] = "Smith";
                saNames[1, 0] = "Tom";
               

                //Fill A2:B6 with an array of values (First and Last Names).
                oSheet.get_Range("A2", "B6").Value2 = saNames;

                //Fill C2:C6 with a relative formula (=A2 & " " & B2).
                oRng = oSheet.get_Range("C2", "C6");
                oRng.Formula = "=A2 & \" \" & B2";

                //Fill D2:D6 with a formula(=RAND()*100000) and apply format.
                oRng = oSheet.get_Range("D2", "D6");
                oRng.Formula = "=RAND()*100000";
                oRng.NumberFormat = "$0.00";

                //AutoFit columns A:D.
                oRng = oSheet.get_Range("A1", "C1");
                oRng.EntireColumn.AutoFit();

                 //Make sure Excel is visible and give the user control
                //of Microsoft Excel's lifetime.
                oXL.Visible = true;
                oXL.UserControl = true;
            }


private void loadPage()
		{
            
            
			string strSql = "";
			int intSkip = 0;

			intSkip = (this.mintCurrentPage * this.mintPageSize);

			string ItemList = SearchList.SelectedItem.ToString();
            

            //Return All rows and columns in Message Trace Table
            if (ItemList == "ALL")
            {
                
                strSql = "SELECT Top " + this.mintPageSize +
                                  " * FROM tblTraceMessages WHERE MessageId NOT IN " +
                                  "(SELECT TOP " + intSkip + " ID FROM tblTraceMessages)";
            }
	SqlCommand cmd = this.mcnSample.CreateCommand();
			cmd.CommandText = strSql;
            
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			
			DataSet ds = new DataSet();
			da.Fill(ds, "tblTraceMessages");

// Populate Data Grid
			this.dgEmp.DataSource = ds.Tables["tblTraceMessages"].DefaultView;

			// Show Status
			this.lblStatus.Text = (this.mintCurrentPage + 1).ToString() + " / " + this.mintPageCount.ToString();
 
			cmd.Dispose();
			da.Dispose();
			ds.Dispose();
Posted
Updated 25-Jul-11 20:10pm
v2

1 solution

 
Share this answer
 
v2
Comments
shan1395 26-Jul-11 2:28am    
thanks a lot for your help Prerak,can you please guide me what am doing is right based on your URL.

this is the only code i have to call in Button click

private void ExportLinkButton_Click(object sender, System.EventArgs e)
{
string strTitle = �put the grid title here�;
new DataGridExcelExporter(this.YourGridName , this.Page).Export(strTitle);
}
Prerak Patel 26-Jul-11 2:51am    
Yes and include the class file from that article. Give it a try.
shan1395 26-Jul-11 2:57am    
sorry to bother you again how to reffer those classes in my code please ?

and i have no idea what is "this.page"
Prerak Patel 26-Jul-11 3:14am    
Are you using win forms? Check the other link.
shan1395 26-Jul-11 3:50am    
yeah am using winforms

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900