如何导出数据表并导出到XLS [英] How do I export a datatable and export to XLS

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

问题描述

我说我有
数据表dt
我的所有数据都放在"dt"中.如何格式化它的样式,并将其导出为xls文件,以便可以在excel中打开它并对其进行正确格式化?

我看过这里的其他指南,例如
-在C#中生成Excel(XML电子表格) [

Lets say I have
datatable dt
with all my data inside ''dt''. How can I format the style of it, and export it as an xls file so that I can open it up in excel and it will be formatted correctly?

I have looked at other guides on here such as this one
-- Generating Excel (XML Spreadsheet) in C#[^]--
but they don''t really explain much. Any help would be greatly appreciated!

Thanks ~MJ

推荐答案

看看下面的链接.
ASP.NET:将数据表导出到Excel [ ^ ]

只需将内容类型更改为application/xls
Take a look the following link.
ASP.NET: Export a DataTable to Excel[^]

Just change the content type to application/xls
context.Response.ContentType = "application/xls";


希望对您有所帮助.


I hope this helps you well.


尝试CP文章
格式化和导出数据表的方法 [
Try CP article
A way to format and export a DataTable[^]


在按钮单击下编写以下代码:


Response.Clear();
Response.AddHeader("content-disposition","attachment; filename = DocumentReport.xls");
Response.Charset =";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType ="application/vnd.ms-excel";
System.IO.StringWriter stringWrite =新的System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =新的HtmlTextWriter(stringWrite);
gvReports.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
write the following code under button click :


Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=DocumentReport.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 HtmlTextWriter(stringWrite);
gvReports.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();


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

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