Excel文件到PDF文件

时间:2018-05-15 08:57:13

标签: asp.net excel pdf

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

3 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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);

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

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

答案 2 :(得分: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