Update excel values via c# .net, allow excel to calculate the value - c#

I have an excel spreadsheet that performs complex calculations. I am injecting a value into excel using c#, and then extracting the new value of the calculations in c# also. The problem is the calculation does not happen unless i manually open the excel file and save it.
for example if cell A1 = 1, A2=3 A4=sum(A1+A2)
after updating A1 = 5 via c# the value of A4 is still 4 instead of 8
Is it possible to get the excel file to update without manually opening it? I am currently using EPPplus as interop cannot be run on azure.
Any help would be appreciated even if it's just telling me it's not possible.

Try Spreadsheet Gear. Works great in a server environment as well.
EDIT: The alternative is to use Automation, but I generally advise against it. Too often you end up with memory leaks and crashes.

Related

Enter Data Into Excel and Print it Out With Out Microsoft.Office.Interop.Excel Service C#

I wantted to insert data an excel after that read calculated values.But this program will work on microsoft server.So Interop service not quite well for that.I looked OleDb ant another services but none of them cant calculate.
I will get values than put excel after calculation i get calculated values to my program.
I did this situation with interop but there is problem with windows server.Can i do this with different style?
You may consider using the Open XML SDK which allows recalculating sheets by using the following code:
spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
See OpenXML SDK: Make Excel recalculate formula for more information.
Also you may consider using third-party components designed for the server-side execution.

How to update data in excel File using C#?

This Question might be repeated, But I couldn't get solution regarding my problem so far. I'm new to Interop. I'm using excel file (as a database).
Here is data presentation in excel file
in my data If Card ID repeated then I need to increment '1' in Counter in the same row, similarly I need to fetch IP address of same row..
I'm using Interop Excel approach to insert data in excel file..
Kindly tell me how can I perform that update operation to that excel file through C# (WPF)
Sorry for bad English..
Thanks
I recommend using Closed XML
You write to the file directly and don't need Excel. It will need to be the latest version of an Excel file to work (The open xml standard).
Epplus.dll or npoi.dll will also read/write to excel files w/o excel.
Save the data in an XML or JSON file, then when you want to visualize them you create the excel file from these data, so you will have a very light file and easy to read and update if you wish.
I haven't done this specifically through wpf, but you can access powershell cmdlets through .net and powershell has commands for retrieving and writing Excel data.
That said, my experience has been it's very tedious and inconsistent with bugs. I would tell your client that using an Excel file as a database is impossible and certainly prone to failure in practice.
For one thing you will run into read/write restrictions if it is used by anything else.
If you don't mind to use comercial libraries, you can try to use Aspose.Cells. It has rich cells API and able to work without Excel interop API.

how to edit a calculation cell and read the result cell in an excel file using c#?

The excel file has super long formulas and rather than convert them to C# I would prefer to just be able to read from it the result cell after I input values in the calculations cell via the app which would be written in C#
To partially answer your question, If you're not familiar with using the Interop library within C# I suggest you check out this link for basic implementation.
http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm
Then, once you've got your app set up to manipulate excel files check out this one for how to read a value from a cell:
How to read single Excel cell value
HOWEVER, depending on how large your excel file may end up being, it may be a good idea to just go ahead and convert the formulae into C#. Taking a little bit of extra time on that will save you tons of time while waiting for excel to execute statements, as I believe it runs off of/in conjunction with VBA(which is an interpreted language as opposed to C# being a compiled language)

Read&Write an opened excel file in C# while it is being edited

Good morning!
I have been struggling with a problem for a few weeks and when it seemed that the worst part was solved, now I have a problem with an Excel file.
What I want to do is to read and write an Excel file (Test.xls) by a program (Datahub), and at the same time, read and write the same Excel file by Unity3d using a C# script. I am using Bytescout Spreadsheet in C# to read/write from Excel. I have:
-Cells with references from Datahub, which values are being updated constantly. These values need to be read by Unity3d using C#
-Cells with values written by Unity3d using C#, which values are being read constantly by Datahub
Datahub needs the excel file to be opened in order to read/write but on the other hand, Unity3d through C# needs the file to be closed.
I have tried with a macro in the excel file to autosave it and in C# open the file and close it each time it reads/writes, but it does not work. I also tried to autosave it as a copy and in C# open that copy but it does not work either.
Would you know how to face this problem? Thanks a lot! :)
If you using ByteScout Spreadsheet SDK API then you may use your Excel file as the in-memory database to store values and recalculating other cells like this:
private void ReCalculate()
{
Worksheet worksheet;
worksheet = _spreadsheet.Workbook.Worksheets[0];
worksheet.Cell("A2").Value = System.Convert.ToInt32(TextBox1.Text);
worksheet.Cell("B2").Value = System.Convert.ToInt32(TextBox2.Text);
worksheet.Cell("C2").Formula = TextBox3.Text;
worksheet.Cell("C2").Calculate();
TextBox4.Text = worksheet.Cell("C2").Value.ToString();
}
This way serves well if you already have the spreadsheet with some data and formulas inside cells.
But if you are looking to just have a good way to store and retrive data then you should be better use database as it provides a scalable and reliable way to store large amounts of data (millions of rows), run queries and actually you may 2 or more simultenious connections! Databases are able to provide higher speed because of their internal design (indexes etc) while spreadsheets are better optimized to store data.
XLS, XSLX, CSV and other files are better suited for data export and data import to/from databases.
Disclosure: I'm affiliated with ByteScout

Excel C# Interop

I have a program that uses a dynamically created reports in excel, and prints them out to a pdf. I am having a problem with some of the reports that run for 4-5 hrs. Excel memory usage starts to exceed the system limits, and I can not determine a way of clearing some of the memory without killing the application process. Does anyone have any incite? I can't kill and restart Excel process because my objects, in the Excel Object in C#, need to persist some information. THanks.
First of all, I suggest that you can prepare your data in database first, because the dynamically invoking is really slow.
Just let your excel api application to do the works about display and convert.
Did you try using excel.Visible = true to show the excel UI?

Categories