Click here to Skip to main content
16,021,417 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
string path = Application.StartupPath;
                    Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                    app.Visible = true;
                    app.DisplayFullScreen = true;
                    Excel.Workbook wb = app.Workbooks.Add(1);
                    Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
                    app.ActiveWindow.DisplayGridlines = true;
                    Excel.Range ChartRange;
                    ws.Shapes.AddPicture(path + "\\Tareta_Image.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 0, 0, 170, 28);

                    ws.Cells[4, 3] = "Key Events Report";
                    ws.Cells[4, 4] = "From:" + Date1.Text + "  To:" + Date2.Text + "";
                    ws.Cells[5, 2] = "Asset Number";
                    ws.Cells[5, 3] = "Asset Description";
                    ws.Cells[5, 4] = "Event Description";
                    ws.Cells[5, 5] = "Event Date";
                    ChartRange = ws.get_Range("c4", "d4");
                    ChartRange.Font.Bold = true;
                    ChartRange.Font.Name = "Microsoft Sans Serif";
                    ChartRange.Font.Size = 11;
                    ChartRange.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(90, 90, 90));
                    

                    ChartRange = ws.get_Range("b5", "e5");
                    ChartRange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic,1);
                    ChartRange.Font.Bold = true;
                    ChartRange.Font.Name = "Microsoft Sans Serif";
                    ChartRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(97, 97, 97));
                    ChartRange.Font.Color=System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(255,255,255));
                    ChartRange.Interior.Pattern = Excel.XlPattern.xlPatternSolid;
                    
                    
                    ChartRange.Font.Size = 10;
                    int k = 7;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        ws.Cells[k, 2] = Excel.XlHAlign.xlHAlignCenter;
                        ws.Cells[k, 2] = ds.Tables[0].Rows[i]["fldAssetNo"].ToString();
                        
                        ws.Cells[k, 3] = ds.Tables[0].Rows[i]["fldAssetDescription"].ToString();
                        ws.Cells[k, 4] = ds.Tables[0].Rows[i]["fldEventDescription"].ToString();
                        ws.Cells[k, 5] = Convert.ToDateTime(ds.Tables[0].Rows[i]["fldEventDate"].ToString()).ToShortDateString();
                        k++;
                    }
                    k = k + 1;
                    ws.Cells[k, 3] = "***** End of the Report *****";
                    ws.Columns.AutoFit();
                }
                else
                {
                    MessageBox.Show("No data to Show");
                }


Response.Clear();
                            Response.Buffer = true;
                            Response.ContentType = "application/vnd.ms-excel";
                            Response.Charset = "";
                            Response.Write("<table><tr><th align="center" colspan="6">Software List By System </th></tr></table>");
                            System.IO.StringWriter tw = new System.IO.StringWriter();
                            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                            GV.RenderControl(hw);
                            Response.AddHeader("Content-Disposition", "attachment;filename=SoftwareListBySystem .xls");
                            Response.Write(tw.ToString());
                            Response.Flush();
                            Response.End();
Posted
Updated 15-May-12 23:10pm
v4
Comments
Alf1har 15-May-12 8:49am    
Hi Aravind

It's not really clear what your question is, or is this just an example of how to export to Excel from C# that you are posting?
ZurdoDev 15-May-12 8:59am    
Do you have a question?
Sandeep Mewara 15-May-12 9:39am    
This is not a well framed question! We cannot work out what you are trying to ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.

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