正文

ASP.NET 保存DataTable 到Excel文件2006-10-26 19:55:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/hotphen/19726.html

分享到:

private void SaveToExcel(DataTable objTable)    {        int CountR = objTable.Rows.Count;//行数        int CountC = objTable.Columns.Count;//列数        Response.Clear();        Response.Buffer = true;         //设置Http的头信息,编码格式        Response.AppendHeader("Content-Disposition", "attachment;filename=result.xls");        Response.ContentType = "application/ms-excel";         //设置编码        Response.Charset = "GB2312";        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");        //写表头        for (int i = 0; i < CountC; i++)        {            Response.Write(objTable.Columns[i].ColumnName+"\t");        }        Response.Write("\n");        //写表内容        for (int RowNo = 0; RowNo <= CountR - 1; RowNo++)        {            string RowContent = "";            for (int CloumnNo = 0; CloumnNo <= CountC - 1; CloumnNo++)            {                RowContent += Convert.ToString(objTable.Rows[RowNo][CloumnNo]) + "\t";            }            RowContent += "\n";            Response.Write(RowContent);        }        Response.End();    }

阅读(4696) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册