asp.net - Need to get ArrayList() to Export to Excel -
i having trouble exporting data excel. application uses arraylist() house data. don't understand arraylist's. inside arraylist() mydataclass, val1, val2, etc...the array has 7 columns , can have anywhere 1 300 rows of data. when this, can't seem down val1 data, excel sheet in cell a1 saying, mydataclass. can please me this, thank in advance. on visual studio 2010 , office 2010.
protected sub btnexport_click_backup(sender object, e system.eventargs) handles btnexport.click dim itemlist new arraylist() itemlist = session("sessitemlist2") itemlist.reverse() dim dt new system.data.datatable() dt.columns.add("serlnmbr") dt.columns.add("itemnmbr") dt.columns.add("locncode") dt.columns.add("status") dt.columns.add("msl") dt.columns.add("invoicenumber") dt.columns.add("activationstatus")
' doing wrong...
integer = 0 itemlist.count - 1 dim dr datarow dr = dt.newrow dr.item(0) = itemlist(0).val1.tostring dt.rows.add(dr) next try dim oexcel interop.excel.application dim obook workbook dim osheet worksheet oexcel = createobject("excel.application") obook = oexcel.workbooks.add(type.missing) osheet = obook.worksheets(1) dim misvalue object = system.reflection.missing.value dim integer dim j integer dim dc system.data.datacolumn dim dr system.data.datarow dim colindex integer = 0 dim rowindex integer = 0 'export columns excel file each dc in dt.columns colindex = colindex + 1 osheet.cells(1, colindex) = dc.columnname next 'export rows excel file each dr in dt.rows rowindex = rowindex + 1 colindex = 0 each dc in dt.columns colindex = colindex + 1 osheet.cells(rowindex + 1, colindex) = dr(dc.columnname) next next 'set final path dim filename string = "c:\temp\exportedfile" + ".xls" dim finalpath = filename osheet.columns.autofit() 'save file in final path obook.saveas(finalpath, xlfileformat.xlworkbooknormal, type.missing, type.missing, type.missing, type.missing, xlsaveasaccessmode.xlexclusive, type.missing, type.missing, type.missing, type.missing, type.missing) 'release objects releaseobject(osheet) obook.close(false, type.missing, type.missing) releaseobject(obook) oexcel.quit() releaseobject(oexcel) 'some time office application not quit after automation: calling gc.collect method. gc.collect() ' open file user dim oxcel interop.excel.application dim owb workbook dim osht worksheet dim ocell range oxcel = new interop.excel.application oxcel.visible = true ' cpu errors out here becz office not registered, can go excel & remove registration msgbox press continue owb = oxcel.workbooks.open(finalpath) osht = owb.worksheets(1) ocell = osht.range("a1") catch ex exception ' messagebox.show(ex.message, "warning", messageboxbuttons.ok) ' need error end try end sub
' here small portion of mydataclass
public class mydataclass
public sub new(byval v1 string, byval v2 string, byval v3 string, byval v4 string, optional byval v5 string = "", optional byval v6 string = "", optional byval v7 string = "", optional byval v8 string = "") val1 = v1 val2 = v2 val3 = v3 val4 = v4 val5 = v5 val6 = v6 val7 = v7 val8 = v8 end sub public property val1() string return _val1 end set(byval value string) _val1 = value end set end property
etc...
public _val1 string public _val2 string public _val3 string public _val4 string public _val5 string public _val6 string public _val7 string public _val8 string
end class
an arraylist list of objects. there no .val property on it. there may object there val property, first have cast it.
you didn't show how make arraylist, or contains have guess.
if have class val1 property, have cast object type before can access val1
dr.item(0) = cast(itemlist(0),myclass).val1.tostring
since arraylist takes object, have cast them appropriate type first. or, if needs strings can call tostring on simple types integers.
fyi, if working excel in .net application understand need excel on server. won't have permissions access default , microsoft has recommended against doing on decade. there third-party .net components designed work various office file formats without requiring office installed. threading model of office apps wrong being used in web environment.
Comments
Post a Comment