Come scrivere excel aprendolo a livello di codice con Microsoft.Office.Interop.Excel dll?

StackOverflow https://stackoverflow.com/questions/1648125

  •  22-07-2019
  •  | 
  •  

Domanda

Voglio usare Microsoft.Office.Interop.Excel dll per scrivere i dati in Excel. Ho un codice:

if (System.IO.File.Exists(strFileName))
{
    System.IO.File.SetAttributes(strFileName, FileAttributes.Normal);
    System.IO.File.Delete(strFileName);
}

// Open an instance of excel. Create a new workbook.
// A workbook by default has three sheets, so if you just want 
//a single one, delete sheet 2 and 3

Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Excel._Workbook xlWB = (Excel._Workbook)xlApp.Workbooks.Add(Missing.Value);

Excel._Worksheet xlSheet = (Excel._Worksheet)xlWB.Sheets[1];
((Excel._Worksheet)xlWB.Sheets[2]).Delete();
((Excel._Worksheet)xlWB.Sheets[2]).Delete();

xlSheet.Name = strSheetName;

// Write a value into A1
xlSheet.Cells[2, 1] = "Tags";
xlSheet.Cells[2, 2] = "Leak Test";
xlSheet.Cells[2, 3] = "FIR";
xlSheet.Cells[2, 4] = "SOP";

xlWB.SaveAs(strFileName, Missing.Value, Missing.Value, Missing.Value, 
Missing.Value,Missing.Value, 
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, 
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlApp.Quit();

// Release the COM object, set the Excel variables to Null, and tell the 
//Garbage Collector to do its thing
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWB);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

xlSheet = null;
xlWB = null;
xlApp = null;

Ora voglio aprire il file Excel esistente dal percorso & amp; quindi inserisci alcuni dati nel foglio Excel che chiamiamo fornito & amp; quindi verrà salvato su un percorso specifico da livello di programmazione.

Per favore rispondimi alla fonte se qualcuno può aprire il file Excel esistente & amp; che può essere append & amp; salva con un altro nome.

Saluti, Girish

È stato utile?

Soluzione

Fare riferimento qui per informazioni .

Altri suggerimenti

ExcelPackage non è più gestito. Sembra avere alcuni bug. Leggendo il commento lì, ho trovato http://epplus.codeplex.com/

Si basa su ExcelPackage ed eredita le licenze (GPL), quindi potrebbe non soddisfare le tue esigenze.

Se stai usando la versione 2007 di Excel, ti consiglio di usare ExcelPackage invece - è un'implementazione dello standard OpenXML, è molto più veloce e molto meno disordinato dell'interoperabilità COM, e puoi eseguirlo su un computer su cui non è installato nemmeno Excel (Office), come sul tuo server web.

Altamente raccomandato, ma limitato a Excel 2007 e versioni successive.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top