2016-04-08 14 views
5

Desidero aggiornare (aggiungere un altro foglio e aggiungere un grafico) un file xlsx esistente utilizzando il pacchetto excel epplus. Tuttavia, ho ottenuto un errore nella riga seguenteparte epplus esiste già

var pieChart = worksheet.Drawings.AddChart("Chart1", OfficeOpenXml.Drawing.Chart.eChartType.Pie); 

errore: Un'eccezione non gestita di tipo 'System.InvalidOperationException' in EPPlus.dll Ulteriori informazioni: fa parte esiste già

Can qualcuno mi aiuta? Grazie in anticipo.

using (ExcelPackage pck = new ExcelPackage()) 
      { 
     using (FileStream stream = new FileStream("Report.xlsx", FileMode.Open)) 
       { 
        pck.Load(stream); 

        ExcelWorksheet worksheet = pck.Workbook.Worksheets.Add("1"); 

        var data = new List<KeyValuePair<string, int>> 
    { 
     new KeyValuePair<string, int>("Group A", 44613), 
     new KeyValuePair<string, int>("Group B", 36432), 
     new KeyValuePair<string, int>("Group C", 6324), 
     new KeyValuePair<string, int>("Group A", 6745), 
     new KeyValuePair<string, int>("Group B", 23434), 
     new KeyValuePair<string, int>("Group C", 5123), 
     new KeyValuePair<string, int>("Group A", 34545), 
     new KeyValuePair<string, int>("Group B", 5472), 
     new KeyValuePair<string, int>("Group C", 45637), 
     new KeyValuePair<string, int>("Group A", 37840), 
     new KeyValuePair<string, int>("Group B", 20827), 
     new KeyValuePair<string, int>("Group C", 4548), 
    }; 

        //Fill the table 
        var startCell = worksheet.Cells[1, 1]; 
        startCell.Offset(0, 0).Value = "Group Name"; 
        startCell.Offset(0, 1).Value = "Value"; 

        for (var i = 0; i < data.Count(); i++) 
        { 
         startCell.Offset(i + 1, 0).Value = data[i].Key; 
         startCell.Offset(i + 1, 1).Value = data[i].Value; 
        } 

        //Add the chart to the sheet 
        var pieChart = worksheet.Drawings.AddChart("Chart1", OfficeOpenXml.Drawing.Chart.eChartType.Pie); 
        pieChart.SetPosition(data.Count + 1, 0, 0, 0); 
        pieChart.Title.Text = "Test Chart"; 
        pieChart.Title.Font.Bold = true; 
        pieChart.Title.Font.Size = 12; 



        pck.Save(); 
       } 
+1

È 'Grafico X' o' Grafico1'? Esiste già 'Chart1' (o Chart X qualunque cosa tu stia utilizzando) presente su altri fogli di questa cartella di lavoro? – Spidey

+0

Sì, dovrebbe essere Chart1, errore di copia-incolla. Già modificato. – Svartalfar

+0

Rinominare xlsx in estensione .zip e aprirlo con 7zip o qualche altro strumento. Vedi una cartella 'xl \ drawings'? Se è così, guarda i file xml e vedi se c'è già un "chart1" elencato lì. – Ernie

risposta

1

Rinominare tabella 1 a un altro nome perché probabilmente si è creato un grafico con lo stesso nome nel vostro file excel da qualche altra parte nel codice

0

provate questo (funzionerà al 100%):

using (ExcelPackage packageNew = new ExcelPackage()) 
{ 
ExcelWorksheet worksheetNew = packageNew.Workbook.Worksheets.Add("Sheet1"); 
worksheet.Cells["A1"].Value = "what ever"; 
. 
. 
. 
Byte[] bin = package.GetAsByteArray(); 
       string file = newFile.FullName; ; 
       File.WriteAllBytes(file, bin); 

       //These lines will open it in Excel 
       ProcessStartInfo pi = new ProcessStartInfo(file); 
       Process.Start(pi); 
} 
+0

La risposta non è relativa alla domanda. Il problema riguarda l'aggiunta di un grafico con lo stesso nome di uno già esistente nella cartella di lavoro e nulla a che fare con il salvataggio di un foglio di calcolo. – Pete

Problemi correlati