Write while workbook is running (real time)

Good afternoon !
Want to write with the workbook opened . On a text file it works as:
public void WriteOpenedFile() {
string file = String.Format(“{0}TestW.txt”, pathToTemp);
using (StreamWriter outputFile = new StreamWriter(file, true)) {
outputFile.WriteLine(“Writing…”);
outputFile.Close();
}
// Then code to kill the process here…
Process.Start(file);
}

I tried [Different Ways to Save Files|Documentation] Saving File to a Stream → with filemode.append and fileshare.write and still throws “File is being used by other process error…”

@Remus87,

I do not think this is possible even using System.IO (.NET) APIs and without involving Aspose.Cells APIs, you got to close the Excel file first. Please perform the task (write the code and run) using System.IO APIs (while the workbook is being opened/used) and without involving/using Aspose.Cells APIs and you will get the same issue.

It is possible as one colleague of mine write the similar functionality as i want to replicate in a much older language (power basic) with excel template (.xlt and now .xltx). Don’t have access to the legacy code and that is why i am coming to you as it should be another approach/trick that should do the job in Aspose.

@Remus87,
We are afraid that this does not seem to be possible using Aspose.Cells. However, if you can provide some sample code using any API other than Apsose.Cells, we will analyze it and provide our feedback accordingly.

Hi !
Found an alternative that is doing the job but is not suitable for our requirements (on security purposes) -> setting the workbook as Shared just before saving it.
So had to turn back to the previous logic (the code from the first thread here) so had several hours of attempts without any luck. I come even with a more simple version which seems to work at Compiling but not on Runtime (againgive the error _> “The process cannot access the file … because it is being used by another process”).
So, again here i am closing the Excel file 1st (as you mentioned) and then i start the process, so should definitely work. Code:

// load a simple .xlsx template into new workbook instance
Workbook wb = new Workbook(fileName);

// populating the workbook with a test cell
Cells cells = wb.Worksheets[0].Cells;
int maxRow = cells.MaxDataRow;
cells[maxRow + 1, 0].PutValue(“Test”);
wb.Save(fileName, SaveFormat.Xlsx);

//Closing Excel file
foreach (var process in Process.GetProcessesByName(“Excel”))
{
if (process.MainWindowTitle.Contains(“Remus”))
process.Kill();
}
// Running Excel file
Process.Start(fileName);

Must be a workbook property that is keeping the process somewhere, and probably need to dispose it: Tried wb.Dispose(); before starting the process and nothing.

It seems you are manipulating the Excel file in some multi-threaded application (e.g. users are opening the same file and are appending data to the sheet(s) and re-save the file), is not it?

FYI, Aspose.Cells is a pure .NET component created in managed C#, so concurrency and multithreading is not a problem by any means. Please note, as long as you don’t have shared data source and every time a user access a new Workbook or Excel file in different thread or use different file, there would be no problem at all. However, if you have a shared data/resource or the same Excel file/workbook, then you will have to do synchronization by yourself. This is the nature of MS Excel file formats and nothing to do with Aspose.Cells APIs. Also, in that case, we recommend you to create/manipulate of filling data into different workbooks, because, you should not use one workbook/file in multiple threads at the same time least you would not get stable data (the reason being there is global shared data/ resources in Excel file spreadsheets, so knowing the nature of the Excel file formats, you should not share a workbook in multiple threads).

Hi
As late coming back to the thread, reopened the project and found an alternative solution. Just have a last minor detail to fix: check the state of the workbook if saved or have unsaved changes.
The alternative approach is similar ->
check if file is in use, if yes then close it, populate row, and this is the loop in short.
only corrected few glitches regarding the process and is argument and works ok.
The question is: is there any way of checking the state of the workbook if has unsaved data ? by using m. office instance this can be easily achieved using the Workbook.Saved property, but we’re not using office objects.
The reason is to add this check inside a file in use check as such:
is file in use? (no -> populate row, run wb)
if yes -> has unsaved data? {
if yes -> Notify user to save/not save changes before populating the row within our app (using Aspose)
break;
else -> close workbook (it’s process), populate row, run workbook }

@Remus87,

Aspose.Cells parses and works with file formats (source (xml) data) only whereas MS Excel is an application so its automation APIs may depend upon it. Anyways, we will still evaluate your requirements in details and get back to you soon.

@Remus87,

To investigate your requirements, we have now logged a ticket with an id “CELLSNET-51644” for your issue. We will check if we can devise some way for checking the state of the workbook if it has unsaved data or not. We will look into it.

Once we have an update on it, we will let you know.

Thank you Amjad !

@Remus87,

You are welcome.

@Remus87,

We evaluated your issue/requirements in details. I am afraid, there is no property/attribute to return whether the workbook is changed in the source Excel file format data. Aspose.Cells works on file formats. So, we cannot support your needs. Please wrap any changes by yourself now.