将网格视图数据转换为excel [英] convert the Grid view data into excel

查看:135
本文介绍了将网格视图数据转换为excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Gridview数据转换为Excel代码,如下所示;



i有两个按钮,如下所示生成(按钮)和导出(按钮)。 />


当我点击Generate按钮时,数据将显示在Gridview中。



然后当我点击导出按钮Gridview数据将显示在Excel中。





导出按钮代码如下;





i am converting the Gridview data into Excel for that code as follows;

i have two button as follows Generate (Button) And Export (Button).

when i click the Generate button the data will be displayed into the Gridview.

Then when i click the Export button the Gridview data will be displayed into the Excel.


Export Button code as follows;


try
     {
         string attachment = "attachment; filename=collectiondetails.xls";
         Response.ClearContent();
         Response.AddHeader("content-disposition", attachment);
         Response.ContentType = "application/vnd.ms-excel";

Response.Write("");
         Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
         Response.Write("<body>");
         Response.Write("<table border="1" bordercolor="black">");

         Response.Write("<tr>");
         Response.Write("<td colspan="5"><center>");
         Response.Write("<font size="4" face="Times New Roman">Batchwise fee collection details</font>");
         Response.Write("</center></td>");
         Response.Write("</tr>");

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
         System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
         GridView1.RenderControl(htmlWrite);
         Response.Write(stringWrite.ToString());

         Response.Flush();
         Response.Write("<table>");
         Response.Write("</body>");
         Response.Write("</html>");
         Response.End();
     }
     catch (Exception Ex)
     {
         Label1.Text = Ex.Message.ToString();
         return;
     }







当我运行时,点击生成按钮数据将显示在Gridview中,

然后当我点击导出按钮显示如下





你有选择打开

collectiondetails.xls



用Microsoft Excel打开

保存文件



当我选择使用Microsoft Excel打开并单击确定时,错误显示如下;



无法读取文件。



从上面的导出按钮代码我上面的代码有什么问题?





我该怎么办?请帮帮我。





问候,

Narasiman P.




When i run, click the Generate button the data will displayed into the Gridview,
then when i click the Export Button shows as follows


You have chosen to open
collectiondetails.xls

Open with Microsoft Excel
Save file

when i select the open with Microsoft Excel and Click Ok, the error shows as follows;

unable to read the file.

From my above Export Button code what is the problem in my above code?


how can i do? please help me.


Regards,
Narasiman P.

推荐答案

Response.Clear();
        Response.Buffer = true;
       
        Response.AddHeader("content-disposition", 
         "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        GridView1.AllowPaging = false;
        GridView1.DataBind(); 

        //Change the Header Row back to white color
        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");

        //Apply style to Individual Cells
        GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
        GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
        GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
        GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");   

        for (int i = 0; i < GridView1.Rows.Count;i++ )
        {
            GridViewRow row = GridView1.Rows[i];

            //Change Color back to white
            row.BackColor = System.Drawing.Color.White;

            //Apply text style to each Row
            row.Attributes.Add("class", "textmode");

            //Apply style to Individual Cells of Alternating Row
            if (i % 2 != 0)
            {
                row.Cells[0].Style.Add("background-color", "#C2D69B");
                row.Cells[1].Style.Add("background-color", "#C2D69B");
                row.Cells[2].Style.Add("background-color", "#C2D69B");
                row.Cells[3].Style.Add("background-color", "#C2D69B");   
            }
        }
        GridView1.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 
        Response.Write(style); 
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();


这篇关于将网格视图数据转换为excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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