Write data with C # quick to Excel

xiaoxiao2021-03-06  39

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 can't start");

Return;

}

app.visible = true;

Excel.Workbooks WBS = app.workbooks;

Excel.Workbook WB = WBS.Add (missing.value);

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 = Objhead;

IF (lv.Items.count> 0)

{

R = ws.get_range ("a2", missing.value);

Object [,] objdata = new object [this.lv.Items.count, 8];

Foreach (listviewItem 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-116310.html

New Post(0)