Reputation: 3
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..
Upvotes: 0
Views: 2918
Reputation: 8207
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);
}
}
}
Upvotes: 0
Reputation: 4850
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.
Upvotes: 7