How to resolve the issue related to Excel while reading data from a PDF file via C#?
I am extracting the data from a PDF file and exporting the same to Excel sheet via a windows service. But on doing the same below issue is seen:
RETRIEVING THE COM CLASS FACTORY FOR COMPONENT WITH CLSID {00024500-0000-0000-C000-000000000046} FAILED DUE TO THE FOLLOWING ERROR: 80070005 ACCESS IS DENIED.
What I have tried:
The core code of extracting data from PDF File is as below:
#region GetFillData
private void GetFillData(string SourcePDFFullPath)
{
WriteServiceLog("" + SmartPDFLogCaption + "Entered " + MethodInfo.GetCurrentMethod().Name + " Method");
try
{
Char delimiter1 = '|';
Char delimiter2 = '~';
PdfReader pdfReader = new PdfReader(SourcePDFFullPath);
AcroFields pdfFormFields = pdfReader.AcroFields;
string PDFFileName = Path.GetFileName(SourcePDFFullPath);
string TemplateID = pdfFormFields.GetField("hidTemplateId");
string CapturedPDFValues = pdfFormFields.GetField("hidPDFDataExport");
string[] PDFValues = CapturedPDFValues.Split(delimiter1);
string UniqueNo = PDFValues[0].Substring(PDFValues[0].LastIndexOf(delimiter2) + 1);
string CurrentDate = DateTime.Now.ToString("" + FileRenameDateFormat + "");
if (TemplateID == "")
{
WriteServiceLog("" + SmartPDFLogCaption + "" + TemplateIdValidationMsg1 + "" + SourcePDFFullPath + MethodInfo.GetCurrentMethod().Name + " Method");
return;
}
else
{
string ExcelTemplateFullPath = string.Empty;
string ExcelExtension = string.Empty;
string DestinationFullPathExcel = string.Empty;
if (File.Exists(ExcelTemplatePath+TemplateNamingStart+TemplateID+".xls"))
{
ExcelExtension = ".xls";
ExcelTemplateFullPath = ExcelTemplatePath + TemplateNamingStart + TemplateID + ExcelExtension;
DestinationFullPathExcel = DestinationExcelPath + TemplateNamingStart + TemplateID + "_" + UniqueNo + CurrentDate + ExcelExtension;
}
else if (File.Exists(ExcelTemplatePath + TemplateNamingStart + TemplateID + ".xlsx"))
{
ExcelExtension = ".xlsx";
ExcelTemplateFullPath = ExcelTemplatePath + TemplateNamingStart + TemplateID + ExcelExtension;
DestinationFullPathExcel = DestinationExcelPath + TemplateNamingStart + TemplateID + "_" + UniqueNo + CurrentDate + ExcelExtension;
}
else
{
WriteServiceLog("" + SmartPDFLogCaption + "Template Id:" + TemplateNamingStart + TemplateID + " doesn't exists in the Template Folder" + MethodInfo.GetCurrentMethod().Name + " Method");
return;
}
_Application xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.DisplayAlerts = false;
object misValue = System.Reflection.Missing.Value;
_Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkBook = xlApp.Workbooks.Open(ExcelTemplateFullPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
_Worksheet xlWorkSheet = (_Worksheet)xlWorkBook.Worksheets.get_Item(1);
for (int i = 0; i < PDFValues.Length; i++)
{
xlWorkSheet.Cells[i + 1, 2] = "'" + PDFValues[i].Substring(PDFValues[i].LastIndexOf(delimiter2) + 1);
}
xlWorkBook.SaveAs(DestinationFullPathExcel, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
string DestinationPDFFullPath = DestinationPDFPath + Path.GetFileName(SourcePDFFullPath);
if (File.Exists(DestinationPDFFullPath))
{
File.Delete(DestinationPDFFullPath);
}
File.Move(SourcePDFFullPath, DestinationPDFFullPath);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
pdfReader.Close();
}
}
catch (Exception ex)
{
WriteErrorLog(MethodInfo.GetCurrentMethod().Name + " " + ex.Message, ex.StackTrace, true);
}
finally
{
}
WriteServiceLog("" + SmartPDFLogCaption + "Finished " + MethodInfo.GetCurrentMethod().Name + " Method");
}
#endregion