• <legend id='8w0Yd'><style id='8w0Yd'><dir id='8w0Yd'><q id='8w0Yd'></q></dir></style></legend>

    <small id='8w0Yd'></small><noframes id='8w0Yd'>

    <tfoot id='8w0Yd'></tfoot>
    <i id='8w0Yd'><tr id='8w0Yd'><dt id='8w0Yd'><q id='8w0Yd'><span id='8w0Yd'><b id='8w0Yd'><form id='8w0Yd'><ins id='8w0Yd'></ins><ul id='8w0Yd'></ul><sub id='8w0Yd'></sub></form><legend id='8w0Yd'></legend><bdo id='8w0Yd'><pre id='8w0Yd'><center id='8w0Yd'></center></pre></bdo></b><th id='8w0Yd'></th></span></q></dt></tr></i><div id='8w0Yd'><tfoot id='8w0Yd'></tfoot><dl id='8w0Yd'><fieldset id='8w0Yd'></fieldset></dl></div>
      <bdo id='8w0Yd'></bdo><ul id='8w0Yd'></ul>

        EPPlus - LoadFromCollection - 文本转换为数字

        时间:2023-05-21

      1. <i id='eYk6j'><tr id='eYk6j'><dt id='eYk6j'><q id='eYk6j'><span id='eYk6j'><b id='eYk6j'><form id='eYk6j'><ins id='eYk6j'></ins><ul id='eYk6j'></ul><sub id='eYk6j'></sub></form><legend id='eYk6j'></legend><bdo id='eYk6j'><pre id='eYk6j'><center id='eYk6j'></center></pre></bdo></b><th id='eYk6j'></th></span></q></dt></tr></i><div id='eYk6j'><tfoot id='eYk6j'></tfoot><dl id='eYk6j'><fieldset id='eYk6j'></fieldset></dl></div>

      2. <tfoot id='eYk6j'></tfoot>
            • <legend id='eYk6j'><style id='eYk6j'><dir id='eYk6j'><q id='eYk6j'></q></dir></style></legend>
                <tbody id='eYk6j'></tbody>

                <bdo id='eYk6j'></bdo><ul id='eYk6j'></ul>

                1. <small id='eYk6j'></small><noframes id='eYk6j'>

                2. 本文介绍了EPPlus - LoadFromCollection - 文本转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在用 C# 编写一个程序,需要将 List<MyObject> 导出到 Excel 中,我正在使用 EPPlus 这样做.

                  I am writing a program in C# that needs to export a List<MyObject> into Excel and I'm using EPPlus for doing so.

                  我的挑战是我的对象有一个属性:

                  My challenge is that my object has a property:

                  string Prop1 { get; set; }
                  

                  而且,我需要导出的值之一有一个值,例如,格式为 Prop1 = "123E4".

                  And, one of the values I need to export has a value that, for example, is of the form of Prop1 = "123E4".

                  挑战在于 EPPlus LoadFromCollection 方法将其导出到 Excel,但 Excel 使用科学计数法将其转换为数字(输出值 = 1.23E+06 或 <代码>1230000).

                  The challenge is that the EPPlus LoadFromCollection method exports this to Excel, but Excel converts it into a number using scientific notation (Outputted value = 1.23E+06 or 1230000).

                  我尝试将整个列设置为 .Style.Numberformat.Format = "@" (以及我能想到的任何其他样式),我什至尝试过设置样式在调用 LoadFromCollection 方法之后.

                  I've tried setting the entire column to .Style.Numberformat.Format = "@" (and any other style I could think of) and I've even tried setting the style before and after the LoadFromCollection method is called.

                  我也尝试在字符串前面加上 ' 字符,但这实际上将该字符保留在该列中的每个单元格中,从而使值不适合分析.

                  I also tried preceding the string with a ' character, but that actually keeps that character in each cell within that column which then makes the values incorrect for analysis.

                  我正在尝试将我的 List 转换为 DataTable 以便使用 LoadFromDataTable 方法,但即使这样似乎也不起作用.

                  I'm playing around with converting my List to a DataTable so as to use the LoadFromDataTable method, but even that seems to not be working.

                  关于如何将其导出为纯文本的任何想法/建议

                  Any ideas / suggestions on how I can export this as pure text

                  推荐答案

                  如果你有看起来像数字的字符串,Excel 会在单元格的角落用绿色三角形警告你.这是假设您正在使用 .ToString() 之类的东西将数字(如果它们是数字)转换为字符串.在 Excel 中无法解决此问题,但您可以使用 XML 操作为该条件打开禁用警告消息,因为 EPPlus 本身不具备此功能.

                  If you have string that look like numbers Excel will warn you with those green trigangles in the corner of the cells. This is assuming you are converting the numbers (if they are numbers) to string using something like .ToString(). There is not way to get around this in Excel but you could turn on the disable warning message for that condition using XML maniulation since EPPlus does not have the ability natively.

                  这样的事情可以做到:

                  public class TestObject
                  {
                      public int Col1 { get; set; }
                      public int Col2 { get; set; }
                      public string Col3 { get; set; }
                  }
                  
                  [TestMethod]
                  public void Number_String_Test()
                  {
                      //Throw in some data
                      var datalist = new List<TestObject>();
                  
                      for (var i = 0; i < 10; i++)
                      {
                          datalist.Add(new TestObject
                          {
                              Col1 = i,
                              Col2 = i *10,
                              Col3 = (i*10) + "E4"
                          });
                      }
                  
                      //Create a test file
                      var fi = new FileInfo(@"c:	emp
                  umtest.xlsx");
                      if (fi.Exists)
                          fi.Delete();
                  
                      using (var pck = new ExcelPackage(fi))
                      {
                          var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
                          worksheet.Cells.LoadFromCollection(datalist);
                  
                          //This would be the variable drawn from the desired cell range
                          var range = "C1:C11";
                  
                          //Get reference to the worksheet xml for proper namspace
                          var xdoc = worksheet.WorksheetXml;
                  
                          //Create the import nodes (note the plural vs singular
                          var ignoredErrors = xdoc.CreateNode(XmlNodeType.Element, "ignoredErrors", xdoc.DocumentElement.NamespaceURI);
                          var ignoredError = xdoc.CreateNode(XmlNodeType.Element, "ignoredError", xdoc.DocumentElement.NamespaceURI);
                          ignoredErrors.AppendChild(ignoredError);
                  
                          //Attributes for the INNER node
                          var sqrefAtt = xdoc.CreateAttribute("sqref");
                          sqrefAtt.Value = range;
                  
                          var flagAtt = xdoc.CreateAttribute("numberStoredAsText");
                          flagAtt.Value = "1";
                  
                          ignoredError.Attributes.Append(sqrefAtt);
                          ignoredError.Attributes.Append(flagAtt);
                  
                          //Now put the OUTER node into the worksheet xml
                          xdoc.LastChild.AppendChild(ignoredErrors);
                  
                          pck.Save();
                      }
                  }
                  

                  这篇关于EPPlus - LoadFromCollection - 文本转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 .NET 格式化大数 下一篇:一次格式化 Visual Studio 项目中的所有文件

                  相关文章

                    <bdo id='d9SIj'></bdo><ul id='d9SIj'></ul>
                  1. <i id='d9SIj'><tr id='d9SIj'><dt id='d9SIj'><q id='d9SIj'><span id='d9SIj'><b id='d9SIj'><form id='d9SIj'><ins id='d9SIj'></ins><ul id='d9SIj'></ul><sub id='d9SIj'></sub></form><legend id='d9SIj'></legend><bdo id='d9SIj'><pre id='d9SIj'><center id='d9SIj'></center></pre></bdo></b><th id='d9SIj'></th></span></q></dt></tr></i><div id='d9SIj'><tfoot id='d9SIj'></tfoot><dl id='d9SIj'><fieldset id='d9SIj'></fieldset></dl></div>
                  2. <legend id='d9SIj'><style id='d9SIj'><dir id='d9SIj'><q id='d9SIj'></q></dir></style></legend>

                    1. <small id='d9SIj'></small><noframes id='d9SIj'>

                    2. <tfoot id='d9SIj'></tfoot>