C# to Open Existing Excel Workbook
How would I open an existing Excel Workbook using C#? I have tried several guides I have seen on the internet, but none of them work succesfully for me :(
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
umer aliPosted Nov 27, 2014, 2:12 AM
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices;
//add these assembly refrences ;
try
{
excel.Application ex = new excel.Application();
ex.DisplayAlerts = true;
ex.ShowStartupDialog = true;
ex.Visible = true;
excel.Workbook newwork = ex.Workbooks.Add();// create a new excel file OR
string workbookPath = "Path.xls";
excel.Workbook excelWorkbook = ex.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
excel.Sheets excelSheets = excelWorkbook.Worksheets;
excel.Worksheet excelWorksheet = (excel.Worksheet)excelSheets.get_Item("Sheet1");
excel.Range excelCell = (excel.Range)excelWorksheet.get_Range("A1", "A10");
}
catch{}
you can open an existing exelfile or you can add new excel workbook;
please reply if this was helpful
CSharp NoobPosted Apr 30, 2013, 11:57 AM
It shouldn't be a permission issue...it is a file saved locally on my desktop. I am running Win 7 and Excel 2007.
Javeed M ShaikhPosted Apr 30, 2013, 11:53 AM
CSharp NoobPosted Apr 30, 2013, 11:20 AM
string mySheet = @"(F:\\Workspace\\Reports\\Excel\\Dailymenu.xls)";
var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbooks books = excelApp.Workbooks;
Excel.Workbook sheet = books.Open(mySheet);
Javeed M ShaikhPosted Apr 30, 2013, 10:50 AM
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.open(v=office.11).aspx