在列表中打印列表以优化,0.5-6列表后随机停止打印。 Comexpection 0x800AC472

首先,我已经有一些代码填充了一些string列表,然后将所有列表放在一个大列表中。 现在,当我想在Excel表单中打印这些列表时,它在第一个列表的一半之后停止,或者在打印五个半列表之后停止,并且它说:从HRESULTexception:0x800AC472。 excel文档中已经有30个选项卡,所以这不是问题。 随意重命名这个标题,我不知道如何调用这个问题。

这是我如何在Excel中打印我的列表:

for (int i = 0; i < ListofLists.Count; i++) { for (int j = 1; j <= ListofLists[i].Count; j++) { excelDataHandler.excel_setValue("A" + j, ListofLists[i][j-1], "", (i+1)); //A = cell, data of list, color of cell, sheetnumber } } 

excel_setValue方法:

 public void excel_setValue(string cellname, string value, string color, int workSheet) { ((Microsoft.Office.Interop.Excel._Worksheet)newWorkbook_First.Sheets[workSheet]).get_Range(cellname).set_Value(Type.Missing, value); if (color == "red") { newSheets.get_Range(cellname).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); } } 

我会appriciate任何帮助这个问题。 非常感谢!

这是我的reflection代码。

  public static DataTable ClassToDataTable<T>() where T : class { Type classType = typeof(T); List<PropertyInfo> propertyList = classType.GetProperties().ToList(); if (propertyList.Count < 1) { return new DataTable(); } string className = classType.UnderlyingSystemType.Name; DataTable result = new DataTable(className); foreach (PropertyInfo property in propertyList) { DataColumn col = new DataColumn(); col.ColumnName = property.Name; Type dataType = property.PropertyType; if (IsNullable(dataType)) { if (dataType.IsGenericType) { dataType = dataType.GenericTypeArguments.FirstOrDefault(); } } else { // True by default col.AllowDBNull = false; } col.DataType = dataType; result.Columns.Add(col); } return result; } public static DataTable ClassListToDataTable<T>(List<T> ClassList) where T : class { DataTable result = ClassToDataTable<T>(); if (result.Columns.Count < 1) { return new DataTable(); } if (ClassList.Count < 1) { return result; } foreach (T item in ClassList) { ClassToDataRow(ref result, item); } return result; } public static void ClassToDataRow<T>(ref DataTable Table, T Data) where T : class { Type classType = typeof(T); string className = classType.UnderlyingSystemType.Name; // Checks that the table name matches the name of the class. // There is not required, and it may be desirable to disable this check. // Comment this out or add a boolean to the parameters to disable this check. if (!Table.TableName.Equals(className)) { return; } DataRow row = Table.NewRow(); List<PropertyInfo> propertyList = classType.GetProperties().ToList(); foreach (PropertyInfo prop in propertyList) { if (Table.Columns.Contains(prop.Name)) { if (Table.Columns[prop.Name] != null) { row[prop.Name] = prop.GetValue(Data, null); } } } Table.Rows.Add(row); } public static bool IsNullable(Type Input) { if (!Input.IsValueType) return true; // Is a ref-type, such as a class if (Nullable.GetUnderlyingType(Input) != null) return true; // Nullable return false; // Must be a value-type }