将数据导出到execl文件 [英] export data to execl file

查看:68
本文介绍了将数据导出到execl文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview whick显示数据库中的数据。现在我想要在execl文件上导出

我该怎么办?

i have a gridview whick shows the data from the database.now i want that to be export on execl file
how will i do it??

推荐答案

protected void btnExport_Click(object sender, EventArgs e)
   {
           string strReport ="Report";
           GetGridDetails(true); // Function to Bind GridView Data
           ExportDataGrid(dgSRDetails, strReport);
   }

 public void ExportDataGrid(DataGrid oGrid, string exportFile)
    {
        
            //Clear the response, and set the content type and mark as attachment
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=\"" + exportFile + "\"");
            Response.ContentType = "application/excel";

            //Clear the character set
            Response.Charset = "";

            //Create a string and Html writer needed for output
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            //Clear the controls from the pased grid
            ClearControls(oGrid);

            //Show grid lines
            oGrid.GridLines = GridLines.Both;

            //Color header
            oGrid.HeaderStyle.BackColor = System.Drawing.Color.LightGray;

            //Render the grid to the writer
            oGrid.RenderControl(oHtmlTextWriter);

            Response.Write(oStringWriter.ToString());

            Response.Flush();
            Response.End();
    }

 private void ClearControls(Control control)
    {
        //Recursively loop through the controls, calling this method
        for (int i = control.Controls.Count - 1; i >= 0; i--)
        {
            ClearControls(control.Controls[i]);
        }

        //If we have a control that is anything other than a table cell
        if (!(control is TableCell))
        {
            if (control.GetType().GetProperty("SelectedItem") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                try
                {
                    literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
                }
                catch
                {
                }
                control.Parent.Controls.Remove(control);
            }
            else if (control.GetType().GetProperty("Text") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                control.Parent.Controls.Remove(control);
            }
        }
        return;
    }


我在多个项目中使用了ClosedXML,它运行良好且易于使用。



http://closedxml.codeplex.com/ [ ^ ]



从数据表创建excel工作表:

I have used ClosedXML in multiple projects and it works great and it''s easy to use.

http://closedxml.codeplex.com/[^]

Create excel worksheet from datatable:
var wb = new XLWorkbook();
var dataTable = GetTable("Information");
wb.Worksheets.Add(dataTable);
wb.SaveAs("AddingDataTableAsWorksheet.xlsx");


google的兴趣有点大... ....



如何使用c#将数据导出到Excel [ ^ ]



http://stackoverflow.com/questions/ 4772272 / exports-data-to-excel-file-c-net [ ^ ]



http://stackoverflow.com/questions/9972768/export-数据到excel-file-in-an-asp-net-application [ ^ ]



问候

塞巴斯蒂安
The interest to google a bit give big results....

How to export data to Excel using c#[^]

http://stackoverflow.com/questions/4772272/exporting-data-to-excel-file-c-net[^]

http://stackoverflow.com/questions/9972768/export-data-to-excel-file-in-an-asp-net-application[^]

Regards
Sebastian


这篇关于将数据导出到execl文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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