Formatting Issues with Excel Interop using C# - c#

I have a C# application that uses an existing Excel spreadsheet as a template and fills in data. In the template there is a blank row that has some style formatting such as font/border and data formatting such as currency and percentages. When filling in the data from the application I copy this row n times and fill in the data. The style formatting works but as far as the data formatting it doesn't apply until I open up the spreadsheet click on the column then off of it. Does this have something to do with Excel interpreting the values as just string data and not being able to format it? Am I stuck with having to do my data formatting in the application?

You could try explicitly formatting each row after populating it. Is this Excel 2003 or 2007?

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.

Copying format of cells in a row to all populated rows below

We are using the AccessDatabaseEngine DLLs to read and write to an .XLSX document without opening it in Excel on the server. I'd like to copy the cell format from a row (in this case row 9) to all the rows below it that are populated.
Unfortunately we do not and will not have Office/Excel installed on the server, so using the Range class doesn't appear to be an option - Copy format from one row to another using c#.
Has anyone done this before just using the AccessDatabaseEngine or is its functionality limited to the values in the cells only?
Any help would be duly appreciated.

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

Formatting of data exported to excel file

I have exported data from a text file to an excel sheet,
with the help of clipboard copy and paste.
I used PIA for this.
The exporting is done properly and the data is exported to a single column i.e in the A Column.
I want to fill my data in the rows of this column ,so that they are filled properly in the cell and I dont have to expand the A column to view complete data.
And also how can i change the font of the sheet while exporting
How is this text file created? If you have full control over its contents, I suggest using a csv ( comma-seperated-value ) file instead. You won't have control over the default font excel uses but it would fulfill your requirement of having the data populate multiple columns.
Outside of using csv file to handle this data, I suggest you look into exporting it into an excel document directly, you would have full control over look and the font used for the contents.

Categories