This example is used to import the content in listview to excel.
Unlike cell-by-cell writing, this example uses the data Writing Method to range. This method is more efficient.
Excel. Application APP = new excel. applicationclass ();
If (APP = NULL)
{
MessageBox. Show ("Excel cannot be started ");
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 = objheader;
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;