c# - excel 2010 类型冲突 NumberFormat

标签 c# excel excel-2010

我有一个从数据库表创建 Excel 文件的小程序,使用 Excel 2013 一切正常,但我现在需要它用于 Excel 2010,现在当我将“格式”添加到NumberFormatLocal (range.NumberFormatLocal = format;) 当我使用 range.NumberFormat = format;

时也会出现同样的异常

异常(exception):

Error message: System.Runtime.InteropServices.COMException (0x80020005): Type Conflict. (Exception of HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)) At System.RuntimeType.ForwardCallToInvokeMember (String memberName, BindingFlags flags, ObjectTarget, Int32 [] aWrapperTypes, MessageData & msgData)

功能:

if (chkWithValues.Checked && results.Item3.Any())
            {
                var rows = results.Item3.Count;
                var cols = results.Item3.Max(x => x.Count);
                object[,] values = new object[rows, cols];
                object[,] format = new object[rows, cols];

                //All returned items are inserted into the Excel file
                //Item2 contains the database types, Item3 the Values
                // pgMain shows the progress for the selected tables
                for (int j = 0; j < results.Item3.Count(); j++)
                {
                    int tmpNbr = 1;
                    SetMessage($"{selectedTableItem.TableName} {j} von {results.Item3.Count}", LogHelper.NotificationType.Information);
                    foreach (string value in results.Item3[j])
                    {
                        values[j, tmpNbr - 1] = Converter.Convert(results.Item2[tmpNbr - 1], value).ToString().Replace("'", "");
                        format[j, tmpNbr - 1] = ExcelColumnTypes.ConvertToExcelTypes(results.Item2[tmpNbr - 1]);
                        tmpNbr++;
                    }
                    pgMain.Maximum = results.Item3.Count();
                    pgMain.PerformStep();
                }
                Excel.Range range = xlWorksheet.Range["A3", GetExcelColumnName(cols) + (rows + 2)];
                SetMessage($"{results.Item3.Count * results.Item1.Count} Zellen werden formatiert....", LogHelper.NotificationType.Information);

                range.NumberFormatLocal = format;
                range.Value = values;
            }

我的 Excel 类型:

public const string INT = "0";

    public const string TEXT = "@";
    public const string GENERAL = "General";
    public const string STANDARD = "Standard";

    public const string Date1 = "m/d/yyyy";
    public const string DATE2 = "TT.MM.JJJJ";
    public const string DATE3 = "T.M.JJ h:mm;@";
    public const string DATETIME = "d/m/yy h:mm;@";

    public const string DOUBLECO1 = "#.##0,00";
    public const string DOUBLECO2 = "0,00";

    public const string DOUBLEPO1 = "#0,##0.00";
    public const string DOUBLEPO2 = "0.00";

    public const string CUSTOM = "#,##000";

    public const string CURRENCYEU1 = "#,##0,00 _€";
    public const string CURRENCYEU2 = "#,##0 _€";
    public const string CURRENCYEU3 = "#,##0,00 €";

    public const string CURRENCYDO1 = "#,##0.00 _$";
    public const string CURRENCYDO2 = "#,##0 _$";
    public const string CURRENCYDO3 = "#,##0.00 $";

    public const string PERCENTAGE1 = "0.00%";
    public const string PERCENTAGE2 = "0.0%";
    public const string PERCENTAGE3 = "0%";

更新:

我已经尝试使用 public const string TEXT = "@"; 作为唯一的格式,但出现了同样的错误

更新 2:

只有当表格有很多条目时才会发生错误。例如,当我使用一个包含 1000 个条目的表时,没问题并且一切正常,如果我使用一个包含 200.000 个条目的表,则会发生错误

更新 3:

[ NumberFormat and the value for one cell[1]

我试过只使用标准格式进行测试,出现如下错误:

Error message: System.OutOfMemoryException: Insufficient memory available to continue the program.

enter image description here

最佳答案

是否可能是您向 Excel 传递的内容被解释为对于 Excel 2010 中的整数数据类型而言太大的整数? 例如行号?通过加载 32,760 行然后加载 32,770 行来测试它,因为整数在 XL 2010 中最多只能达到 32,767。值得一试 :o)

关于c# - excel 2010 类型冲突 NumberFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40023044/

相关文章:

c# - 甚至找不到在 rowdatabound GridView 上添加的复选框控件

arrays - 如何创建一个excel列内容的数组

vba - 使用 VBA 代码复制和粘贴数据

c# - 无法加密 Azure CloudFile。能够加密 Azure Blob

c# - 执行 ExecuteSqlCommand 后检索插入的 ID?

输入数据时 Excel 卡住

excel - 在 Excel VBA 脚本中使用单选按钮

excel - 通过 IP 地址到工作簿上的工作表的路径?

excel - 同一行中匹配值的计数

c# - 从 Windows 窗体异步执行存储过程然后断开连接?