c# - Print lists in a list to excel, randomly stops printing after 0.5-6 lists. Comexpection 0x800AC472 -
first of have have codes filling lists of strings, after puts lists in 1 big list. when want print lists in excel sheets, stops after pint half of first list, or stops after print 5 , half list, , says: exception hresult: 0x800ac472. there 30 tabs in excel doc, not problem. feel free rename title, ihad no idea how call problem.
this how print lists in excel:
for (int = 0; < listoflists.count; i++) { (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 method:
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); } }
i appriciate problem. in asvance!
here reflection code.
public static datatable classtodatatable<t>() 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 default col.allowdbnull = false; } col.datatype = datatype; result.columns.add(col); } return result; } public static datatable classlisttodatatable<t>(list<t> classlist) 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) t : class { type classtype = typeof(t); string classname = classtype.underlyingsystemtype.name; // checks table name matches name of class. // there not required, , may desirable disable check. // comment out or add boolean parameters disable 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; // ref-type, such class if (nullable.getunderlyingtype(input) != null) return true; // nullable return false; // must value-type }
Comments
Post a Comment