C# Excel Formatting Issue - c#

I have an issue which I cannot solve. I'm writing some strings into Excel cells. But Excel is formatting them in a wrong way. These strings are IDs, for example 12A1, 12D1, 12E1 etc..
Everything works well except from the "12E1" string which is seen as an exponential. I tried with numberformat but it doesn't work.
For example the string "12E3" is written as 1.2e03, or 1200 and so on. i just want to see "12E3" written there. This program is going to be used by many PCs so i cannot change the Microsoft Excel General Settings for everyone!
Thank you and sorry for my poor english!
EDIT: Here's the code:
foreach (var Citem in ComQuery)
{
i++;
xlWorkSheet.Cells[i, 1] = "'" + Citem.commessaID; //the item is "12D3"
xlWorkSheet.Cells[i, 2] = Citem.commessaID; //the item is "12D3"
}
The first cell gives me the string "12D3" but with a warning on Excel, the second cell gives me 1.2E+004

xlWorkSheet.Cells[i, 2].NumberFormat = "#";
xlWorkSheet.Cells[i, 2] = Citem.commessaID;

You could do something like this:
var id = "12E1"
ws.Range("A1").Value = "'" + id
which will give your cell the following value:
'12E1

Related

Why is an # symbol after = symbol in excel formula with EPPLUS?

This is how the formula appears in excel:
I want to use Forecast formula in Excel with EPPLUS from c#. The formula in the code is correct but in Excel appears =#FORECAST(params).
ExcelRange targetDate = sheet.Cells[listItems + 2, 2];
ExcelRange values = sheet.Cells[2, 3, listItems+1, 3];
ExcelRange timeLine = sheet.Cells[2, 2, listItems+1, 2];
sheet.Cells[8, 4].Formula = "=FORECAST.ETS(" + targetDate + "," + values + "," + timeLine + ",1,1)";
sheet.Cells[8, 4].Calculate();
I want to trim the # from the formula in the excel file, like this:
=FORECAST.ETS(B8,C2:C7,B2:B7,1,1)
In code you don't need to put the '=' character
Just use:
sheet.Cells[8, 4].Formula = "FORECAST.ETS(" + targetDate + "," + values + "," + timeLine + ",1,1)";
Similar Problem
I'm having a similar problem with a different formula:
worksheet.Cells[y, x].Formula = "SUMME(C5:C35)";
gave me =#SUMME(C5:C35) within Excel.
Instead of =SUMME(C5:C35).
Solution
It seems that EPPLUS has problems with the German formula. The problem disappears when I use:
worksheet.Cells[y, x].Formula = "SUM(C5:C35)";
My generated Excel now looks like this: =SUMME(C5:C35) (which is what I need)
I know it's not the answer to the exact problem mentioned above. But I thought it might help someone. And maybe serves as a hint to the original question.

EPPlus How to add LineSeries with specific range of data from worksheet

I create an Excel file in code. So far, everything works out fine.
Created excel file (image)
I want to create the following chart in code
Excel with Chart (image)
(Notice the selected cells and xseries names)
In Excel its easy.
But how do I do that in code?
My experiment
string values = "='Overall Results'!B3;'Overall Results'!D3;'Overall Results'!F3;'Overall Results'!H3;'Overall Results'!J3";
string xSerie = "='Overall Results'!$B$1:$K$1";
linechart.Series.Add(values, xSerie);
didn't work.
You have to create a string with the EPPlus cell adresses.
string values = worksheet.Cells[3, 2].Address + ":" + worksheet.Cells[3, 4].Address + ":" + worksheet.Cells[3, 6].Address;
linechart.Series.Add(values, ExcelRange.GetAddress(1, 2, 1, 11));
Had this problem aswell, but i found a solution:
For some reason, in order to get multiple specific cells in EPPlus, every cell has to be in a range.
So creating multiple ranges, which each only contains 1 cell, is the way to go
string values = "sheetName!B3:sheetName!B3,sheetName!D3:sheetName!D3,sheetName!F3:sheetName!F3,sheetName!H3:sheetName!H3,sheetName!J3:sheetName!J3";
var valueCells = sheet.Cells[values];
string xSerie = (Same concept);
var xCells = sheet.Cells[xSerie];
linechart.Series.Add(valueCells, xCells);

How to make part of a string Bold and add it to a excel Cell?

Hello i try to make a part of a string bold and then add it to an excel Cell. So it looks like:
What i tried:
Inside Excel using a range:
excelSheet.get_Range("A" + 16, "D" + 16).Font.Bold = true;
But this makes everything bold....
Then i tried:
"<b>" + text + "<b>"
and had no success.
So i make something wrong. Any help or advise would be great and thanks for your time.
EDIT: Working C# Code:
Excel.Range range1 = excelSheet.Range["A36"];
Excel.Characters test = range1.get_Characters(21, 4);
test.Font.Bold = true;
You can't make parts of a string bold, but you can make characters in a cell bold:
Sub BoldAndBeautiful()
With Range("A68")
.Value = "Test 1234 Test"
.Characters(Start:=1, Length:=4).Font.FontStyle = "bold"
.Characters(Start:=11, Length:=4).Font.FontStyle = "bold"
End With
End Sub
Basically do it in two steps. First put the text in the cell using the Value of the Range object and then apply the font using the Characters of the Range object.Note that some systems use an "HTML-type" method to format parts of a string, that is they embed markers to define where formatting starts and stops. Excel is not one of them.
Just adapt this to your c# code.

How to write long numbers as text with interop.excel

I am using interop.excel dll and creating an Excel file. I am having issues while writing long numbers as text.
Below is the code I am using. I can use apostrophe, but if you look in formula bar, you can see the cell is formatted.
Is there any other way of writing long numbers to an Excel spreadsheet without seeing the formatted value in the formula bar (using numberformat)?
worksheet.Cells[i, j] = "'" + dr[p];
//var startCell = (Excel.Range)worksheet.Cells[i, j];
//var endCell = (Excel.Range)worksheet.Cells[i, j];
//worksheetRange = worksheet.get_Range(startCell, endCell);
//worksheetRange.NumberFormat = "#####";
If you wanted to do it in one line, you could use the Text function.
Example:
worksheets.Cells[i, j] = "=Text(" + dr[p] + ", \"#\"";
But that probably isn't what you're looking for because this would store the number as a text and display the formula in the formula bar. In that case, like user194076 suggested, you could use .NumberFormat = "#";
or .NumberFormat = "#"; if you don't want it to show in scientific notation.
You can use NumberFormat with '#' or '#'
Example:
Range r= worksheet.Cells;
r.NumberFormat = "#";

The correct separator to use in Excel get_Range in VSTO

In a VSTO project for Excel written in C#, I need to get the Range object from a string list of cells.
Here is a simplified version of the problem:
string strRange = "A1:A2,A5";
Excel.Range r = sheet.get_Range(strRange);
However since the list separator can be different from the comma in different culture settings I'm actually using this:
listSep = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator;
string strRange = "A1:A2" + listSep + "A5";
Excel.Range r = sheet.get_Range(strRange);
My problem is when the user changes "Decimal Separator" in Excel Options > Advanced (the Application.DecimalSeparator) to match the ListSeparator, this won't work.
What is the correct way to call get_Range with a string specifying the Range?
EDIT: Slight modification to add information of my comment below.
Not the cleanest approach, but this workaround helped me:
private static string GetRangeSeparator(Excel.Worksheet sheet)
{
Excel.Application app = sheet.Application;
string sRng = app.Union(sheet.get_Range("A1"), sheet.get_Range("A3")).AddressLocal;
sRng = sRng.Replace("$", string.Empty);
string sSep = sRng.Substring(sRng.IndexOf("A3") - 1, 1);
return sSep;
}
Hope it'll help someone.
You might consider using the Application.Union method to build your range. Something like (untested):
Application.Union(sheet.get_Range("A1:A2"), sheet.get_Range("A5"));

Categories