tags:

views:

56

answers:

2

Hi,

I had a problem with my program for opening an excel file through the use of PIA. here below is my sample code; any suggestions?

path = @"C:\\Test Template.xls";
wb = objExcel.Workbooks.Open(path, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

after i execute this code the program returns an error meesage "Cannot access Test Template.xls". Can someone explain the cause of this error, i'm confused..

+7  A: 

I'm pretty sure the problem is here:

path = @"C:\\Test Template.xls";

You should EITHER use "@" to mean the string is literal

path = @"C:\Test Template.xls";

OR escape backslashes as "\\".

path = "C:\\Test Template.xls";

Not both.

Foole
+1 I didn't even notice the double-backslash! lol
Will Marcouiller
i did ur suggestion but it gives the same result,
newbie programmer
A: 

your syntax is wrong.........

@"C:\Test\templat.xls"....check this too

using System;

using System.IO;

using System.Reflection;

using NUnit.Framework;

using ExcelTools = Ms.Office;

using Excel = Microsoft.Office.Interop.Excel;

namespace Tests {

    public class ExcelSingle
    {
            public void ProcessWorkbook()
            {
                    string file = @"C:\Users\Chris\Desktop\TestSheet.xls";
                    Console.WriteLine(file);

                    Excel.Application excel = null;
                    Excel.Workbook wkb = null;

                    try
                    {
                            excel = new Excel.Application();

                            wkb = ExcelTools.OfficeUtil.OpenBook(excel, file);

                            Excel.Worksheet sheet = wkb.Sheets["Data"] as Excel.Worksheet;

                            Excel.Range range = null;

                            if (sheet != null)
                                    range = sheet.get_Range("A1", Missing.Value);

                            string A1 = String.Empty;

                            if( range != null )
                                    A1 = range.Text.ToString();

                            Console.WriteLine("A1 value: {0}", A1);

                    }
                    catch(Exception ex)
                    {
                            //if you need to handle stuff
                            Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                            if (wkb != null)
                                    ExcelTools.OfficeUtil.ReleaseRCM(wkb);

                            if (excel != null)
                                    ExcelTools.OfficeUtil.ReleaseRCM(excel);
                    }
            }
    }
peril brain