简体   繁体   English

在出口到Excel没有得到没有。 在C#中以正确的格式

[英]In Export to excel not getting no. in proper format in c#

I am trying to export Grid View in excel and I get formatting issue like number is not getting proper(eg -1.523E+11) also 0 is missing, where value is leading with 0. 我正在尝试在excel中导出Grid View,但出现格式问题,例如数字不正确(例如-1.523E + 11),也缺少0,其中值以0开头。

                Response.ClearContent();
                Response.ClearHeaders();
                DataTable dtGetReportBank = new DataTable();
                dtGetReportBank = objPO.GetReportBank(id);
                gridData.DataSource = dtGetReportBank;
                gridData.DataBind();

                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "BankCMS" + ".xls"));
                Response.ContentType = "application/ms-excel";
                StringWriter sw1 = new StringWriter();
                HtmlTextWriter htw1 = new HtmlTextWriter(sw1);
                gridData.AllowPaging = false;
                dvContent.RenderControl(htw1);
                Response.Write(sw1.ToString());
                Response.End();
                Btn.Visible = false;

Amol, 阿莫尔

The best to way to handle this is using OpenXML. 解决此问题的最佳方法是使用OpenXML。

Create a SpreadsheetDocument -> WorkbookPart -> WorksheetPart -> Cell 创建SpreadsheetDocument-> WorkbookPart-> WorksheetPart->单元格

While creating a Cell, specify the Cell.DataType and you will get whatever you expect. 在创建单元格时,指定Cell.DataType,您将得到期望的结果。

Once the Document is filled, use SpreadsheetDocument.Save() method to save the same 填充文档后,使用SpreadsheetDocument.Save()方法保存相同的文档

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM