NCF参数化建筑论坛

标题: 轻松读写excel [打印本页]

作者: panhao1    时间: 2011-1-6 03:59
标题: 轻松读写excel
轻松时因为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都木有问题的
作者: panhao1    时间: 2011-1-6 04:00
本帖最后由 panhao1 于 2011-1-6 04:15 编辑 先讲怎么写数据 objExcel.Cells(行, 列).clear objExcel.Cells(行, 列).Value = String 就这么简单 当然还有很多属性 大家自己去发挥吧
作者: panhao1    时间: 2011-1-6 04:00
本帖最后由 panhao1 于 2011-1-6 04:05 编辑 再讲怎么读数据 基本代码 objExcel.Cells(行数,列数).Value 返回值是String啦 [attach]13567[/attach] [attach]13568[/attach] 输入的公式函数是要通过其他代码实现的 这里只是返回值而已 一般情况下返回value就够了 想找别的值可以去CSDN上查下 我记不住了
作者: panhao1    时间: 2011-1-6 04:29
本帖最后由 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.
作者: panhao1    时间: 2011-1-6 04:33
本帖最后由 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()); } }
作者: panhao1    时间: 2011-1-6 04:39
怎么 还是不懂 好吧 再举个例子 具体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
作者: panhao1    时间: 2011-1-6 04:45
读完这些还是不懂的 那只能用reader去读2进制文件了 每次都要保存一下 悲催啊~ excel读取可以新开一个线程没秒读一次 省的每次去弄grasshopper
作者: wyx10022    时间: 2011-1-6 09:17
楼主辛苦了,早上4点多就起来发贴…………不会一宿没睡吧?
作者: musofan    时间: 2011-1-6 10:37
提示: 作者被禁止或删除 内容自动屏蔽
作者: wyx10022    时间: 2011-1-6 14:50
身体是参数化革命的本钱,二位版主悠着点
作者: JEREMY    时间: 2011-2-6 22:54
学习了 顶一个· ·
作者: Leimu    时间: 2011-2-6 23:50
支持技术贴。。。。
作者: zxl900113    时间: 2011-2-7 11:30
学习了 顶一个· ·
作者: flansrjf    时间: 2011-5-13 14:41
不知道能不能用,参考一下看看
作者: bxsqrym    时间: 2011-7-13 12:58
强啊,楼主。。。
作者: carcass    时间: 2011-7-13 19:28
..............................
作者: tank1862882    时间: 2011-7-13 19:47
表示完全看不明白
作者: 丞丞    时间: 2012-2-18 10:45
求照度,顶一个。。。
作者: 丞丞    时间: 2012-2-18 10:47
求照度,顶一个。。。
作者: ---------------    时间: 2012-4-26 12:28
科普知识。 lz幸苦
作者: SKY-A    时间: 2012-4-26 13:03
挺有用的,刚试过




欢迎光临 NCF参数化建筑论坛 (http://www.ncf-china.com/) Powered by Discuz! X3.2