Suppress formatting characters in Excel in C# - c#

I am building an Excel sheet in C# and there is one field that gets converted to scientific notation and I want it to stay text. When I prepend the value with '=""' it displays it like this 20211104075106.399=" ", which is the correct value but I don't want the =" " at the end. This is how I am adding it to the value:
excelWorksheet.Cells[intRow, 5] = string.Join("=\" ", row[4].ToString(), "\"");
Is there any way to suppress that? Thanks.

To force text formatting to a cell, you prepend the value with an apostrophe
excelWorksheet.Cells[intRow, 5] = $"'{row[4]}";

Related

ClosedXML Adding Formula creates unreadable content

I am adding a formula to worksheet using C# closedxml but it is coming back as unreadable content. I think the reason is the slashed that I added to the formula but I need the slashes to escape the quotes because I need the quotes in the formula. How else can I do this?
Here is my code:
CodeWorksheet.Range(CodeWorksheet.Cell(2, 25).Address, CodeWorksheet.Cell(CodeWorksheet.LastRowUsed().RowNumber(), 25).Address).FormulaR1C1 = "=IF(SUM(RC[-6]:RC[-1])>0,\" + \",\")";
I think you only have one double quote in the "if false" part of your formula. Try changing
=IF(SUM(RC[-6]:RC[-1])>0,\" + \",\")
to
=IF(SUM(RC[-6]:RC[-1])>0,\" + \",\"\")

C# Excel interop, put a line break in the text of a cell in a Excel

I am writing an Excel sheet using interop. In the sheet I need to put a set of sentences in to a cell. The text should be in a line break manner. How can I achieve this?
Thanks in advance.
It was done by either entering "\r\n" or Environment.NewLine. And also must remember to make WrapText property true in order to make the line breaks visible.
you can add style to cells programically as bellow
worksheet.Cells.Style.WrapText = true;
New line within an Excel cell is the LF character, which is "\n" in C#. And do not forget to set the WrapText property of the cell to TRUE.
New line within an Excel cell is the LF character, which is "\n" in C#.
In VB, or VBA, I used to use vbCrLf (case sensitive) to separate sentences to separate lines in an Excel's Cell like the following:
Dim myString As String
myString = "First sentence" & vbCrLf & "Second sentence"
ActiveCell.ForumulaR1C1 = myString
In C#, I am rather confident that the C# equivalent of VB's vbCrLf is "\r\n", hence:
myString = "First sentence\r\n" + "Second sentence"
I have encountered this problem but difference is I do not have access to the worksheet in code as I only pass it to memorystream then create file as .csv type.
foreach (var s in propertyValues)
streamWriter.WriteLine(s);
streamWriter.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
the I use it from here.
subjExportData.FileStream = stream;
subjExportData.FileName = string.Format("SubjectExport_{0}.csv", DateTime.Now.ToString("ddMMyyyy_HHmm"));
So the suggestion here to set text or cell wrap is not an option. Got it working using the above answer plus put a double quote before and after the text/string. The replace is to handle when the sentence has a double quote too inside it. So this solution handles, new line, comma and double quotes inside a sentence or paragraph.
if (value.ToString().Contains("\n"))
{
value = value.ToString().Replace("\n", "\r\n");
sb.Append('"'+ value.ToString().Replace(#"""", #"""""") + '"'+ ",");
}
the answer of this issue is to add a "quot" in front and at the end of the string you want to display in your excel cell. In C# it would be something like (Convert.ToChar(34) + stringToExport + Convert.ToChar(34))

how to display leading zeros programmatically

this field has leading zeros.
newSheet.Cells[i + 2, 5] = drugorder.NDC;
the code is seeing it when it looks up the NDC description in the SQL Server table. But it doesnt display when we write this col to the new sheet in Excel.
how can we display the leading zeros?
Did you try drugorder.NDC.ToString("0000") ?
You can set the number format of the cells in question to be Text. This will preserve the leading zeroes. Take the Range object in question and set its NumberFormat property to be #

How make xml to not auto replace ampersand characters in ASP.NET

There is a method wich takes the *.xml template made with Excel formatting and insert some text into it.
When I'm inserting text with \r\n symbols, Excel ignores the brakes and write all in one line. It turns out, that Excel needs the "
" in xml instead of "\r\n". So i'm trying to replace
NewText = NewText.Replace("\r\n", "
").Replace("\n", "
");
node["Data"].InnerText = NewText;
But then I see, that all the "
" are implicitly changed with "&amp ;#10;" by XmlDocument.
What should I do to save xml with "
" in it?
Use CDATA
http://www.w3schools.com/xml/xml_cdata.asp

How to make Excel with zero before the number in C#

I make from my program an excel file (xls or csv).
I send 00123 and in Excel I see 123
How I can send and see 00123
Thanks in advance
Its because excel is treating the data as 'numeric'. A simple way to force Excel to accept anything as text is to prepend an apostrophe to the text.
e.g. to write an integer 123 as 00000123 just write:
ActiveCell = "'" & Format(123, "00000000")
EDIT: Another solution is to set the Cells NumberFormatProperty to text:
Worksheet.GetRange(..).EntireColumn.NumberFormat = "#"
You might want to see this article: Excel Cell Auto Format
In C# to see the CSV with the padding use
myVar.PadLeft(5,'0')
In Excel set the number format to custom 00000 or ZipCode

Categories

Resources