austxcara

Hi, I'm creating a software applicaition in Visual Studio using C#. I need to read/write to an excel spreadsheet. Can I get some ideas on how best to do that without opening the excel file.

Thanks,

austxcara



Re: Visual Studio Tools for Office getting content from excel in Visual Studio

Ji Zhou ¨C MSFT

Hi Austxcara,

You can set the Visible property of a Excel.Application to false. So the Excel will not luanch in visible when you manipulate the .xlsx file. Look at codes as following, they work fine for me:

Code Snippet

Excel.Application myApp = null;
object missing = Type.Missing;

private void Form1_Load(object sender, EventArgs e)
{
myApp = new Microsoft.Office.Interop.Excel.Application();
myApp.Visible = false;
myApp.Workbooks.Open(@"C:\temp.xlsx", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Excel.Range myRange = this.myApp.get_Range("A1","E5");
myRange.Value2 = "Hello";
myApp.ActiveWorkbook.Save();
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
myApp.ActiveWorkbook.Close(1,missing,missing);
myApp = null;
}

You must close the workbook you are working with in the FormClosed function, or the Excel application will remain in the memory!

Thanks

Ji






Re: Visual Studio Tools for Office getting content from excel in Visual Studio

Dennis Wallentin

Hi austxcara,

You can treat Excel files as datasources and use classic ADO to read/write to them. However, it require that the data is structured in a "database manner", i e tables. If that's not the case then the only workable approach is to use automation as Ji shows.