我尝试将excel
转换为PDF
,我可以转换它。但是在打开该文件时,我收到的错误如下图所示。
我已从此链接中引用此链接How do I convert Word files to PDF programmatically?我可以解决Word到PDF文件转换问题。我不需要在服务器上安装办公室,我想对excel
和PPT
文件做同样的事情。
我的代码:
// Create a new Microsoft Excel application object
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
excelApplication.Visible = false;
excelApplication.ScreenUpdating = false;
FileInfo ExcelFile = new FileInfo(sourcePath);
// Cast as Object for Excel Open method
Object filename = (Object)ExcelFile.FullName;
// Use the dummy value as a placeholder for optional arguments
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApplication.Workbooks.Open(ExcelFile.FullName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
object outputFileName = ExcelFile.FullName.Replace(Path.GetFileName(ExcelFile.FullName), Path.GetFileName(targetPath));
object fileFormat = WdSaveFormat.wdFormatPDF;
// Save document into PDF Format
//excelWorkbook.SaveAs(outputFileName);
excelWorkbook.SaveAs(outputFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
// Close the Excel Workbook, but leave the Word application open.
// excelWorkbook has to be cast to type _Document so that it will find the
// correct Close method.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((Microsoft.Office.Interop.Excel.Workbook)excelWorkbook).Close();
excelWorkbook = null;
// Excel has to be cast to type _Application so that it will find
// the correct Quit method.
((Microsoft.Office.Interop.Excel._Application)excelApplication).Quit();
excelWorkbook = null;
注意
我不想使用内置的导出功能来实现这一目标。如下我已经有了代码。这是用于将 excel转换为pdf 的工作代码。
Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
excelWorkbook = excelApplication.Workbooks.Open(sourcePath);
excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, targetPath);
excelWorkbook.Close();
excelApplication.Quit();
excelApplication = null;
请让我知道我在哪里做错了。
答案 0 :(得分:0)
试试这个: - 这个代码足以将excel转换为pdf。 为我工作,我可以阅读pdf文件。
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excelDocument = excel.Workbooks.Open(savedFileName, ReadOnly: true);
excelDocument.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf");
excelDocument.Close();
excel.Quit();
在该方法之外请创建:
public Microsoft.Office.Interop.Excel.Workbook excelDocument { get; set; }
答案 1 :(得分:-1)
代码正在运行。只需检查您是否为源和目标传递了正确的文件名(带扩展名)参数。还要检查已安装的办公室是否已另存为PDF选项
// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
word.Visible = false;
word.ScreenUpdating = false;
FileInfo wordFile = new FileInfo("c:\\a.docx");
// Cast as Object for word Open method
Object filename = (Object)wordFile.FullName;
// Use the dummy value as a placeholder for optional arguments
Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
doc.Activate();
object outputFileName = wordFile.FullName.Replace(System.IO.Path.GetFileName(wordFile.FullName), System.IO.Path.GetFileName("c:\\a.pdf"));
object fileFormat = WdSaveFormat.wdFormatPDF;
// Save document into PDF Format
doc.SaveAs(ref outputFileName,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Close the Word document, but leave the Word application open.
// doc has to be cast to type _Document so that it will find the
// correct Close method.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
// word has to be cast to type _Application so that it will find
// the correct Quit method.
((Microsoft.Office.Interop.Word._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;