如何从 asp.net 将数据表导出到 MS word 2007、excel 2007、csv?

发布于 2024-08-25 18:47:36 字数 3287 浏览 4 评论 0原文

我使用下面的代码将数据表导出为 MS Word、Excel、CSV 格式和工作正常。但问题是此代码导出到 MS Word 2003、Excel 2003 版本。我需要将我的 DataTable 导出到 Word 2007、Excel 2007、CSV,因为我应该一次处理超过 100,000 条记录,而我们知道 Excel 2003 仅支持 65,000 条记录。

如果您知道如何将 DataTable 或 DataSet 导出到 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();

}

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();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2024-09-01 18:47:36

使用application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Use application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文