300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > c#操作excel 使用excel自带类库Microsoft.Office.Interop.Excel

c#操作excel 使用excel自带类库Microsoft.Office.Interop.Excel

时间:2022-01-01 02:41:00

相关推荐

c#操作excel  使用excel自带类库Microsoft.Office.Interop.Excel

使用条件:安装excel,在安装位置找到库Microsoft.Office.Interop.Excel.dll添加引用

using Excel=Microsoft.Office.Interop.Excel;1//将数据写入已存在Excel2 public static void writeExcel(string result, string filepath)3 {4 //1.创建Applicaton对象5 Microsoft.Office.Interop.Excel.Application xApp = new 6 7 Microsoft.Office.Interop.Excel.Application();8 9 //2.得到workbook对象,打开已有的文件10 Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks.Open(filepath,11Missing.Value, Missing.Value, Missing.Value, Missing.Value,12Missing.Value, Missing.Value, Missing.Value, Missing.Value,13Missing.Value, Missing.Value, Missing.Value, Missing.Value);14 15 //3.指定要操作的Sheet16 Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];17 18 //在第一列的左边插入一列 1:第一列19 //xlShiftToRight:向右移动单元格 xlShiftDown:向下移动单元格20 //Range Columns = (Range)xSheet.Columns[1, System.Type.Missing];21 //Columns.Insert(XlInsertShiftDirection.xlShiftToRight, Type.Missing);22 23 //4.向相应对位置写入相应的数据24 xSheet.Cells[Column(列)][Row(行)] = result;25 26 //5.保存保存WorkBook27 xBook.Save();28 //6.从内存中关闭Excel对象29 30 xSheet = null;31 xBook.Close();32 xBook = null;33 //关闭EXCEL的提示框34 xApp.DisplayAlerts = false;35 //Excel从内存中退出36 xApp.Quit();37 xApp = null;38 }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。