首页 > 开发 > 综合 > 正文

用C#快速往Excel写数据

2024-07-21 02:19:14
字体:
来源:转载
供稿:网友
本示例是用于将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[1];
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[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;


  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表