在C#中调用Excel
2010-01-20 17:13
148 查看
1. 调用Excel的COM组件。
在项目中打开Add Reference对话框,选择COM栏,之后在COM列表中找到"Microsoft Excel 11.0 Object Library"(Office 2003),然后将其加入到项目的References中即可。Visual C#.NET会自动产生相应的.NET组件文件,以后即可正常使用。
2. 打开Excel表格
Excel.Application excel = new Excel.Application(); //引用Excel对象
Excel.Workbook book = excel.Application.Workbooks.Add(Missing.Value); //引用Excel工作簿
excel.Visible = bVisible; //使Excel可视
有时调用excel.Application.Workbooks.Add(Missing.Value)会遇到如下错误:
Exception:
Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
这是Excel自身的一个bug,当本地系统环境被设置成非英文的,而Excel是英文的时候,就会出现,需要临时设定英文环境,代码如下:
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
3. 往Excel表格中插入数据
Excel.Worksheet sheet = (Excel.Worksheet)book.Worksheets["Sheet1"]; // 选中当前新建Sheet(一般为Sheet1)
有两种插入方法
a. 逐格插入数据
sheet.Cells[iRow, iCol] = value; // 左上角第一格的坐标是[1, 1]
b. 按块插入数据
object[,] objVal = new object[Height, Length];
// 设置数据块
Excel.Range range = sheet.get_Range(sheet.Cells[iRow, iCol], sheet.Cells[iRow + Height, iCol + Length])
range.Value2 = objVal;
4. 清理内存和恢复环境
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excel) > 0) ;
range = null;
sheet = null;
book = null;
excel = null;
GC.Collect();
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
------------------------
特别是第二点。好有用!
在项目中打开Add Reference对话框,选择COM栏,之后在COM列表中找到"Microsoft Excel 11.0 Object Library"(Office 2003),然后将其加入到项目的References中即可。Visual C#.NET会自动产生相应的.NET组件文件,以后即可正常使用。
2. 打开Excel表格
Excel.Application excel = new Excel.Application(); //引用Excel对象
Excel.Workbook book = excel.Application.Workbooks.Add(Missing.Value); //引用Excel工作簿
excel.Visible = bVisible; //使Excel可视
有时调用excel.Application.Workbooks.Add(Missing.Value)会遇到如下错误:
Exception:
Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
这是Excel自身的一个bug,当本地系统环境被设置成非英文的,而Excel是英文的时候,就会出现,需要临时设定英文环境,代码如下:
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
3. 往Excel表格中插入数据
Excel.Worksheet sheet = (Excel.Worksheet)book.Worksheets["Sheet1"]; // 选中当前新建Sheet(一般为Sheet1)
有两种插入方法
a. 逐格插入数据
sheet.Cells[iRow, iCol] = value; // 左上角第一格的坐标是[1, 1]
b. 按块插入数据
object[,] objVal = new object[Height, Length];
// 设置数据块
Excel.Range range = sheet.get_Range(sheet.Cells[iRow, iCol], sheet.Cells[iRow + Height, iCol + Length])
range.Value2 = objVal;
4. 清理内存和恢复环境
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excel) > 0) ;
range = null;
sheet = null;
book = null;
excel = null;
GC.Collect();
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
------------------------
特别是第二点。好有用!
相关文章推荐
- C#调用Excel的宏
- 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- 编程实现C#调用Excel
- C#调用epplus 创建Excel
- C#中编程创建、调用Excel 宏
- .net杂记 (调用EXE 获取路径 target VB DIV C# Split 页面之间传递值 更改session时间 EXCEL操作 控制电子邮件Mail input记录值的窍门)
- C# 调用Excel报 error CS1969: 找不到编译动态表达式所需的一个或多个类型。是否缺少引用?
- c#调用excel中的vba宏
- C# 调用Excel
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- C#下面调用 Excel 另存代码
- C# 生成Excel 并保存 包含web和winform调用方法
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- C#调用NPOI自动创建Excel文档(一)
- C#调用NPOI创建Excel文档样式设置方法总结
- C#调用Excel VBA宏
- C#调用Excel报 error CS1969: 找不到编译动态表达式所需的一个或多个类型。是否缺少引用?
- VS2013 C#调用EXCEL 编译报错:找不到编译动态表达式所需的一个或多个类型。是否缺少引用?
- C# 调用Excel组件生成excel文件