NCF参数化建筑论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 34838|回复: 20
打印 上一主题 下一主题

[研发成果] 轻松读写excel

[复制链接]
跳转到指定楼层
1m
发表于 2011-1-6 03:59:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
轻松时因为excel支持。net远程操作 大家可以参考.net开发excel plugins的相关网页 这里先写下怎么读取单元格数据 Dim oldCI As System.Globalization.CultureInfo =system.Threading.Thread.CurrentThread.CurrentCulture 获取正在执行的线程 这里表示excel需要正运行 一般出错也就是在这里啦 解决方法可以参看线程错误的例子 是在搞不懂就去找计算机系的同学吧 System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US") 配置一些属性(语言),当然配置CN gh也不会识别的 Dim objExcel As Object objExcel = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") 获取类后就可以开始操作excel了 一般出错状态 错误消息 1 运行时错误 '429': ActiveX 组件不能创建对象 错误消息 2 错误: 0x800401e3"操作不可用" 原因(机器翻译的 凑合下吧 大多数人不是计算机系的 e文看不太懂) 虽然 Office 应用程序正在运行,它可能不被注册在运行对象表 (ROT)。可以将的 Office 应用程序必须注册它之前 ROT 中的运行实例附加到使用 GetObject (Visual Basic) 或 GetActiveObject (Visual c + +)。 将启动 Office 应用程序时它不会立即注册其正在运行的对象。此优化应用程序的启动过程。而不是注册的启动,Office 时其运行对象的应用程序寄存器 ROT 中一次,则会失去焦点。 因此,如果试图使用 GetObjectGetActiveObject 将附加到某个 Office 应用程序的运行实例应用程序已失去焦点之前您可能会收到上述错误之一。 解决方案: 找个好点的PJ版本装下,不要装一些木有PJ好的版本 2003和2007,2010都木有问题的

评分

参与人数 1强度 +3 照度 +30 收起 理由
musofan + 3 + 30 感谢分享~

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
2m
 楼主| 发表于 2011-1-6 04:00:12 | 只看该作者
本帖最后由 panhao1 于 2011-1-6 04:15 编辑 先讲怎么写数据 objExcel.Cells(行, 列).clear objExcel.Cells(行, 列).Value = String 就这么简单 当然还有很多属性 大家自己去发挥吧
3m
 楼主| 发表于 2011-1-6 04:00:23 | 只看该作者
本帖最后由 panhao1 于 2011-1-6 04:05 编辑 再讲怎么读数据 基本代码 objExcel.Cells(行数,列数).Value 返回值是String啦
4m
 楼主| 发表于 2011-1-6 04:29:30 | 只看该作者
本帖最后由 panhao1 于 2011-1-6 04:30 编辑 那么 还有其他方法链接上excel么 ? 官网上说有9个 PIA Interop Using the Office PIAs is the most RAD approach, with the greatest level of design-time and compile-time support. This has to be the preferred approach for almost all scenarios. A variation of this is the use of embedded PIA types using ComImport, as described here and here. Process.Start (百试不爽的方法) The simplest fire-and-forget approach, useful if you want to launch any executable but don’t need to interact with the app’s OM afterwards. Activator.CreateInstance Internally, this uses reflection to find and execute an appropriate constructor for the specified object. This can be slow, but useful if you intend to continue using reflection to work with the app – which you might do if you want to eliminate use of the PIAs altogether. Marshal.BindToMoniker Internally, this p/invokes to the Win32 BindToMoniker API. Useful if you have a narrow interest in the app’s exposed object that deals with a particular file type. In other words, if you want to work with a particular doc/workbook/presentation/etc using only a limited subset of the OM. Marshal.GetActiveObject Internally, p/invokes to the Win32 GetActiveObject. This will throw an exception if the object’s server is not already running, as it looks up the target ProgID in the ROT. One of the classic uses of this API is to determine whether or not the target app is already running. VisualBasic.CreateObject A compatibility API, which internally calls Activator.CreateInstance. VisualBasic.GetObject A compatibility API, which internally calls either Activator.CreateInstance or Marshal.BindToMoniker. In other words, it will connect to an already-running instance of the app if it finds one, otherwise it will create a new instance. ActivateMicrosoftApp This is a method exposed from the Excel Application object (and only the Excel Application object), used for activating or starting other Office apps (specifically, Access, FoxPro, Outlook, PowerPoint, Project, or Word). This approach does not give you access to the target app’s OM, so its effect is very similar to Process.Start. AccessibleObjectFromWindow Given the HWND for the target app (which you can find using FindWindowEx), this gets you access to the app’s OM. This is useful if your starting point is an HWND, or if you’re specifically focused on the app’s IAccessible implementation, and only minimally interested in the rest of the OM.
5m
 楼主| 发表于 2011-1-6 04:33:19 | 只看该作者
本帖最后由 panhao1 于 2011-1-6 04:36 编辑 同理开ppt也是一样 internal void GetActiveObject() { ppt = null; pptx = null; try { ppt = (PowerPoint.Application) Marshal.GetActiveObject("PowerPoint.Application"); ppt.Visible = Office.MsoTriState.msoTrue; pptx = ppt.Presentations.Open(pptxFile, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue); } catch (COMException cex) { Debug.WriteLine(cex.ToString()); } }
6m
 楼主| 发表于 2011-1-6 04:39:46 | 只看该作者
怎么 还是不懂 好吧 再举个例子 具体C#对Excel的操作: 1、如何获取到当前Excel、workbook、worksheet等: object o = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); Excel._Application app = o as Excel._Application; Excel.Workbook workBook = app.ActiveWorkbook;//得到当前活动的excel文档 Excel._Workbook wr = app.ActiveWorkbook; if (workBook == null) { MessageBox.Show("No workbook is currently defined"); } Excel.Worksheet xlsSheet; xlsSheet = (Excel.Worksheet)workBook.ActiveSheet;//得到当前Sheet m_iJ = xlsSheet.UsedRange.Rows.Count;//得到当前Sheet行数 2、各个属性值: newWorksheet = (Excel.Worksheet)workBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);//新增一个Sheet newWorksheet.Name = "导入MQC";//设置Sheet的名称 newWorksheet.get_Range("A1", Missing.Value).Value2 = "Path";//设置某个单元格的值 newWorksheet.Cells.Clear();//清除单元格里面的值 newWorksheet.Cells.Font.Size = "10";//设置单元格字体大小 来自:http://hi.baidu.com/nirvanan/blo ... cb8a123b293568.html
7m
 楼主| 发表于 2011-1-6 04:45:00 | 只看该作者
读完这些还是不懂的 那只能用reader去读2进制文件了 每次都要保存一下 悲催啊~ excel读取可以新开一个线程没秒读一次 省的每次去弄grasshopper
8m
发表于 2011-1-6 09:17:45 | 只看该作者
楼主辛苦了,早上4点多就起来发贴…………不会一宿没睡吧?
头像被屏蔽
9m
发表于 2011-1-6 10:37:17 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
10m
发表于 2011-1-6 14:50:20 | 只看该作者
身体是参数化革命的本钱,二位版主悠着点
11m
发表于 2011-2-6 22:54:02 | 只看该作者
学习了 顶一个· ·
12m
发表于 2011-2-6 23:50:05 | 只看该作者
支持技术贴。。。。
13m
发表于 2011-2-7 11:30:25 | 只看该作者
学习了 顶一个· ·
14m
发表于 2011-5-13 14:41:59 | 只看该作者
不知道能不能用,参考一下看看
15m
发表于 2011-7-13 12:58:14 | 只看该作者
强啊,楼主。。。
16m
发表于 2011-7-13 19:28:32 | 只看该作者
..............................
17m
发表于 2011-7-13 19:47:10 | 只看该作者
表示完全看不明白
18m
发表于 2012-2-18 10:45:46 | 只看该作者
求照度,顶一个。。。
19m
发表于 2012-2-18 10:47:25 | 只看该作者
求照度,顶一个。。。
20m
发表于 2012-4-26 12:28:15 | 只看该作者
科普知识。 lz幸苦

小黑屋|手机版|NCF参数化建筑论坛 ( 浙ICP备2020044100号-2 )    辽公网安备21021102000973号

GMT+8, 2024-5-7 13:13 , Processed in 0.067683 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表