Hi!
I use VS Pro 2015
The version of Microsoft Office 2010 and 2013 that you are using.
A WindowsService1 service project is being created in the C#.
The service uses code that opens any Excel *.xlsx file using the module Microsoft.Office.Interop.Excel.
Next, select any sheets of the file and run the command to convert to PDF format.
ObjWorkSheet.ExportAsFixedFormat(
Excel.XlFixedFormatType.xlTypePDF,
localPath + localName + ".pdf",
Excel.XlFixedFormatQuality.xlQualityStandard, true, false,
Type.Missing, Type.Missing, false, Type.Missing);
This command is not executed, and a failure occurs that is not caught by the try block.
The service crashes the method and runs the method again. An open excel process does not terminate, and as a result, many Excel processes accumulate. In the folder there are more files with the extension *.tmp.
We believe that this is a problem of interaction between the service and the Excel module. If the same code is inserted into a regular Windows Forms application, it runs without errors.
Other programmers found similar errors on the Internet, but no one offered an answer or solution. Below links:
https://stackoverflow.com/questions/29009041/exportasfixedformat-not-working-with-excel2013
https://stackoverflow.com/questions/25252451/excel-2013-com-api-hangs-exportasfixedformat-under-service-account
Please help us resolve this issue.
Below is a code snippet:
Process appProcess = null;
try
{
Excel.Application ObjExcel = new Excel.Application();
appProcess = GetExcelProcess(ObjExcel);
Excel.Workbook ObjWorkBook = null;
Excel.Worksheet ObjWorkSheet = null;
Excel.Sheets ObjWorkSheets = null;
try
{
ObjExcel.Visible = false;
ObjExcel.ScreenUpdating = false;
ObjExcel.EnableEvents = false;
ObjExcel.DisplayStatusBar = false;
ObjExcel.DisplayAlerts = false;
ObjExcel.Workbooks.Add();
ObjExcel.Calculation = Excel.XlCalculation.xlCalculationManual;
ObjWorkBook = ObjExcel.Workbooks.Open(localPath + localName + ".xlsx");
string[] sheetsToBeSelected = { configDateTime.Day.ToString(), "1", "2", "3" };
ObjWorkSheets = (Excel.Sheets)ObjWorkBook.Sheets.Item[sheetsToBeSelected];
ObjWorkSheets.Select();
ObjWorkSheet = (Excel.Worksheet)ObjWorkBook.ActiveSheet;
try
{
ObjWorkSheet.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, localPath + localName + ".pdf",
Excel.XlFixedFormatQuality.xlQualityStandard, true, false, Type.Missing, Type.Missing, false, Type.Missing);
}
catch (Exception ex)
{
Program.log.Info("Ошибка" + ex.Message);
}
}
catch (Exception ex)
{
Program.log.Info(string.Format("Проблема чтения Excel-файла, ошибка - {0}", ex.Message));
appProcess.Kill();
}
finally
{
Marshal.ReleaseComObject(ObjWorkSheet);
ObjWorkBook.Close(false);
Marshal.ReleaseComObject(ObjWorkBook);
ObjExcel.Workbooks.Close();
ObjExcel.Quit();
Marshal.ReleaseComObject(ObjExcel);
ObjExcel = null;
ObjWorkBook = null;
ObjWorkSheet = null;
GC.Collect();
GC.WaitForPendingFinalizers();
appProcess.Kill();
Program.log.Info("Good");
}
}
catch (Exception ex)
{
appProcess.Kill();
Program.log.Info(ex.Message);
}
appProcess.Kill();