将ASP.Net网格数据导出为ex​​cel w.o.第三方dll

时间:2012-12-03 10:12:29

标签: c# asp.net

我想在不使用任何第三方dll的情况下将网格数据从ASP.Net网页导出到Excel。

任何人都可以告诉我该怎么做吗?

2 个答案:

答案 0 :(得分:0)

Hi PLease使用下面的代码导出到excel:

 FileInfo FI = new FileInfo(Path);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid DataGrd = new DataGrid();
DataGrd.DataSource = dt1; //This is the same dataTable by which you are binding your grid
DataGrd.DataBind();

DataGrd.RenderControl(htmlWrite);
string directory = Path.Substring(0, Path.LastIndexOf("\\"));// GetDirectory(Path);
if (!Directory.Exists(directory))
{
    Directory.CreateDirectory(directory);
}

System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true);
stringWriter.ToString().Normalize();
vw.Write(stringWriter.ToString());
vw.Flush();
vw.Close();
WriteAttachment(FI.Name, "application/vnd.ms-excel", stringWriter.ToString());

上面的代码使用WriteAttachment函数,该函数将附件推送到Response对象中的用户。以下代码显示了WriteAttachment的实现:

public static void WriteAttachment(string FileName, string FileType, string content)
{
    HttpResponse Response = System.Web.HttpContext.Current.Response;
    Response.ClearHeaders();
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
    Response.ContentType = FileType;
    Response.Write(content);
    Response.End();
}

有关详细信息,请Click Here

答案 1 :(得分:0)

我建议你,如果你在GridView的模板字段中使用任何控件,如Bullted List,RadioButtonList,CheckBoxList,并且你想在Excel中导出,那么只使用下面的代码......

public void ExportToExcel(GridView gv, string filename)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", filename + ".xls"));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a table to contain the grid
                Table table = new Table();

                //  include the gridline settings
                table.GridLines = gv.GridLines;

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    // PrepareControlForExport(gv.HeaderRow);

                    table.Rows.Add(gv.HeaderRow);
                }
                //Make Header Coloruful

                for (int j = 0; j 

否则我想建议你绑定Grid使用的DataTable来导出Excel中的数据..并且使用以下代码。

 public void ExportToExcel(DataTable dt, string fileName)
    {
        try
        {
            //**************************Excel Generation starts***************************

            string attachment = "attachment; filename="+fileName+".xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            string tab = "";

            foreach (DataColumn dc in dt.Columns)
            {
                Response.Write(tab + dc.ColumnName);
                tab = "\t";
            }
            Response.Write("\n");

            int ik;
            foreach (DataRow dr in dt.Rows)
            {
                tab = "";
                for (ik = 0; ik