.net开发Excel
欢迎阅读《C#操作Excel的前世今生》文章。
由于很少写技术文章,就模仿了msdn帮助文档的手法。觉得思路清晰,给人予帮助。于是效仿之。(喂,说你呢,不要笑!)
发布日期:5:40 2009-09-01|更新日期:5:40 2009-09-01
行者无疆&lxm
试用于xpsp3 Excel2003 .net2008 .net framework 2.0环境
第一次使用,第一次写,写的不对,欢迎指正!
![](https://images.cnblogs.com/cnblogs_com/u_xiaomo/205002/o_title_08.gif)
![](https://images.cnblogs.com/cnblogs_com/u_xiaomo/205002/o_title_10.gif)
Office 应用程序或组件以及主互操作程序集名称 的介绍
第一个c#操作Excel程序(附下载文档)
最终程序:分享一个批量上传Excel数据,保存到数据库,或者前台显示在Web控件的Demo!
后续更新:(我会更新该demo,做到完美的和excel无缝集成,希望大家不吝赐教!)
Step one
Question?
- 微软的office方面的功能需要注册office里的组件dll 为什么呢?
- C#将xml格式的数据加载到Excel中编辑?
- C# 读取Excel数据,在表示控件中呈现
Answer!
- 平台调用是一种服务,它使托管代码可以调用在动态链接库 (DLL)(如 Microsoft Win32 API 中的那些 DLL)中实现的非托管函数。此服务将查找并调用导出的函数,然后根据需要跨越互用边界封送其参数(整数、字符串、数组、结构等)。添加引用:Microsoft.Office.Interop.Excel;另外help:这里贴出msdn的详细操作步骤:XP请点击 Server 2003请点击
- 遇到点问题,没研究明白。迟点发布代码
- 根据表头编号获取列、 提供一个 foreach 循环枚举数--为 方法名.GetEnumerator 方法提供循环枚举对象
![](https://images.cnblogs.com/cnblogs_com/u_xiaomo/205002/o_title_08.gif)
![](https://images.cnblogs.com/cnblogs_com/u_xiaomo/205002/o_title_10.gif)
用C#调用com组件创建一个Excel工作区(代码打开Excel.exe)
protected void Page_Load(object sender, EventArgs e) { Create_Excel(); } public void Create_Excel() { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { Response.Write("EXCEL无法启动。检查你的办公室安装和项目引用是正确的."); return; } xlApp.Visible = true; Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet ws = (Worksheet)wb.Worksheets[1]; if (ws == null) { Response.Write("表不能创建。检查你的办公室安装和项目引用是正确的."); } // Select the Excel cells, in the range c1 to c7 in the worksheet. Range aRange = ws.get_Range("C1", "C7"); if (aRange == null) { Response.Write("无法得到一个范围。检查以确保您有办公室DLL的正确版本。"); } // Fill the cells in the C1 to C7 range of the worksheet with the number 6. Object[] args = new Object[1]; args[0] = 6; aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args); // Change the cells in the C1 to C7 range of the worksheet to the number 8. aRange.Value2 = 8; }
获取自定义的Excel格式的数据
![](https://images.cnblogs.com/cnblogs_com/u_xiaomo/205002/o_excel_01.jpg)
![download](https://images.cnblogs.com/cnblogs_com/u_xiaomo/download_btn.png)