How to read a Microsoft Excel file which is already opened - c#

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.

Related

How to import data from Excel to C# Service based database?

Im using C# and Service based database and I need to import some data from Excel to my database ..How could I possibly do this?? Please help. Thanks a lot.
You can open the excel file with the Excel database driver and read it like any other data source, however this means you need the driver, which isn't installed by default.
Download
HowTo
However if the sheet only contains data and doesn't need any calculations, you can unzip the XLSX file, and find sheet1.xml (or whatever it's called in your file), open it in your app like any other XML file and import the data.
This is likely to be a much better long term solution, since MS has been trying to kill off the Access database driver for ages.
Also, it's been a while, but I don't believe MS recommends using the MSDE from within a service.
I would recommend you to use OfficeOpenXml.Core.ExcelPackage or EPPlus to read/write excel files. Bellow is some links to reference
https://www.nuget.org/packages/OfficeOpenXml.Core.ExcelPackage/
https://github.com/JanKallman/EPPlus
https://www.c-sharpcorner.com/article/import-and-export-data-using-epplus-core/
https://tedgustaf.com/blog/2012/create-excel-20072010-spreadsheets-with-c-and-epplus/
http://www.talkingdotnet.com/import-export-xlsx-asp-net-core/
https://toidicodedao.com/2015/11/24/series-c-hay-ho-epplus-thu-vien-excel-ba-dao-phan-1/
http://www.zachhunter.com/2015/11/xlsx-template-with-epplus-and-web-api-save-as-xlsx-or-pdf/

Multiple users writing concurrently to shared Excel file using OpenXML

I have a requirement to read and write to a shared excel(xlsx) file using open xml sdk in C#.
I have updated the shared mode setting using the answer to this question on stack overflow and the setting is updated in the created excel file.
I have wrote a small program to insert data to this generated Excel file based on this.
I have tested this with 3 different users trying to write data at the same time over LAN.
I initially got an exception during open at the below statement.
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(excelStream, true))
At this point of time only one user can write to the shared excel file even though the shareworkbook settings were enabled.
Later I changed the above statement to use Stream as below
using (FileStream excelStream = new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite))
{
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(excelStream, true))
After this change the frequency of the initial exception is reduced but still comes sometimes. But, when this succeeds multiple users are able to write to the excel file.
I have observed two important behavior when multiple users write to a shared excel file.
Even though the Users write the data concurrently, all the rows written by a user in one session are arranged in a sequence.
When
multiple users try to write to the same shared excel file, the open
xml seem to have writing the data user after user in a sequence.
This I have verified by inserting a timestamp while writing into the
excel file for each user. Writing for user2 starts after end of
user1.
Can anyone please guide me in finding the right approach to eliminate the exception during open and also do a concurrent write to the excel file using OpenXML Sdk.
Thanks in advance.
You probably should consider using a database if you need multi-user access and expect it to work well. Even Access would be better than Excel. If you need the output to be in Excel, a report that can export to Excel could function for that.
If an exception is occurring while opening the file, maybe try handling that exception, and adding retry logic? You can use something like, https://github.com/App-vNext/Polly to make doing that easy.
The user sequencing is probably just how it's handling writes. I wouldn't expect this to be something you can work around since it's Excel, not a database.

C# File open / convert and save

I am very new to C# and hoping this is a simple question. Not finding what I need on google
I have a file C:\test\losses.csv
That I want to open up then convert to an xlsx file and save in a different directory.
Save to
C:\test\Losses.xlsx
The reason for opening the file is the move command does not convert it to xlsx, unfortunately it keeps the same structure as the csv and is unusable in that format.
File.Copy(#"C:\test\losses.csv", #"C:\test1\Losses.xlsx");
The above code works great but still is a csv file (well really a hybrid of one). That is another SAP story.
Any help will be greatly appreciated. Thanks
File.Copy only copies the file - similar to copying a file in DOS or in windows file explorer.
You'd need to translate your CSV to an XLSX file. The format should be pretty straight-forward, but you'll need to do more research:
Load the CSV as a data table
Use the Excel.XlFileFormat.xlOpenXMLWorkbook class to translate the file.
A different StackOverflow problem addresses how to use the xlOpenXMLWorkbook:
Exporting to .xlsx using Microsoft.Office.Interop.Excel SaveAs Error
Hope this helps. Good luck.

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.

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

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.).

Categories

Resources