Manipulating Excel Data using Visual C# - c#

I need to set/insert hyperlinks for a range of values in an Excel sheet from my program written in Visual C#.
I am using "Microsoft.Office.Interop.Excel" and no third party Excel tools.
I have URLs in string format with me in the program which need to show up as Hyperlinks in the Excel workbook.
Any ideas on how this can be implemented? I did not see any familiar feature in Excel.Range which I could use. Any suggestions would be appreciated!
Thanks,
Ivar

Have you tried Hyperlinks.Add?
You can add a hyperlink to a range of cells (after setting the text content).

Related

Read excel cell styling and formatting in C#

I have an excel file located on SharePoint. I am reading the excel file in C# using OpenXML. While reading the data, I also need the information of any styles that are been added to the cells.
If there is any cell that has been added a background color to, How can I read it in C# ?
Have a look at this you will find some references about foreground color and background color of cell
https://social.msdn.microsoft.com/Forums/vstudio/en-US/c46f8610-0394-4eb9-a0f4-3d9a569817f0/other-properties-of-cell?forum=oxmlsdk
I don't know how you can do that with OpenXML, in the past i have always used Microsoft.Office.Interop.Excel. It's a nuget package that allow you to manipulate your excel style.
I hope it can help you!

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.

fetch excel column headers like A,B... in listbox of window form by using visual c#

i have made an excel COM Add-in for sending sms. in this i made a window form in which there is a listbox. i want to use excel columns like A,B,C.... as such in listbox list, though which i can read all contents of a particular column of excel. here i dont want to write code for a particular file. i want that when user open excel , my plugin should work with active workbook. please help me how to do that. please give me some coding examples which can i implement in my plugin. really need for help.please...
here i am replying on my own question. this answer is very helpful for this question.
Microsoft.Office.Interop.Excel.Worksheet activeSheet =
(Microsoft.Office.Interop.Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
Excel.Range firstColumn = activeSheet.UsedRange.Columns["A", System.Type.Missing];
firstColumn.Select();

How to maintain formatting of cells when copying cells from Excel to Word

Currently I'm using OleDB to connect with Excel Spreadsheet and getting data in a DataTable, doing some modifications on data and then copying the data to Word.
In doing so I'm losing the formatting of the cell, like if any part of text was colored or if the background color was grayed or if it's bold.
I'm using Interop library to communicate with word and OLEDB with Excel.
If this solution is not good enough for what I need to achieve , can you suggest alternative solutions? (Macros?)
I tried using Interop.Excel.Styles but I can't figure out how to relate it with the cell being currently used.
We copy the range of the table in the spreadsheet and the directly paste in the word document which preserves formatting.
wordDoc.Tables.Add(b.Range,newsheet.UsedRange.Rows.Count,newsheet.UsedRange.Columns.Count);
Microsoft.Office.Interop.Word.Table table = b.Range.Tables[1];
newsheet.UsedRange.Copy();
table.Range.Select();
wordApp.Selection.Paste();
wordDoc is Word.Document and wordApp is word.Application. Hope this helps
Yeah, that's gonna happen. OleDB moves data, not formatting information. If you want the formatting, you're going to have to copy/paste from Excel to Word. If you need to automate the process, VBA is the easiest way to control Excel and Word from the outside.

Converting HTML table to excel sheet using C#

I have html table in Microsoft.Office.Interop.Outlook.MailItem body and I just need to fill excel sheet with this table using C# for desktop application. Could any one help me in this regard. Thanks
A quick and dirty way:
File.WriteAllText(#"C:\Temp\Table.xls", mailItem.Body);
Excel will open it even though the file does not contain a valid xls document
You can also use HtmlAgilityPack to parse the e-mail and EPPlus to write to Excel (only 2007/2010 xlsx version).

Categories