Last post May 08, 2017 01:53 AM by Leon Davis
Apr 14, 2017 04:31 PM|peeweex10|LINK
Hello Everyone. I am using the Microsoft.Office.Interop.Excel library to save some of the data in the workbook to a PDF. I am able to save a single sheet in the workbook but not multiple sheets. Here is a sample of what I am trying:
List<string> s = new List<string>();
wb.Sheets[s].ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, flexFile.FullName.Replace(".xlsm", ".pdf"), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
I've tried s.ToArray(), List of objects, string arrays, object arrays to no avail.
Substituting "s" with and integer or the name if the sheet (ex "Summary") works.
Apr 14, 2017 04:49 PM|bbcompent1|LINK
Hi, I found a thread on StackOverflow covering this very topic. The link to the discussion is:
And the answer to the question is this:
You should be able to accomplish this via the PageSetup class and select the appropriate PrintArea. For PDF conversion we use the 'FitToPages' feature to zoom the view so that each worksheet occupies only a single page wide.
_Workbook document = application.Workbooks.Open(FileName,
oFalse, oTrue, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oFalse, oFalse, oMissing, oFalse,
foreach (Worksheet ws in document.Worksheets.OfType<Worksheet>())
ws.PageSetup.Orientation = XlPageOrientation.xlLandscape;
ws.PageSetup.Zoom = false;
ws.PageSetup.FitToPagesWide = 1;
ws.PageSetup.FitToPagesTall = false;
oMissing, oMissing, oFalse, oMissing,
oMissing, oFalse, oMissing);
Or you can set the print area instead of zooming:
// Specified in the same range format as the UI
ws.PageSetup.PrintArea = "a1-e50";
You may need to obtain the used columns and rows from the ws.UsedRange property to supply the correct print area. Make
sure when you call 'ExportAsFixedFormat' you provide false for the IgnorePrintAreas parameter.
Apr 14, 2017 06:49 PM|peeweex10|LINK
That's a great tip for sizing the pages....I was having an issue with that also.
I can export all sheets just fine. I need to be able to select only 3 of the sheets out of the entire workbook.
May 04, 2017 04:33 PM|bbcompent1|LINK
You might have to go with a paid-for option in this case. It sounds like you need advanced control over PDF generation.
May 08, 2017 01:53 AM|Leon Davis|LINK
You can take a look at Spire.XLS, it allows converting Excel workbook to PDF along with converting specific worksheets to PDF.
Convert worksheet to PDF in C#, VB.NET
Convert Excel to PDF in C#