Paste-Link Excel ranges into C# app - c#

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.

Related

Open and Edit Excel sheet in browser and save it using Visual Studio C#

Probably looking for a feasible solution for the below requirement
The user will upload the excel sheet through file upload in Web Application. And I am saving it in physical path of the server.For viewing it, I am rendering it in iframe in the html that works perfect.
Now the requirement is the entire excel sheet needs to be opened in browser and cells, rows, columns could be modified and then saved to server.
All with Interop, aspose, third party dlls, only certain fields could be edited through code by specifying the cell range and row number.
This case in quite that any cell/row/field value entire excel could be modified.

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.

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

how to identify modified cells of a datatable?

MY users want to download data into a spreadsheet, modify and then upload to save it. I have written a feature to download table into a tab separated file and upload the modified file. Now they want to review it before clicking "save". Wondering if there is any easy way to highlight modified cells.
PS: yes. some refuse to use any other tool. They love their spreadsheets.
Add an additional column checksum that is the hash of all the values retrieved from database and before saving re calculate the hash and highlight the cells that are modified.
If I understand the question, you want changed values to be denoted with a color or some equivalent identifier, you can do this with a simple macro in Excel:
Right click the tab you want to include the macro on and select "View Code", then type the following VBA:
Private Sub WorkSheet_Change(ByVal Target as Range)
Target.Interior.ColorIndex = 6
End Sub
This will make the background change for any modified cell to bright yellow.
You will need to modify the "download excel" function to supply a .xlsm file from your site and use a templated file on the server (look into EEPlus for serving up Excel files from .NET - supports templating) that contains the macro for this to work. If that is not possible, you could go the harder route and develop a custom Excel Add-In that tracks changes, but this would need to be deployed to each users workstation / Excel install..

How can I export data from Excel to png?

I have a set of Excel spreadsheets with multiple tabs which contains each one table that I need to export as pictures in an automated process (I have dozens of such files to process).
While I could "manually" select the table, copy and paste them as image in another software, I need to industrialize this process to save time.
What would be the best approach using .Net or any builtin Excel feature?
Thanks
Check this question.
Programmatically (C#) convert Excel to an image
It looks like they're doing what you need?
Think I would use a small C# apop to do it - that assumes that you have a one off task and don't want to mess about with Excel templates or global excel macros and opening each spreadsheets etc.
I would do it like this:
dump all my excel docs in a single folder.
open up each doc in the folder in C# app
iterate each tab
If data capture data for all used ranges (from A1 to the whatever the bottom right cell is) - for any embedded charts pull them off as well
If chart pull it off
dump each to the folder as an image prefixed with the excel doc name and some iterative suffix like _chat01 _data01
How to rwead it ina and convert to image? See here => http://csharp.net-informations.com/excel/csharp-excel-chart-picturebox.htm
Copy all desired cells
Open MS-Paint
Paste
Save as PNG.

Categories