嗨我正在尝试将带有文本框和矩形对象的Excel文件转换为PDF文件,我尝试使用Spire.Xls但在转换XML错误时遇到错误然后当我尝试GemBox文本框和矩形未显示在PDF上然后当我累了Microsoft .Office.Interop.Excel没有正确转换提前谢谢
您复制粘贴以下方法,
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);
你可以得到你期望的结果。
如果我的答案满足您的期望,请向上提示答案并单击绿色勾号。
您是否尝试过将文件另存为保存类型(在下拉列表中向下)和* .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");