Write data with C # quick to Excel

xiaoxiao2021-03-06  68

This example is used to pour the content in the listview into Excel

Unlike commonly used one-way folds, this example uses data to write to the RANGE. This method is more efficient

Excel.Application App = New Excel.ApplicationClass (); if (app == null) {MessageBox.show ("Excel Unable to start"); return;} app.visible = true; Excel.Workbooks WBS = App.workBooks; Excel .Workbook wb = wbs.add (miss "; Excel.worksheet ws = (Excel.worksheet) WB.Worksheets [1]; Excel.Range R = ws.get_range (" A1 "," H1 "); Object [ ] objHeader = {"Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7", "Title 8"}; R.Value = Objheader ; If (lv.items.count> 0) {r = ws.get_range ("a2", missing.value); object [,] objdata = new object [this.lv.Items.count, 8]; Foreach (listview) Lvi in ​​lv.Items) {objData [lvi.index, 0] = lvi.text; objdata [lvi.index, 1] = lvi.subitems [1] .text; objdata [lvi.index, 2] = lvi.subitems [2] .Text; Objdata [lvi.index, 3] = lvi.subitems [3] .text; objdata [lvi.index, 4] = lvi.subitems [4] .text; Objdata [lvi.index, 5] = lvi.subitems [5] .Text; Objdata [lvi.index, 6] = lvi.subitems [6] .text; Objdata [lvi.index, 7] = lvi.subitems [7]. TEXT;} r = r.Get_resize (lv.items.count, 8); r.value = objdata; r.entirecolumn.autofit ();} app = null;

转载请注明原文地址:https://www.9cbs.com/read-120638.html

New Post(0)