Read Streaming Excel file which is not saving content to hard disk - c#

i can comfortably read excel file via ADO.net and ExcelReader but i have to read an Excel file in which data is streaming(by some other application) and that data is not not getting saved on Hard disk
Problem is filestream reads content of file which is saved in hard disk :( but here data is not saved on hard disk
Task at Glance
1) Some exe named abc.exe (3rd party exe, i have no control over it) writing data to excel named temp.xls in every 1 second,and this excel file is open. We can see this data in excel but that data is not getting saved on hard disk. abc.exe is using excel just to display data where we can merely see data.
2) Now I am trying to read data from excel, since that data is not getting saved on hard disk so we can not read with help of file stream class.
3) I am looking for technique by which we can read data in C# from this opened excel file directly from its memory not from hard disk.
Is it possible to read ?
Please help me out

There is approch you can follow:
open temp.xls with readonly flag with standard way: Workbooks.Open Method
Perform your traitement on data. Peraphs copying content to another Workbook.
detect changes on temp.xls using FileSystemWatcher Class and reopen it as 1st point
Another thing can be useful is to detect end of process from abc.exe. Maybe it is only by detect non-existance of temp.xls or non-existance of abc.exe in table of process (via ManagementEventWatcher Class).
Anyway at this point you should sniff everything passing thru temp.xls.

IF you really want "live access" to those changes you can do this:
Write an Excel-Addin (basically a PlugIn for Excel)
It runs inside Excel and can receive events (like workbook/cell changed, file opened/closed etc.)...
this AddIn then communicates all information needed via IPC (for example MMF) to your EXE
Another option might be to use Interop to communicate directly with Excel - whether this works robustly enought depends on several aspects (how the other EXE communicates with Excel etc.).

Related

How to persist data in Excel (VSTO) shared among few users?

I have to load huge amount of data, pre-process it, share it among few users and finally gather updates back from users.
This is what I did in my previous project -
Created an excel add-in using C++. Loaded the data in memory using the add-in code and processed it. For each type of data I have sent the processed data to a sheet and saved a new excel file. That way, if I have three types of data, I have created three new excel workbooks. My users then opened those new workbooks, made their changes and dropped a text file that contains their changes (through a button). The main excel keeps polling for those updates (text files) and loads them as soon as they are found. That's the way I get the updates back from my users.
I am not a fan of what I did in my previous project, it produces too many temporary files (of course I can delete those). In my current project I want to use C# VSTO Workbook so I can have more control over excel. I was hoping once I load the data, I will ask my users to open the same excel in Read-Only mode and they will make changes. While testing this, I realized user's excel (opened in read-only) mode does not see the loaded data. And their changes do not update the data held in memory. This probably means I have no idea what I am doing.
Do you guys have any idea how to achieve this? I will really appreciate any help/hint.
Excel supports so-called "co-authoring" mode, when many people can edit the same document at the same time. But there is might be a catch: afaik, you need a Share Point/Office Online server/OneDrive Business to support this scenario (you need a non-free office document server product).
Using VSTO, you can do just the same you have done with C++ add-in, but in C# (means, the set of capabilities is 1:1 - it basically just wraps C++ COM Excel API for .NET)
But for online version of Excel, there may be yet another alternative - javascript addins (now that's called "Office Addins", afaik). But I doubt you'd want to process your "huge amounts of data" with javascript.
So I would say, there is a good rule: Don't fix something that isn't broken :)
If the problem is the number of temporary files, these files is not the only option to transfer data between applications. You know, you can connect two applications directly (so that they can exchange data with messages/updates). Use network, Luke :)
Of course if your 3 users live on 3 deserted islands, totally disconnected from anything, exchanging with text files on USB stick may still be the only viable option...
I think the "web" solution could be: store your file in some "co-authoring"-capable service (sharepoint, google shees, onedrive, officeonline, whatever). Make some web job to update that file in that storage automatically. Just like a "fourth" user would do.

Creating excel - improve performance

Good afternoon,
we have a small problem with performance of generating excel.
First, we was creating excel cell by cell - it is ... let's say unacceptable.
Second, we started insert into excel with one command - range creating and it is much faster, but still not perfect so we are searching next solutions.
Because we can load XML file from database, we tried used XSLT and from these two files create xls file. It is nice, but after open this file there is error message shown (it is because of problem or bug in registry). User has to accept this message and after excel is opened. We want to eliminate this error message. However we don't know how.
We was thinking about convert this xls file into xlsx but we are unable to do it becouse we can't install office on server (we cannot use Interop) and OpenXML libraries don't know work with normal xls file. So my question is:
Is possible to generate from XML file with using of some XLST (or something) the xlsx file?
Eventually can what files do we need to create and zip together if we want create xlsx file?
Thank you for information
You mention not being able to use the OpenXML libraries because they don't work with .xls files, but you also say "creating cell by cell", which implies that you are generating the file from scratch. Where is the xls file coming from? You mention excel opening, but then say you can't install it on the server. So, it appears to me that a user is uploading an xls file to your server, and then you are doing something with it and giving it back to them? If that is the case and you must be able to read/write an xls file without installing office, then I would suggest using ExcelLibrary, as mentioned in this post
Indeed, creating an xlsx file is much magnitudes faster with the open xml sdk.

How to write Microsoft.Office.Interop.Excel.Application object on asp.net output stream

I was using the Microsoft.Office.Interop.Excel in C# to create a custom .xlsx file.
In doing so I created a Workbook object. Due to the nature of complex SQL queries to grab the data, process it, and apply via Interop the custom styles and formatting the code is very lengthy. Not to mention the very careful process of avoiding memory leaks from the Interop itself, and ensuring that Excel actually closes properly after running.
I originally was testing it out as a console application, and got it working to my satisfaction. What it does is save the end result to the filesystem using the SaveAs member.
However, my next goal was to instead redirect the output as an output stream to asp.net similar to this question here. I've done some rudimentary research and I cannot seem to find an approach that does not involve first saving the Workbook to the server's file system. This may cause conflicts if several users are accessing at the same time, etc.
So my question is, is there an easy way to set the asp.net ContentType for .xlsx and stream out the Workbook object without saving it to the file system? If not, is there a way asp.net can save temporary files automatically without conflicts, serve the temp file, and then delete the temp file after it's been served?
I agree with the comments that you should avoid using Excel Interop server-side, and the third party libraries I've used (EPPlus, Aspose) all support streaming the output. However, if you want to save temporary files without conflict you can use Path.GetTempFileName.
If your ASP.NET app is running under an account without a profile, you may need to give it write access to %WINDIR%\Temp or whatever temporary directory it uses.

How do you read the binary data of an Excel file (.xls) using .NET?

No, ADO.NET will not solve my problem because the excel files I'm working with do not contain information in tabular form. In other words, there is nothing to query, and the name of the sheets and number of sheets will vary.
essentially my job is to search every single cell in an excel document and validate it against some other data.
Right now all I have is a byte[] array that represents the contents of an .xls file. Converting to a string is meaningless since it's just binary data.
If I use COM interop and run Excel in the background, is it possible to inject it with binary data in byte[] array form or do I have to save the file to disk and then automate the process of opening it and scanning each row?
Isn't there an easier way to do it?
How do you read the binary data of an excel file (.xls) using .NET
There are a number of ways, the excel file format has changed a few times so reading the files natively is hard work and version dependent, it's usually not recommended. For reading tabular data most people choose ADO.NET, but as you allude, if you need any formatting or discovery then MS would recommend COM Interop.
If I use COM interop and run Excel in the background, is it possible to inject it with binary data in byte[] array form
The excel COM object model does allow you to bulk set data to a Range object you set it with a 2 dimensional object array (object[,])
or do I have to save the file to disk and then automate the process of opening it and scanning each row?
No, you can interact with the "out of process" COM server (Excel) without having to save first, you can set your data, format it etc in memory.
Isn't there an easier way to do it?
Yes there is, checkout Spreadsheet Gear their object model is nearly identical to the com model, however you do not need Excel involved at all, it is also an order of magnitude faster working with large data. Its not cheap ($1000 bucks last time I checked) but will save you way more than that in coding effort. (I am not affiliated with Spreadsheet gear in any way)
You could use NPOI to open & read your XLS files, you'll basically want to loop through your Sheets / Rows / Columns looking for data. I commonly use NPOI to read & write XLS forms that contain data in random cells throughout a worksheet.

How to read a Microsoft Excel file which is already opened

I want to develop an application which reads data from an MS excel file which is opened.
I want to develop this application because the data is updated for every one minute in the cells present in it and i cant see it again if i want to see. so i want to read the file data and save it in a text file or an ms access file.
I know how to save it but i dont know how to read the ms excel file which is opened.
There would be a great appreciation if someone could help me.
Thanks In Advance.
If I understand your query, you'll need to open the file in read only mode to prevent any access violations from occuring.
(eg. Your app saves some new data and then the open file is saved, removing your saved data)
If you are opening it in read-only, you'll need a refresh timer that will check for revisions of the file. It would only update if the Excel file is saved as you would not be able to access the memory location of an unsaved file.
Perhaps saving your data as .CSV will be easiest to read in to your app. Excel will allow you to save as this type and it is easy to read in C#, using a normal file stream.
Hope this helps.

Categories

Resources