导出为ex​​cel数据格式 [英] Export to excel data format

查看:71
本文介绍了导出为ex​​cel数据格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试将我的数据导出到excel



我有以下代码这是完美的工作。



Hi,

I am trying to export my data into excel

I have below code which is perfectly working.

protected void btnExport_Click(object sender, EventArgs e)
   {
       Handler h1 = new Handler();
       DataSet ds = new DataSet();
       int RoleId = Convert.ToInt16(Request.Cookies["RoleId"].Value);
       if (RoleId == 1)
           ds=h1.get_MilestoneReport(1, Convert.ToInt16(Request.Cookies["BB"].Value),"");
       else if (RoleId == 3)
           ds=h1.get_MilestoneReport(2, Convert.ToInt16(Request.Cookies["BB"].Value),"");
       string attachment = "attachment; filename=MileStoneReport.xls";
       Response.ClearContent();
       Response.AddHeader("content-disposition", attachment);
       Response.ContentType = "application/vnd.ms-excel";
       string tab = "";
       string tab1 = "";
       foreach (DataTable table in ds.Tables)
       {
           foreach (DataColumn column in table.Columns)
           {
               Response.Write(tab1 + column.ColumnName);
               tab1 = "\t";
           }
       }
       tab = "\n";
       foreach (DataRow dr in ds.Tables[0].Rows)
       {
           for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
           {
               Response.Write(tab + dr[i].ToString());
               tab = "\t";
           }
           tab = "\n";
       }
       Response.End();

   }







现在的问题是,数据即将进入excel是不正确的我的意思是

宽度问题,我想为每列放置适当的宽度,一些值将作为




Now the issue is, data which is coming in excel is not coming properly what i mean is
width issue, i want to put proper width for each column and some of the values are coming as

2.07135E+12



如果我双击那个单元格它就在右边,这里是2071351010002。 />


有人请帮我格式化这个excel,以便对人们有意义。





感谢问候,

SUNIL。


if i double click that cell its coming right, here it is 2071351010002.

Somebody please help me in formatting this excel so that it makes sense to people.


Thanks Regards,
SUNIL.

推荐答案

请阅读Pheonyx的评论。我建议你使用OLEDB:

使用MS Excel(xls / xlsx)使用MDAC和Oledb [ ^ ]

使用OLEDB读取和编写Excel [ ^ ]

在ASP.NET中加速上传和导出使用OLEDB和SQL的C# [ ^ ]
Please, read comment by Pheonyx. I would suggest you to use OLEDB:
Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^]
Reading and Writing Excel using OLEDB[^]
Excel Upload and Export in ASP.NET and C# using OLEDB and SQL[^]

如上所述,OLEDB不是您的请求的正确答案。

OleDb是一个DataProvider,无法自定义显示数据的样式(但数据类型是可能的)。



使用OLEDB,您只需按照与开始时的代码类似的方式按原样提供数据。



看看这里:我可以通过oledb-connection和没有ms-office-格式化网络中的彩色excel单元格安装 [ ^ ]



也许你想使用这个excel库
As mentioned above OLEDB is NOT the right answer to your request.
OleDb is a DataProvider and has no ability to customize the style of displaying data (however data types are possible).

With OLEDB you just deliver the data 'as is'
similar to your code at the beginning.

Take a look here: Can I format color excel cells in net by oledb-connection and without ms-office-installation[^]

maybe you want to use this excel library


这篇关于导出为ex​​cel数据格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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