导出到Excel时在gridview中删除格式化整数数据

问题描述:



我正在用c#开发一个Web应用程序,其中有一个包含grodview的页面.Gridview具有整数数据,还有一个按钮可以将gridview数据导出到excel文件.导出到excel工作正常.但是,当gridview具有较大的整数值时,该数字将在excel工作表中格式化.谁能告诉我如何在excel中使用数字格式.请在下面找到代码.

导出代码:

Hi,

I am developing a web application with c# in which there there is a page which containins a grodview.Gridview is having integer data and there is also a button to export the gridview data to excel file. Exporting to excel is working fine. But when the gridview is having large integer values, the number is getting formatted in the excel sheet. Can anyone tell me how to preent the formatting of numbers in excel.Please find the code below.

Code to export:

/// <summary>
        /// Export To Excel
        /// </summary>
public static void Export(string fileName, GridView gv)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader(
                "content-disposition", string.Format(CultureInfo.CurrentCulture, "attachment; filename={0}", fileName));
            HttpContext.Current.Response.ContentType = "application/ms-excel";

            using (StringWriter sw = new StringWriter(CultureInfo.CurrentCulture))
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {                   
                    Table table = new Table();
                    table.GridLines = gv.GridLines;                   
                    if (gv.HeaderRow != null)
                    {
                        GridViewExportUtility.PrepareControlForExport(gv.HeaderRow);
                        table.Rows.Add(gv.HeaderRow);
                    }                  
                    foreach (GridViewRow row in gv.Rows)
                    {
                        GridViewExportUtility.PrepareControlForExport(row);
                        table.Rows.Add(row);
                    }
                   
                    if (gv.FooterRow != null)
                    {
                        GridViewExportUtility.PrepareControlForExport(gv.FooterRow);
                        table.Rows.Add(gv.FooterRow);
                    }
                   
                    table.RenderControl(htw);
                  
                    HttpContext.Current.Response.Write(sw.ToString());
                    HttpContext.Current.Response.End();
                }
            }
        }

/// <summary>
        /// Replace any of the contained controls with literals
/// </summary>
private static void PrepareControlForExport(Control control)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                Control current = control.Controls[i];
                if (current is LinkButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
                }
                else if (current is Image)
                {
                    control.Controls.Remove(current);
                }
                else if (current is ImageButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
                }
                else if (current is HyperLink)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
                }
                else if (current is DropDownList)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
                }
                else if (current is CheckBox)
                {
                    control.Controls.Remove(current);
                }
               
                if (current.HasControls())
                {
                    GridViewExportUtility.PrepareControlForExport(current);
                }
            }
        }



Gridview数据:

状态 问题编号

已发送637
已保存3409009408908908

Excel数据:

状态 事项编号

已发送637
已保存 3.40901E + 15

请帮忙.

预先感谢,
Sruthi



Gridview Data:

Status Matter Number

Sent 637
Saved 3409009408908908

Excel Data:

Status Matter Number

Sent 637
Saved 3.40901E+15

Please help.

Thanks in advance,
Sruthi

尝试对每个单元格进行跟踪

在导出每个单元格之前将数据转换为字符串并将其与"连接
例如:-
如果单元格包含值123456,则

try following with every cell

before Exporting each cell Convert Data to string and concatenate it with "''"
ex:-
if Cell has contain value 123456, then

"'" + (123456).ToString()


关于导出到Excel有一些研究,您可以检查一下:
将数据导出到Excel [ 9将数据导出到Excel for ASP的解决方案. NET [ ^ ]
导出C#的Excel文件 [
Hi, there are some research on exporting to Excel, you may have a check:
Exporting Data to Excel[^]
9 Solutions to Export Data to Excel for ASP.NET[^]
Export Excel File for C#[^]