如何在c#中从数据网格格式化datagrid / excel列writtem [英] how to format datagrid/excel column writtem from data grid in c#

查看:219
本文介绍了如何在c#中从数据网格格式化datagrid / excel列writtem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码绑定datagrid



 DataGrid dgGrid = new DataGrid(); 
dgGrid.DataSource = dt;
dgGrid.DataBind();







这个网格我写的是一个excel文件使用系统.io.file如下



 String path = Application [UpldPath]。ToString()+ filename; 
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();

//获取控件的HTML。
dgGrid.RenderControl(hw);

//将HTML写回浏览器
string renderedGridView = tw.ToString();
File.WriteAllText(path,renderedGridView);





这是将excel写入特定路径。但是大量数字都带有科学记数法。



如何在写作时格式化excel专栏?



Plz的建议。

解决方案

你需要在导出时在特定的列上放置显式样式,告诉它​​是字符串列而不是整数。



以下链接详细说明

excel export formatting [ ^ ]


我生成excelsheet时出现同样的问题.....



我已经删除了我的Excel工作表中的编辑/删除按钮,那时我已经使用了



此代码删除特定列< br $>




 dt.Columns.RemoveAt(0); 

dt.Columns.RemoveAt(1);

dt.Columns.RemoveAt(dt.Columns.Count - 1);





希望它对你有所帮助...: - )


I have the following code to bind datagrid

DataGrid dgGrid = new DataGrid();       
dgGrid.DataSource = dt;
dgGrid.DataBind();




this grid i'm writing to an excel file using system .io.file as below

 String path = Application["UpldPath"].ToString() + filename;
      System.IO.StringWriter tw = new System.IO.StringWriter();
      System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

              DataGrid dgGrid = new DataGrid();
              dgGrid.DataSource = dt;
              dgGrid.DataBind();

 //Get the HTML for the control.
               dgGrid.RenderControl(hw);

//Write the HTML back to the browser
               string renderedGridView = tw.ToString();
               File.WriteAllText(path, renderedGridView);



this is writing the excel to the specific path. But large numbers are coming with scientific notation.

How to format the excel column while writing?

Plz advice.

解决方案

You need to put explicit style on the specific column while doing the export to tell that it is a string column not an integer.

Following link show this in detail
excel export formatting[^]


I have same problem occured when i have generate excelsheet.....

I have remove edit/delete button in my excel sheet that time i have used

this code to remove particular column


dt.Columns.RemoveAt(0);

dt.Columns.RemoveAt(1);
            
dt.Columns.RemoveAt(dt.Columns.Count - 1);



hope it's help you...:-)


这篇关于如何在c#中从数据网格格式化datagrid / excel列writtem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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