Reputation: 117
I would like to export a specific worksheet of my excel file to PDF. When I do wbk.ExportAsFixedFormat it work but it dosn't work when I do worksheet.ExportAsFixedFormat.
Someone can help me?
I have to "activate" the worksheet maybe ?
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
namespace XLConvert
{
class Program
{
static void Main(string[] args)
{
Application app = new Application();
Workbook wkb = app.Workbooks.Open(@"C:\Users\ALPIC\Desktop\XL\0000351608.xlsx");
var worksheet = wkb.Sheets["FRA"];
worksheet.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, @"C:\Users\ALPIC\Desktop\XL\0000351608.pdf");
wkb.Close(false, @"C:\Users\ALPIC\Desktop\XL\0000351608.xlsx");
Marshal.ReleaseComObject(wkb);
}
}
}
Thanks!
Upvotes: 0
Views: 262
Reputation: 117
I found !
I tried this and it works :
static void Main(string[] args)
{
Application app = new Application();
Workbook wkb = app.Workbooks.Open(@"C:\Users\ALPIC\Desktop\XL\0000351608.xlsx");
((Worksheet)wkb.Sheets["FRA"]).ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, @"C:\Users\ALPIC\Desktop\XL\0000351608.pdf");; //activates first sheet
wkb.Close(false, @"C:\Users\ALPIC\Desktop\XL\0000351608.xlsx");
Marshal.ReleaseComObject(app);
}
Upvotes: 1
Reputation: 79
From the looks of it, var worksheet = wkb.Sheets["FRA"];
gets assigned as a worksheet collection, while you specifically assigned wkb as a workbook, which has that function.
Upvotes: 0