Office automation export Excel as pdf





 
private void saveBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
{
_pdfFilePath = Path.GetDirectoryName(_filePath) + "\\" + Path.GetFileNameWithoutExtension(_filePath) + ".pdf";
if (File.Exists(_pdfFilePath))
File.Delete(_pdfFilePath);
 
object pdfFilePath = _pdfFilePath;
object oMissing = Missing.Value;
object fileFormat = Excel.XlFileFormat.xlOpenXMLWorkbook;
_excelDocument.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF,
pdfFilePath,
Excel.XlFixedFormatQuality.xlQualityStandard,
true,
true,
1,
10,
false);
PdfFilePath = PdfFilePath;
_excelDocument.Close(oMissing, oMissing, oMissing);
releaseObject(_excelDocument);
DialogResult = DialogResult.OK;
}
 
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}



Comments are closed.