Writing on open Excel application using NPOI - c#

folks,
Environment
Windows 8.1
Visual Studio 2013
C#
Issue
How do I write values and make charts on visible Excel sheets using NPOI (https://npoi.codeplex.com/).
Why do I want that?
I'm developing an application to measure temperature in an apparatus. To put together experimental data in one place, I'd like to record data on an Excel sheet and make a chart on the sheet. In addition, I'd like to keep the Excel sheet visible and check the chart updated in real time.
You could also make graphs on Windows Form apps with MeasurementStudio by NationalInstruments for example but considering the flexibility of Excel charts (size and xy range changeable, easy-to-use user interface, etc...), I'd like to stick to Excel.
You can easily do this with Microsoft.Office.Interop.Excel by
ExcelApp.visible = true;. However, this module requires users to release every COM object generated. Otherwise, the objects remain and eat up memory. This is the reason I prefer to use NPOI.
How can I achieve this? Any answers would be appreciated.

You cannot do this with NPOI. NPOI reads and writes data from serialized Excel files. You cannot access those files while Excel has them open, and even if you could, Excel simply wouldn't re-read the files so your modifications wouldn't show up.
The problem you describe comes down to "I want to interact with a running Excel instance without using Excel interop". That's not going to work.

Related

C# program runs too long mapping to an excel sheet

I have a C# program that takes a legacy report file and maps to an Excel. It was running ok but we changed the process. The legacy program groups all the detail rows together and my program breaks page rather than before when they provided all the pages. this had added to run time about 4 times as long.
I have been told that if I can manually modify Excel to create 'proper' output for input file, It can speed things up a good bit.
also go to an Excel code-behind or add-in which would run from Dxcel and thus be faster
Can someone direct me how to apply these 2 ideas?
We do the code now as an array and write the entire row rather than cell by cell.
here is copy of the code: http://www.mediafire.com/?cebg17u5wl0ir25
Automation of Office applications is generally very slow. I just encountered this problem while trying to create a complicated graphic with Visio form c# code. It lasted about 30s. Now i create a SVG-File, that is then opened in Visio. Creating the SVG-File lasts less than 1s now!
I suggest that you export your data as CSV-File and then import it into Excel. Do only the minimum, i.e. the creation of worksheets, the import of the CSV and the formatting, with Excel-automation.

C# output to Excel spreadsheet, using Excel 2000+

I'm writing a program that reads a text file, extracts information, and outputs it to a template Excel spreadsheet that already exists.
I've managed to do this on my computer using the Microsoft.Office.Interop.Excel reference and its related methods, and it works fine. I have Excel 2010. However the computers that this program will be used on mostly have either Excel 2000 or Excel 2003, and it won't work on them.
Does anyone know a way to make a program target all versions of Excel from 2000 upwards?
Cheers,
Greg
If your needs are simple and you don't have $900 for Aspose.Cells to throw around, you can do any of the following:
Use NPOI to read, inject data into, and export your template.
Create a basic HTML file with a table and just named it *.xls. You can save your template in Excel as HTML and replace bits and pieces to insert your data.
Create an XML file using Office 2002/2003 XML format, it's pretty straightforward (caveat: can't be read in Excel 2000). As above, you can save your template in XML Spreadsheet format, read it in, and do some simple stuff to inject your data.
You really need to target 2000 or under. 2010 and 2003 will open a 2000 format document whereas 2000 will not open a 2010 document. Office has a single format for 97-2000 and that's what you need to create to make everybody happy.
Interop depends on the version you have installed and I personally have dodged using interop due to its "unmanaged" nature (and it seems to love file locks).
If you want hassle free and extremely fast/powerful creation of Excel documents, you really can not do better than Aspose.Cells in my opinion.
Find it here.

Getting real time values from an excel feed

Hello
My application from the stockbroker has this button called "Start excelfeed" which opens an excel file and then updates the incoming prices in realtime. I want these prices extracted into my application (Java), but after several tries with jxl and poi ive found out it only extracts the values which occurred last time i saved the excel files. Are there any possible ways to extract these prices in real time? I'm not sure which method theyre using to feed the excel file, except their app is written in C#.
You can have a look at the VBA code behind the "Start excelfeed" button by hitting Alt+F11 ... Then you can check how the excel macro taps into the data source und try to adapt that in Java
[EDIT]
#Zico Sorry then I got you wrong. In that case I'd try to automate it via the Java Robot Class like user489041 suggested or you could fire up wireshark and try to snoop around the network traffic
They are using the DDE technique and you should use excel automation to pull data from excel into your app.
In the old days, we used DDE links. Chances are your stockbroker addin uses precisely that.
Chances of using DDE from java are pretty slim (close to zero) because it is windows specific. However, possibly there are other methods involved (like COM components or TCP/IP connection). I'd suggest you get the values from teh stockbroker source like the Excelfeed, instead of going through Excel.
Of course, the problem with that is that you won't get help from the vendor of the Excel addin. If things are like they were 8 years ago, they are making tons on your buying the Excel addin and don't really want to help you program against that :)
Worst case, you could use the Java Robot class to create the Excel spreadsheet, save the Excel spreadsheet as a CSV file, then read the CSV file from your Java program.
You can do this with solutions like Obba. Obba allows to access a Java virtual machine running the "Obba Server" directly from Excel.
For your problem, you have to "create" your application from the spreadsheet (load the jar, create an object representing you app - e.g. launching it in a separate thread). Then you can feed the app from Excel... - In this case, Excel is to some extend the "control program" of your app. However, if you start the Obba Server process manually, the process will keep on running if you close and re-open Excel.

Open a single worksheet (single tab) from a huge excel file on a web browser using c# asp.net / MVC

I have huge excel files that I have to open from web browser. It takes several minutes to load huge file. Is it possible to open a single worksheet (single tab) at a time from excel file that contains many worksheets? I have to do this using C# / asp.net MVC
I'm assuming you have the excel workbook on the server and just want to send a single worksheet to the client. Does the user then edit the worksheet? Will they be uploading it back?
Assuming this is just a report then why not use the OpenXML sdk to read the workbook, extrac the sheet in question and send it back to the client? This is what #Jim in the comments was suggesting. You can get the SDK here: Open XML SDK 2.0 for Microsoft Office . However, I'm not sure if it will work with the 'old' excel format. I assume you'll need to save the template workbook in the new Office formats (xslx).
Your question is slightly unclear as to where the spreadsheet is stored.
If it's on a server you control, process it, extracting sheets you need, and create other sheets which are smaller in size. (Or possibly save them in a different format.).
If they're not on a server you control, download the file using C#, then go through a similiar process of extracting the sheet before opening it.
Having said that, I've dealt with some largish spreadsheets (20MB or so), and haven't really had a problem processing the entire spreadsheet as a whole.
So where is the bottleneck? Your network or possibly the machine you're running?
Use third party components.
We are fighting with server side Excel generation for years and has been defeated.
We bought third party components and all problems gone.
From your question, it seems you want to improve load time by using (opening) the data from one worksheet instead of the whole workbook. If this is the case and you only want the data, then access the workbook using ADO.NET with OLEDB provider. (You can use threading to load each worksheet to improve load performance. For instance, loading three large data sets in three worksheets took 17 seconds. Loading each worksheet on a separate thread, loaded same data sets in 5 seconds.)
From experience, performance starts to really suffer with workbooks of 40MB or more. Especially, if workbooks contain many formulas. My largest workbook of 120MB takes several minutes to load. Using OLEDB access, I can load, access, and process the same data in a few seconds.
If you want the client to open data in Excel, gather data via ADO.NET/OLEDB, get XML and transform into XMLSS using Xslt. Which is easy and there is much documentation and samples.
If you just want to present the data, gather data via ADO.NET/OLEDB, get XML and transform into HTML using Xslt. Which is easy and there is much documentation and samples.
Be aware that the browser and computer become non-responsive with large data sets. I had to set limit upper limit. If limit was reaced, I notified user of truncated results, otherwise, user thought computer was "locked".
Take a look at this question in StackOverflow:
Create Excel (.XLS and .XLSX) file from C#
I think you can open your workbook on the server (inside your ASP.NET MVC application) and process only the specific worksheet you want. You can then send such worksheet to the user using NPOI.
The following post shows you how to do that using an ASP.NET MVC application:
Creating Excel spreadsheets .XLS and .XLSX in C#
You can't "say" to Excel, even via Interop that you only want a single worksheet. There are a lot of explanations, like formulas, references and links between them, which makes the task impossible.
If you only want to read the data from the worksheet, maybe OLEDB Data Provider is the best option for you. Here is a full example: Reading excel file using OLEDB Data Provider
Otherwise, you will need to load the entire workbook in memory before do anything with it.

Using templates in excel with C#

As part of the c# program that I am producing I need to generate 1 workbook containing; 2 different worksheets and a 3rd that could be produced any number of times, what is the most effective way of doing this? I have looked into using templates although I am unsure how to repeat certain worksheets whilst only displaying others once. Any help or advice would be appreciated.
A simple way is to make a hands off template example workbook with the three worksheets. Then make a copy of it. Open both and re-copy worksheet number 3 on to the working workbook as a new worksheet as needed.
In response to the comment:
There are a couple of excel engines in a .net component products our there like spreadsheet gear or aspose cells. But if your application is a windows form based and where the application is guaranteed to run has office you can use office automation. You can't legally use office automation on a web server, but it is just as possible on a web server as on a client desktop. I've used the aspose cells and it's very easy to work with and very capable and a little less expensive than spreadsheet gear, but spreadsheet gear does also have a good reputation. Both of those components have very good documentation on how to do anything with excel. But if you have excel and want to use office automation, be sure to look for example code on the web on how to properly close excel from c# or vb.net. There are some tricks to getting it to close properly.
SpreadsheetGear for .NET has ISheet.CopyAfter / CopyBefore methods which enable you to copy an entire worksheet within a workbook or between workbooks.
You can see an example of duplicating a single worksheet multiple times in the Worksheet with Chart to Multiple Worksheets with Charts sample on the SpreadsheetGear / Excel Reporting Samples page here.
Disclaimer: I own SpreadsheetGear LLC
I have done this before with templates. I would create a template xls with the first two worksheets that you don’t want changed, then add a 3rd worksheet that you could copy to the end of the workbook (as you need more worksheets).
If you know ahead of time how many of the 3rd worksheet you need, then you can copy them to the end and delete the template 3rd slot.
ExcelTemplateManager t = new ExcelTemplateManager(template_path, log_path);
t.CopyWorksheetToEnd(3);
t.CopyWorksheetToEnd(3);
t.RemoveAtIndexWorksheet(3);
t.SetSomeValue(3);
t.SetSomeValue(4);
t.Close();
If you don’t know, then keep the template around to copy it to the end as needed, then when you are done, just remove the 3rd worksheet template.
ExcelTemplateManager t = new ExcelTemplateManager(template_path, log_path);
t.CopyWorksheetToEnd(3);
t.SetSomeValue(4);
t.CopyWorksheetToEnd(3);
t.SetSomeValue(5);
t.RemoveAtIndexWorksheet(3);
t.Close();
I used the Microsoft.Office.Interop.Excel dll to create my ExcelTemplateManger class. The basic idea is to create a copy of the template excel file, and work off the copy. Let me know if you need help setting that part up, but it should be too bad.

Categories

Resources