Problems with ranges in Excel Interop - c#

I'm writing a program in C# that accesses the Excel Interop interface to create a spreadsheet. My program creates a worksheet, then enters some text into the first row. I want to set the font to bold for the first row, as a title, but things aren't working as expected.
First I create a range object for the row, then I set the BOLD attribute
Range rng1 = ws.Rows[1];
rng1.Style.Font.Bold = true;
This works OK and afterwards I have a bold header line. However, I noticed that the rest of the spreadsheet was getting set to bold also. So I added some code to turn it OFF but it didn't work as expected. The code looks like this:
Range rng2 = ws.Rows[rownum];
rng2.Style.Font.Bold = false;
The "rownum" variable starts at 2 and increments through 12. It's never set to 1. So this should be creating a second range object that has zero overlap with the first one. Yet for some reason, this turns off the bold attribute for everything.
It seems like the entire spreadsheet is affected by turning BOLD on or off. Oh, and I can do these steps in any order.
Any ideas what's going on and how to fix it?

Related

Excel cell formula is updating with sheet name. Exporting from ASP.NET

=CONCATENATE( "Level3!$B", MATCH($B6,Level31!$A:$A,0),":$B", COUNTIF(Level31!$A:$A,$B6) + MATCH($B6,Level31!$A:$A,0)-1)
I have this formula in excel cell. Which I am copy pasting in all the rows below through C# code. It is fine till here.
The problem is that in this formula Level3 was a sheet name which is renamed to Level31. This change is reflecting in formula automatically in three places and not on first place..Why
After manual formula update in cell it works fine.
Second problem is in next cell
where another formula is implemented. When I am opening sheet it shows #N/A
but when I click in the cell and hit enter key. It starts working..
Can anyone please guide me?
If you are referring to first place as =CONCATENATE( "Level3!$B", , that value is stored as string and will not automatically adjust from 3 to 31. Try this formula =CONCATENATE(MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)&"!$B", MATCH($B6,Level31!$A:$A,0),":$B", COUNTIF(Level31!$A:$A,$B6) + MATCH($B6,Level31!$A:$A,0)-1)
formula MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255) makes your first place also dynamic.

How to Dselect Usedrange area from spreadsheetgear after using Select Function

I have a Scenario to print UsedRange area From Spreadsheetgear For that i have used below statement
workbookView.ActiveWorksheet.UsedRange.Select();
Using above statement i got like below
and after that Print works Fine. As soon as this goes for print then i want to Dselect that UsedRange.
Means i want like below
How to do this?
You can move the selected cell back to the first cell in the sheet (or any other cell for that matter) after the print code has run using the Activate() method which will deselect the selected cells:
workbookView.ActiveWorksheet.Cells["A1"].Activate();
The API documentation here states To set the active cell, use the IRange.Activate() method.
I don't think it's possible to have no cell selected so it could be worth storing the selected range before you select the entire contents then after the print re-select the previously selected range with the above code.

Getting the number of rows PER PAGE in Excel using C#

I stated the question above the way I did, because I can already do the following, and it doesn't give me the solution I need.
I can:
Get the number of pages per sheet
Get the last USED row on any given SHEET (not per printed page, so that doesn't help)
I can find out if manual or automatic page breaks have been inserted
(just not WHERE)
And that's what I need: To know the number of the row for each page break on any given sheet. Some sheets have 1 page, some have 100.
I need to do this because I need to put something at the bottom of each page, however, the number of pages per sheet is not static. Some sheets have been set to landscape, others to portrait. Some sheets have the "Put all columns on one page" option set, which CHANGES the number of rows on that page.
PLEASE NOTE: The solution to this problem is not "Use the footer", as the footer will not accept the data that I need to put on each page.
I also cannot statically decide the number of rows each page type might have -- I let Excel decide that when I set the orientation.
I've been looking for several days now, and I have been unable to find how to determine what the last row on a printed page will be.
I'm using office and VS 2010 and I'm using the office interop. I'm not posting much code because I have no idea what to use at this point. However, it is safe to say that to access any given worksheet I'm using the following code:
Excel.Application excelApp = new Excel.Application();
Excel.Worksheet workSheet;
I'd be willing to bet that there's a very simple way to do this, and I just haven't hit across the right combination of google search terms. But it's been a few days now, so I thought I'd post something here. Thanks for any help.
I don't think you have a way to calculate that on your own... It is very complicated, because the number of rows on each page depends on many variables (Printer page size, orientation, zoom level, etc.).
You can try to use this: Repeat specific rows or columns on every printed page

Force string format in Excel cell

I'm currently exporting to Excel a RadGrid and I have some cells that have the text "1 / 10" meaning that they had 1 hour used for 10 units. When this gets to Excel, it thinks the cell is a date, so it changes the cell to be January-10 which is not what I'm wanting.
I've gone the other direction before (changing a string to a number/date) but I've never had the issue where I needed to make Excel keep the cell as a string and not try to change the format.
How do I do this? I've tried adding a apostrophe to the beginning of the cell text:
e.Cell.Text = String.Format("'{0}", e.Cell.Text);
but that seems to just make Excel display two apostrophe's at the start of the cell:
Update:
I'm using Telerik RadGrid (v2012.2.929.40) and all it does is generate some HTML for Excel to open (you always get that horrible prompt from Excel when trying to open it). So before, if I wanted something to be formatted as a number like I had specific in the grid, I could have the following event that would set the mso-number-format. But, this doesn't quite work when I want Excel to display the cell value as strictly text.
protected void RadGridQuote_ExcelExportCellFormatting(object sender, ExcelExportCellFormattingEventArgs e)
{
if (e.Cell.Style["mso-number-format"] != null)
{
e.Cell.Style["mso-number-format"] = "/#";
e.Cell.HorizontalAlign = HorizontalAlign.Left;
}
}
If you're using Excel Automation in C#, I've found the best results would be to set the NumberFormat first, which you should set to "#". Then set the value in your .Text property. (If you do it in reverse, it'll convert the date into a number, which you don't want.)
See also MSDN regarding NumberFormat.

Excel/Visual Studio/C#. How to change the display name of a cell but keep the formula

I have some code that those:
void mActiveSheet_Change(Microsoft.Office.Interop.Excel.Range Target)
{
if (Target.Cells.Formula.ToString().StartsWith("=FR("))
....
}
So whenever someone uses my custom function "=FR" I can handle it.
That's fine but while the formula value for that cell is fine, the display value is "#NAME?"
I presume that's because Excel does not recognise what =FR is.
Is there a way that I can change the display name but not the formula so I can have something other then "#NAME?" displayed?
I tried Target.Cells.Value2 = "Boo"; but that also changes the formula
I think you got this upside down.
The real goal is not to hide #NAME? error; The goal is to create your own function which can be plugged into Excel as any other standard function, like SQRT() or ABS().
Search Google or MSDN for user defined function Excel C# - there are working examples.
You are correct about #NAME?. That is the Excel error code that is displayed when a function is not recognized. You can't suppress this.
One of the 'dirty tricks' that might work is to hide the message. Detect the background color of the cell, and change the font color to match the background. Unless the cell is highlighted (not just selected) this, in effect, hides the #NAME? error message. After you handle your =FR function and return a result, then you can reset the font color to what it was before so the answer can be seen.

Categories