Excel文件到PDF文件

问题描述 投票:0回答:3

嗨我正在尝试将带有文本框和矩形对象的Excel文件转换为PDF文件,我尝试使用Spire.Xls但在转换XML错误时遇到错误然后当我尝试GemBox文本框和矩形未显示在PDF上然后当我累了Microsoft .Office.Interop.Excel没有正确转换提前谢谢

asp.net excel pdf
3个回答
0
投票

您复制粘贴以下方法,

private bool ConvertToPDF(string sourcePath, string targetPath, XlFixedFormatType targetType)
    {
        bool result;
        object missing = Type.Missing;
        ApplicationClass application = null;
        Workbook workBook = null;
        try
        {
            application = new ApplicationClass();
            object target = targetPath;
            object type = targetType;
            workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                               missing, missing, missing, missing, missing, missing, missing, missing, missing);
            workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (workBook != null)
            {
                workBook.Close(true, missing, missing);
                workBook = null;
            }
            if (application != null)
            {
                application.Quit();
                application = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;

    }

将引用添加为“Microsoft.Office.Interop.Excel”,添加后,右键单击“Microsoft.Office.Interop.Excel”并选择属性,并将“嵌入互操作类型”更改为false

你可以像下面这样调用方法

ConvertToPDF(@"D:\WithImage_Shapes.xlsx", @"D:\WithImage_Shapes.pdf", XlFixedFormatType.xlTypePDF);

你可以得到你期望的结果。

如果我的答案满足您的期望,请向上提示答案并单击绿色勾号。


0
投票

您是否尝试过将文件另存为保存类型(在下拉列表中向下)和* .pdf

对我来说,也可以转换文本框和三角形。

把它放在一个按钮上,并通过引用为我工作:

using Excel = Microsoft.Office.Interop.Excel

Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"H:\My Documents\Fun\tri.xlsx");
            xlWorkbook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, @"H:\My Documents\Fun\tri.pdf");

0
投票

我尝试过Spire.XLS但它转换成功了吗?我的代码:

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"aa.xlsx");

Worksheet sheet = workbook.Worksheets[0];

sheet.SaveToPdf("ToPDF.pdf");

PDF文件:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.