Home > Mobile >  Export multiple Excel Chart into one PDF with C#
Export multiple Excel Chart into one PDF with C#

Time:09-20

How can I export multiple Excel Charts into one PDF File? In Excel I would select each Chart Tab with the mouse and then save via "save as" PDF.

In C# I tried to loop over every Chart and use the methode select().

If I then use the ExportAsFixedFormat function, all charts, worksheets are saved in one PDF. But I only want the charts.

List<string> chartnames = new List<string>();

for (int i = 0; i < workbook.Charts.Count; i  )
{
   chartnames.Add(workbook.Charts[i   1].Name);
}
workbook.Charts[chartnames.ToArray()].select();
workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, filename);

CodePudding user response:

This is how it works. ExportAsFixedFormat only saves the visible worksheets

List<string> chartnames = new List<string>();
    
for (int i = 0; i < workbook.Charts.Count; i  )
{
  chartnames.Add(workbook.Charts[i   1].Name);
}
    
for (int i = 0; i < workbook.Worksheets.Count; i  )
{
  workbook.Worksheets[i   1].Visible = false;
}
    
workbook.Charts[chartnames.ToArray()].select();
workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, filename);
    
for (int i = 0; i < workbook.Worksheets.Count; i  )
{
  workbook.Worksheets[i   1].Visible = true;
}
  • Related