Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted

1 solution

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:

C#
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
                }


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:

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


Note the lack of Type.Missing arguments: they are optional. OutputPath was defined by the user using sfd.
 
Share this answer
 
Comments
krishpraj123 15-Apr-14 22:52pm    
you answer is half to my point.
I dont want to save excel file ,I want to import(open) it at runtime(that file should be open from anywhere it is stored).

I have menustrip option on form (IMPORT FROM EXCEL)
when customer opens it Some Labels on Form Change its text which is in Excel(column).
and there can be upto 100 Labels Whose text changes.
I have done openfiledialog excel coding that is not a problem

So my question was 1}while opening will it open any version of excel
and 2} instead of using interop i should use OLEDB connection to open,
Will there be any problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900