本示例是用于将ListView中的内容倒入到Excel
与常用的逐单元格写不同的是,本例子采用数据写入到range的方法。该方法效率明显较高
Excel.Application app = new Excel.ApplicationClass();
if( app == null)
{
MessageBox.Show("Excel无法启动");
return;
}
app.Visible = true;
Excel.Workbooks wbs = app.Workbooks;
Excel.Workbook wb = wbs.Add(Missing.Value);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets;
Excel.Range r = ws.get_Range("A1","H1");
object objHeader = {"标题1","标题2","标题3",
"标题4","标题5","标题6",
"标题7","标题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.Text;
objData[lvi.Index,2] = lvi.SubItems.Text;
objData[lvi.Index,3] = lvi.SubItems.Text;
objData[lvi.Index,4] = lvi.SubItems.Text;
objData[lvi.Index,5] = lvi.SubItems.Text;
objData[lvi.Index,6] = lvi.SubItems.Text;
objData[lvi.Index,7] = lvi.SubItems.Text;
}
r = r.get_Resize(lv.Items.Count,8);
r.Value = objData;
r.EntireColumn.AutoFit();
}
app = null;