Åbne eksisterende excel fil
HejJeg forsøger at lege med Excel og nedenviste kode, virker ganske glimrende. Jeg får åbnet excel og skrevet noget ind.....men det jeg har brug for, er at åbne en eksisterende excel fil ikke en ny hver gang. hvordan skal jeg få det føjet ind i koden?
En anden ting - jeg har oprettet en reference til excel 11. Hvad sker der, når jeg kører programmet på en computer, der måske ikke har samme version af excel men 10 måske?
Koden der virker:
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
return;
}
xlApp.Visible = true;
Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
if (ws == null)
{
Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
}
// Select the Excel cells, in the range c1 to c7 in the worksheet.
Range aRange = ws.get_Range("C1", "C7");
if (aRange == null)
{
Console.WriteLine("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
}
// Fill the cells in the C1 to C7 range of the worksheet with the number 6.
Object[] args = new Object[1];
args[0] = 6;
aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args);
// Change the cells in the C1 to C7 range of the worksheet to the number 8.
aRange.Value2 = 8;
mvh
simsen :-)
