Excel formula calculation using OpenXML - c#

I have scenario where I have to prepare two batch of excel files. I have two templates. I am automating a process. The 1st Excel file has formulas in it. I populate data using C# winform and OpenXML library in the first excel file. I want to have the formulas calculate results and then grab them to populate the 2nd excel file. I have tried various methods but was unsuccessful. I did use the following code hoping it will make the excel file calculate the formulas in it.
SpreadSheetDocument.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
SpreadSheetDocument.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
So basically the process should be
Make copy of the 1st excel file and populate it with data [This part is working]
Some how make the 1st excel file data calculate the formulas [This is the step I have problem with]
Copy the formula results values to the 2nd excel file [This part is working but as formula is not calculated the values are not being copied over]
I am reluctant to use excel interlop library as closing the excel does not work as well.

Related

EPPlus: the xlsx file I generate contains cells that do not appear until I edit them in Excel

I have a report that I generate using Epplus (.NET 3.5).
When I open the file with Excel (windows 10) the cells are empty. But When I edit one of those cells I can see the correct formula and if I just press enter (no changes at all) the correct value appears correctly calculated.
EPPlus is not a full Excel engine, and does not implement formula calculations. Think of it as just a way to read/write the file format. Any formula based values will not update until you open it in Excel and perform a recalculation on the sheet or interact with a cell that triggers the calculation.

How to paste a specific range from one excel sheet to another as Paste Special - Linked Picture using C#

I am trying to including a table or specific range from one sheet into another sheet as an image/picture.
In Excel it is done by selecting the entire table from sheet2 and then on sheet1: Paste Special - Linked Picture.
Is there a way to get this done using C#? I am developing a project which requires this task.
I'm not sure about converting the contents to a picture, but you can read and write to/from a spreadsheet using the Open XML SDK (link). I've used it to manipulate Word documents, but I imagine it functions similarly for spreadsheets.

Paste-Link Excel ranges into C# app

I am writing a C# app where I need to paste/link tables/ranges from existing Excel documents.
Functionality that I am looking for is this:
user can select a range of cells in an open Excel doc and do a Copy
user switches to my C# app and does a past-link ... my app shows the table from Excel.
user can edit the source Excel doc - this does not automatically get reflected in the C# app. But I want to provide a Refresh button that when clicked will update the C# app based on the latest data from the linked Excel sheet.
I have figured out how to do a basic copy/paste. I cannot figure out how to do this paste-link. Please note I do not want to ask user in my C# app for any cell ranges..I simply want to do paste-link of what is already in the clipboard...
Any ideas if this can be done...it is all Microsoft so I would be surprised if it can't be.. but I am a C# novice.
Thanks for all input.
I figured it out. Here are the steps.
User copies a range in Excel sheet. It goes to Clipboard in a number
of formats but CSV and ObjectLink formats are of particular interest.
In C# app, trigger a Paste-Link function (this is any button).
Retrieve data from Clipboard using ObjectLink format. This comes out as text which contains:
Excel version identifier
Path to the excel file
The sheet name and the selected range in R1C1 notation
Save the ObjectLink data in your C# app, we will use it later as part of refresh
Retrieve the data from clipboard using CSV format. Parse it out and present in C# app. I converted it to HTML since this is what I am building
Modify the original source excel file - change something in the cells that were part of the original range - save the file.
Go back to C# app, trigger Refresh functionality (this is any button). IN your code do the following:
Using ObjectLink data saved in step 2, open the Excel sheet in the background using Excel Interop API tools. Select the sheet and range. Copy the range programmatically to clipboard.
invoke the same copy from clipboard as used in the last step of 2. Basically get the updated Excel data in CSV format from clipboard and replace the original representation you built during step 1.
This works like a charm although the COM part of opening an excel doc from C# is a bit slow I have to admit.
I have not found any references to this procedure on the net...works for me like a charm.
Cheers.

How to work with .xls file in C# application

In a C# console application I have a PointF[] array. I need to draw those points in an .xls chart file, but I need guidance how to do this?
Have a look at Open XML SDK 2.0 for Microsoft Office
I've used it before to do something like you're describing.
I started off creating an .xls file in Excel that had the correct chart which read values from a specific range of cells and so on. Then using the SDK I added/changed the values of those cells and saved a new version of the file. So basically I used the file created in Excel as a template that I could change whenever without changing the code (as long as the values are written to the same range of cells).
You could also put the value cells in a separate worksheet so that the user does not see them when opening the file...

Excel Automation: read a csv file and update an Excel file

I have an automated test which produces a csv with two columns of data.
I have an excel file which I use to gather the results of all the runs in a worksheet.
I want to fully automate the process of updating the Excel file after each test run.
This is probably what I want to do:
1. Read the two columns from the CSV file
2. Paste the two columns in a worksheet in the excel file, in the first
empty column to the right of the existing block of columns.
3. Save the Excel file
EDIT:
Now i understand that i can do steps 1-3 using a macro.
All that is left for me to figure out is how to launch the macro.
You can read the csv and write to the Excel sheet with a single data provider, the OleDb provider. Here is an article on how to write to Excel, and here is one on how to read csv.
Once you write the macro, it should be in the macro list in Excel for that Excel document. You can bind the macro to a keystroke (like Alt-i, Alt-whatever) so that it runs when your press that key combination.
I've done this on a couple of similar projects where I need to import CSV files from other sources and put the data into an Excel sheet (or sheets)

Categories