当前位置: 移动技术网 > IT编程>开发语言>.net > .NET操作Excel实例分享

.NET操作Excel实例分享

2017年12月12日  | 移动技术网IT编程  | 我要评论

中国二手网,密度计的使用,柳絮的图片

1. 读取

读取好像有几种方式,通过ado.net, 通过microsoft.interop.excel支持类库用调用com读取,还有通过zip解压最终读取dom(这个貌似蛮复杂)
这里我用的ado.net只介绍这一个。

复制代码 代码如下:

public datatable exceltodatatable(string strexcelpath, string strsheetname)
{
  string strconn =
    "provider=microsoft.jet.oledb.4.0;" + "data source=" + strexcelpath + ";" + "extended properties=excel 5.0;";
  string strexcel = string.format("select * from [{0}$]", strsheetname);
  dataset ds = new dataset();
  oledbdataadapter adapter = new oledbdataadapter(strexcel, strconn);
  adapter.fill(ds, strsheetname);
  conn.close();
  return ds.tables[strsheetname];
}

参数: strexcelpath excel文件的路径,strsheetname 要读取表的名称
这里贴个读sheetname的代码,这个是调用microsoft.interop.excel来读取
    excel.workbook theworkbook = excelobj.workbooks.open("excel文件路径", 0, true, 5, "", "", true, excel.xlplatform.xlwindows, "\t", false, false, 0, true);
    excel.sheets sheets = theworkbook.worksheets;
    excel.worksheet worksheet = (excel.worksheet)sheets.get_item(1);
    execname = worksheet.name;
    theworkbook.close(null, null, null);//记得要关闭,否则程序关闭,excel的进程还在
2.创建新的excel文件

复制代码 代码如下:

excel.applicationclass myexcel = new excel.applicationclass();      //实例一个excel
excel._workbook xbk;       //工作薄  相当于一个excel文件                
excel._worksheet xst;      //工作sheet    一个文件里的表
xbk = myexcel.workbooks.add(true);      //允许添加 sheet
object missing = system.reflection.missing.value;//空值
myexcel.sheets.add(missing, missing, 1, excel.xlsheettype.xlworksheet);//添加sheet
xst = (excel._worksheet)xbk.activesheet;//得到默认sheet
xst.name = "新的表";      //这里设置表名
xbk.saveas( “保存路径” , missing, missing,
    missing, missing, missing, excel.xlsaveasaccessmode.xlshared,
    missing, missing, missing, missing);      //保存文件
myexcel.quit(); //同样要记得关闭

3.添加内容
这里只说一般数据的添加,图表的就不介绍了, 在excel的操作里一般是以表格和范围为单位,行或列是从1开始而不是从0开始
先介绍一下格式设置,如下
如设置第一行字体为红色:((excel.range)xst.rows[1, type.missing]).font.color = 0xff0000;      //xst 是上边代码的变量名,颜色值是用16进制rgb
设置第二列为百分数的格式 ((excel.range)xst.columns[2, type.missing]).numberformat = "0.00%";
      备常用格式:文本:@ 日期:yyyy/mm/dd  数字:#,##0.00  货币:¥#,##0.00  百分比:0.00%
上边两种为行选择和列选择,如果要选择第1行第2列到第1行第5列就用get_range();
      xst.get_range(xst.cells[1,2],xst.cells[1,5])
如果需要其它格式,如单元格背景,边框,字体样式 ,查下文档。不过大部分从点出来的方法名就能知道了
再就是添加内容,直接写点代码参考。

复制代码 代码如下:

int rowidx = 2; //从第2行列始
//这里dt 是datatable数据源
foreach( datarow dr in dt.rows )
{
 int j = 1;      //从第1列开始
 myexcel.cells[rowidx, j++] = dr["dt列名"].tostring(); 
 myexcel.cells[rowidx, j++] = dr["dt列名"].tostring(); 
 myexcel.cells[rowidx, j++] = dr["dt列名"].tostring(); 
 myexcel.cells[rowidx, j++] = dr["dt列名"].tostring(); 
 myexcel.cells[rowidx, j++] = dr["dt列名"].tostring(); 
 myexcel.cells[rowidx, j++] = dr["dt列名"].tostring();
 //用公式的情况,显示a+b+c+d的结果
 myexcel.cells[rowidx, j++] = string.format("=sum(a{0}:d{0})",rowidx);
 rowidx++;
}

写完了save一下 xbk.save(),还是记得要关闭。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网