PROBLEMA C# Scrivere in file excel

Pubblicità

neurone3

Utente Attivo
Messaggi
1,002
Reazioni
126
Punteggio
49
Buongiorno a tutti,
Dovrei scrivere dei valori in delle celle di un file excel e non posso usare oledb.
Ho fatto questo metodo, quando lo faccio girare mi dice che il file esiste già se voglio sovrascriverlo, faccio si e risultato non scrive nulla.
Vi posto di seguito il metodo spero qualcuno sappia aiutarmi
public static void WriteExcelDataViaOp(string SourceFile, string SheetName, DateTime DataRiferimento, decimal ValoreNav, decimal ValoreCommFissa, decimal ValoreConferimenti, decimal ValoreBenchmark)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
object misValue = System.Reflection.Missing.Value;


xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(SourceFile, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(SheetName);
range = xlWorkSheet.UsedRange;


for (int i = range.Rows.Count; i > 6; i--)
{
if (range.Cells[1].Value == null)
{

int Windex = i + 1;
range.Cells[1][Windex].Value = DataRiferimento;
range.Cells[2][Windex].Value = ValoreNav;
range.Cells[3][Windex].Value = ValoreCommFissa;
range.Cells[4][Windex].Value = ValoreConferimenti;
range.Cells[5][Windex].Value = ValoreBenchmark;
xlWorkBook.Save();
xlWorkBook.Close();
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
break;
}
}
}

- - - Updated - - -

risolto avevo aperto il file in read-only potete chiudere grazie
 
Pubblicità
Pubblicità
Indietro
Top