将列表中的列表打印到 excel,在 0.5-6 个列表后随机停止打印。通信预期 0x800AC472
Print lists in a list to excel, randomly stops printing after 0.5-6 lists. Comexpection 0x800AC472
首先,我有一些代码可以填充一些字符串列表,然后将所有列表放入一个大列表中。现在,当我想在 excel 工作表中打印这些列表时,它会在第一个列表的 pint 一半后停止,或者在打印 5 个半列表后停止,并显示:HRESULT 异常: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);
}
}
我会感谢任何帮助解决这个问题。提前致谢!
这是我的反射代码。
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
}
首先,我有一些代码可以填充一些字符串列表,然后将所有列表放入一个大列表中。现在,当我想在 excel 工作表中打印这些列表时,它会在第一个列表的 pint 一半后停止,或者在打印 5 个半列表后停止,并显示:HRESULT 异常: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);
}
}
我会感谢任何帮助解决这个问题。提前致谢!
这是我的反射代码。
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
}