首页 > 解决方案 > 工作表导出为 PDF

问题描述

我想将我的 excel 文件的特定工作表导出为 PDF。当我执行 wbk.ExportAsFixedFormat 时,它可以工作,但是当我执行 worksheet.ExportAsFixedFormat 时它不起作用。

有人可以帮助我吗?

我可能必须“激活”工作表?

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);
        }
    }
}

谢谢!

标签: c#

解决方案


我发现 !

我试过这个并且它有效:

 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);
        }

推荐阅读