Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Sql Table. I want to convert that table data with column names to xlsx file. sql column name is the column header of the Excel file and
User can Open that xlsx file and make update and save..
Posted

Here is my code to generate xls file from gridview

C#
Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=Contacts.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";

        StringWriter StringWriter = new System.IO.StringWriter();
        HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);

        Grv_Data.HeaderRow.Style.Add("background-color", "#FFFFFF");
        for (int i = 0; i < Grv_Data.HeaderRow.Cells.Count; i++)
        {
            Grv_Data.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
            Grv_Data.HeaderRow.Cells[i].Style.Add("height", "20");
        }
        int j = 1;
        foreach (GridViewRow gvrow in Grv_Data.Rows)
        {
            gvrow.BackColor = System.Drawing.Color.White;
            if (j <= Grv_Data.Rows.Count)
            {
                if (j % 2 != 0)
                {
                    for (int k = 0; k < gvrow.Cells.Count; k++)
                    {
                        gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                    }
                }
            }
            j++;
        }

        Grv_Data.RenderControl(HtmlTextWriter);
        Response.Write(StringWriter.ToString());
        Response.End();


Hope this helps a lot...

Enjoy Coding...
 
Share this answer
 
Comments
[no name] 25-Sep-12 4:44am    
Use Epplus... automate the process
__PP__ 25-Sep-12 5:13am    
Thanks for your reply....is there any possible to create xlsx file
[no name] 25-Sep-12 7:05am    
Yeah it supports .xlsx only....
Hi,

you have to just change extension .xls to .xlsx and code working...
 
Share this answer
 
 
Share this answer
 
v2
C#
SqlConnection sqlcon = new SqlConnection("User ID=sa;Password=Mobius@123;Data   Source=MOB-PERLZ-WS192;Initial Catalog=script");
            SqlCommand comm1 = new SqlCommand("SELECT column_name FROM information_schema.columns where table_name='Employee1' order by ordinal_position", sqlcon);
            sqlcon.Open();
           
            Excel.Application excel = new Excel.Application();
            Excel.Workbook wb = excel.Application.Workbooks.Add(true);
            Excel.Worksheet excelSheet = (Excel.Worksheet)excel.ActiveSheet;
           
           
            SqlDataReader readers = comm1.ExecuteReader();
            int col = 1;

            while (readers.Read())
            {

                excelSheet.Cells[1, col] = readers["column_name"].ToString();
                col++;
            
            }

                excel.Visible = true;
                excelSheet.SaveAs(Filename: @"C:\Documents and Settings\Docs\exceldoc16.xlsx");
                sqlcon.Close();
                readers.Close();
 
Share this answer
 
v2

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