Waiting open excel when program is writing data to excel

0

I have been making application that read data from serial port then write it to excel(continuous).

There is no problem if i open current excel file after that the serial data is written, or the serial data was written then i open the current file.

The problem is after data of serial received, then program is writing data to excel and at the same time i try to open the current file or open another excel application from out of program (manually).

error message: error: Bad extension for file(Exception from HRESULT:0x800401E6(MK_E_INVALIDEXTENSION) Line: mscorlib

Serial data received >> Open excel file >> Write data to excel >> Save as >>> Serial data received >> Open excel file >> Write data to excel >> Save as >>> After data is written to excel, i didn't closed the workbook, i just make it invisible.

This is my sample code

Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
.....
oXL = new Excel.Application();
oWB = oXL.Workbooks.Open("C:\\Folder\\SerialData" + filenumber + ".xlsx", 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
oSheet = (Excel._Worksheet)oWB.Worksheets["Sheet" + sheetnumber];
.....
oWB.SaveAs("C:\\Folder\\SerialData" + filenumber + ".xlsx",Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

What is the solution? Can you provide me some sample code?

EDIT: master @Vasi, i do that and get error.

I remember that in past time i did like that, after write to excel, close workbook and excel application. But in the past error is just when close workbook, and excel process is stuck in RAM, process doesn't disappear in task manager.

So, now i try not to close excel application and workbook, and i hope process of program will be faster when write data to excel. And as your suggestion, i tried to close excel app, workbook and worksheet like in this site http://goo.gl/SzwhCF

But process in task manager keep doesn't disappear after windows form closed and error message is Object reference not set to an instance of an object. This is my sample code http://postimg.org/image/w6614hbq3/

Is any wrong with my code?

Thank you

c#
excel
com
interop
asked on Stack Overflow Jun 20, 2014 by Anti Mafia • edited May 15, 2015 by Benoit Esnard

1 Answer

1

Due to OpenXML Standard once you opened a file in Excel Application, there is no possibility to open from other applications/coding.

Prefer comma(or semicolon) separated (we can easily import in excel) text file or database.

answered on Stack Overflow Jun 20, 2014 by Vasistan

User contributions licensed under CC BY-SA 3.0