adding specific database data to an excel file using VB.NET -
i want add data excel file, have established connection database , pulled information need stored procedure using ssms.
i have added columns based on information need, of rows static data here code:
private sub exceloutput_click(byval sender system.object, byval e system.eventargs) handles exceloutput.click dim xlapp excel.application dim xlworkbook excel.workbook dim xlworksheet excel.worksheet dim xlrange excel.range dim misvalue object = system.reflection.missing.value dim rnum random = new random xlapp = new excel.application xlworkbook = xlapp.workbooks.add(misvalue) xlworksheet = xlworkbook.sheets("sheet1") xlrange = xlworksheet.usedrange xlworksheet .range("a1").value = "col1" .range("b1").value = "col2" .range("c1").value = "col3" .range("d1").value = "col4" .range("e1").value = "col5" .range("f1").value = "col6" .range("g1").value = "col7" .range("h1").value = "col8" .range("i1").value = "col9" .range("j1").value = "col10" .range("k1").value = "col11" .range("l1").value = "col12" xlworksheet.range("e2:e10").value = datetime.now.tostring("dd-mmm-yy") xlworksheet.range("f2:f10").value = "upload" xlworksheet.range("g2:g10").value = rnum.next() xlworksheet.range("i2:i10").value = "included" end
what want know how able loop through database table , populate each cell in excel file based on information in each column, example col1 have account customer id's pulled stored procedure. want use loop put excel file , include information.
i new appreciate guidance
let's have pulled second query datatable. populate excel file order: a1 being 1660, b1 being ht5-088, a2 being 5882, etc....
here's code:
dim pos string = "" integer = 0 datatable.rows.count - 1 'rows j integer = 0 datatable.columns.count - 1 'columns pos = chr(asc("a") + (j)) & 'calculate column according j 'after this, pos have right excel cell. xlworksheet.range(pos).value = datatable.rows(i)(j) next next
the gist here is, can use correct string represent excel range...
Comments
Post a Comment