从Excel文件读取值并在运行时更改标签文本(Interop或oledb?) [英] Reading values from Excel file and changing Label text at runtime (Interop or oledb?)

查看:68
本文介绍了从Excel文件读取值并在运行时更改标签文本(Interop或oledb?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上显示了多达100个警报。如果客户想要更改,我想在运行时更改标签文本

警报。



所以我现在使用了Excel对象库12.0从excel文件中读取值()。

并更改报警文本标签



客户可以修改报警数量,其文本在excel文件中,保存他想要的地方

然后导入它以在运行时更改标签文本。



所以我的问题是好的使用dll(excel 12.0对象库引用)或者我应该OLEDB即通过数据库导入文件(值)。



因为Excel中有很多TYPE.MISSING因为我正在使用的MS Excel版本和客户可能有不同的版本,有没有机会崩溃文件。



谢谢你提前。

I have upto hundred alarms displayed on form. I want to change the Label text of
Alarm at runtime if customers want to change.

so I have right now used Excel object library 12.0 to read values() from excel file.
and change the text of alarms Labels

customer can modify number of alarms, its text in excel file ,save where he wants
and then import it to change label text at runtime.

So my question is it good to use dll(excel 12.0 object library reference) or I should OLEDB i.e by database to import the file(values).

because there is lots of TYPE.MISSING in Excel and are there any chances of crashing the file because of version of MS Excel i am using and the customer may have different version.

Thankyou in Advance.

推荐答案

我个人会使用互操作,因为如果你正在创建Excel文件那么en d用户可能需要使用它们并且将安装某个版本的Excel。即使她/他不需要查看它们,他们也可能拥有Excel。 :)



您不需要知道用户安装了哪个版本,除了保存时的文件扩展名。这是我用来在SaveFileDialog实例上设置变量的一些代码的小片段,允许用户指定保存位置:



Personally I would use interop, since if you are creating Excel files the end user probably needs to use them and will have some version of Excel installed. Even if she / he doesn't need to view them they will probably have Excel. :)

You don't need to know which version the user has installed, except maybe for the file extension on save. Here's a little snippet from some code I used to set the variables on a SaveFileDialog instance which allowed the user to specify a save location:

Microsoft.Office.Interop.Excel.Application excelApp =
                    new Microsoft.Office.Interop.Excel.Application();
                string strVersion = excelApp.Version;
                string[] strsVersion = strVersion.Split('.');
                int version = Convert.ToInt32(strsVersion[0]);

                if (version >=12)
                {
                    sfd.Filter = "Excel Workbook|*.xlsx";
                    sfd.DefaultExt = "xlsx";
                }
                else if (version > 0 && version < 12)
                {
                    sfd.Filter = "Excel Workbook|*.xls";
                    sfd.DefaultExt = "xls";
                }
                else
                {
                    // No version installed
                }





在这里你可以看到SaveFileDialog (sfd)被设置为向sdf.FileName添加正确的路径+扩展名。这稍后用于以这种方式保存电子表格:





Here you can see the SaveFileDialog (sfd) being set up to add the proper path + extension to sdf.FileName. This is later used to save the spreadsheet in this way:

eWorkbook.SaveAs(OutputPath,
    Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault);
eWorkbook.Close(true);





注意缺少Type.Missing参数:它们是可选的。 OutputPath由用户使用sfd。



Note the lack of Type.Missing arguments: they are optional. OutputPath was defined by the user using sfd.


这篇关于从Excel文件读取值并在运行时更改标签文本(Interop或oledb?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆