How I can export a datatable to MS word 2007, excel 2007,csv from asp.net?

Posted by bala3569 on Stack Overflow See other posts from Stack Overflow or by bala3569
Published on 2010-03-23T06:54:02Z Indexed on 2010/03/24 5:43 UTC
Read the original article Hit count: 773

Hi,

I am using the below code to Export DataTable to MS Word,Excel,CSV format & it's working fine. But problem is that this code export to MS Word 2003,Excel 2003 version. I need to Export my DataTable to Word 2007,Excel 2007,CSV because I am supposed to handle more than 100,000 records at a time and as we know Excel 2003 supports for only 65,000 records.

Please help me out if you know that how to export DataTable or DataSet to MS Word 2007,Excel 2007.

 public static void Convertword(DataTable dt, HttpResponse Response,string filename)
{
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".doc");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.word";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
        System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
        dg.DataSource = dt;
        dg.DataBind();
        dg.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
        //HttpContext.Current.ApplicationInstance.CompleteRequest();

}

  public static void Convertexcel(DataTable dt, HttpResponse Response, string filename)
{

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
        System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
        dg.DataSource = dt;
        dg.DataBind();
        dg.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
        //HttpContext.Current.ApplicationInstance.CompleteRequest();

}

 public static void ConvertCSV(DataTable dataTable, HttpResponse Response, string filename)
{

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".csv");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "Application/x-msexcel";
        StringBuilder sb = new StringBuilder();
        if (dataTable.Columns.Count != 0)
        {
            foreach (DataColumn column in dataTable.Columns)
            {
                sb.Append(column.ColumnName + ',');
            }
            sb.Append("\r\n");
            foreach (DataRow row in dataTable.Rows)
            {
                foreach (DataColumn column in dataTable.Columns)
                {
                    if(row[column].ToString().Contains(',')==true)
                    {
                        row[column] = row[column].ToString().Replace(",", "");
                    }
                    sb.Append(row[column].ToString() + ',');
                }
                sb.Append("\r\n");
            }
        }
        Response.Write(sb.ToString());
        Response.End();
        //HttpContext.Current.ApplicationInstance.CompleteRequest();

}

© Stack Overflow or respective owner

Related posts about datatable

Related posts about export-to-excel