wrting double problem to excel - c#

i have a text and i try to write this text to excel file by c#. this text include not one kind of value. i mean, it can be include "abc" "1.6", "1/6" etc. when i write 1.86 or 1/86 i see "feb.86" in the excel. but i want to see what i write to. what can i do to solve this problem?

for numbers like 1/86 you can prefix it with '. But then you will get "Number Stored as Text" error in excel (little green triangle in cell). Otherwise you can always put things in double quotes to work:
="1/86"
PS:- procede with "="

Related

Field and text delimiters within cells in csv files

This is likely a very basic question that I could not, despite trying, find a satsifying answer to. Feel free to skip to the question at the end if you aren't interested in the background.
The task:
I wish to create an easy localisation solution for my unity projects. After some initial research I concluded it would be best to use a .csv file read by a streamreader, so that translators would only ever have to interact with the csv table, where information is neatly organized.
The main problem:
Due to the nature of the text, I need to account for linebreaks and special characters in the actual fields. As such I could not use the normal readLine() method.
This I worked with by using Read() and checking if a linebreak is within a text delimiter bracket. But as I check for the text delimiter, I am afraid it might run into an un-escaped delimiter part of the normal in-cell text (since the normal text delimiter is quotation marks).
So I switched the delimiter to §. But now every time I open the file I have to re-enter § as a text delimiter in OpenOfficeCalc, probably due to encoding differences. Which is annoying but not the end of the world.
My question:
How does OpenOffice (or similar software) usually tell in-cell commas/quotation marks apart from the ones used as delimiters? If I knew that, I could probably incorporate a similar approach in my reading of the file.
I've tried to look at the files with NotePad++, revealing a difference in linebreaks (/r instead of /r/n) and obviously it's within a text delimiter bracket, but when it comes to how it seperates its delimiters from ones just entered in the text/field, I am drawing a blank.
Translation file in OpenOffice Calc:
Translation file in NotePad++, showing all characters:
I'd appreciate any insight or links on the topic.
From https://en.wikipedia.org/wiki/Comma-separated_values:
The CSV file format is not fully standardized. The basic idea of separating fields with a comma is clear, but that idea gets complicated when the field data may also contain commas or even embedded line breaks.
LibreOffice Calc has a reasonable way to handle these things.
Use LF for line breaks and CR at the end of each record. It seems your code already handles this.
Use quotes to delimit strings when needed. If the string contains one or more quotes, then duplicate the quote to make it literal.
From the example in your question, it looks like you told Calc not to use any quotes as string delimiters. Why did you do this? When I tried it, LibreOffice (or Apache OpenOffice) showed the fields in different columns after opening the file saved that way.
The following example CSV file has fields that contain commas, quotes and line breaks.
When viewed in Calc:
A B
--------- --
1 | 1,",2", 3
--------- --
2 | a c
| b
Calc correctly reads and saves the file as shown below. Settings when saving are Field delimiter , and String delimiter " which are the defaults.
"1,"",2"",",3[CR]
"a
b",c[CR]

Excel formula with double quote with C#

I would like to write a formula in a Excel cell with SpreadSightLight in C#.
I have a following formula in excel
=COUNTIFS(Sheet2!E:E, D2, Shet2!A:A, ">0")
In the code it looks
"=COUNTIFS({0}!E:E,{1},{0}!A:A ,\">0\")";
I save the file from the code, but when i try to open the file Excel (2013) gives error message and the formula is removed
Removed Records: Formula from /xl/worksheets/sheet3.xml part
and the sheet3.xml contains the following line
COUNTIFS(Sheet2!E:E, D2, Sheet2!A:A ,">0")
I tried the verbatim string and the char(34) solutions also but causes the same error.
How can I solve this problem?
Thanks in advance.
I have not succeed to find the reason why there can be no double quotes written into an excel formula. However there is a solution to avoid double quotes:
string formula = "=COUNTIFS({0}!E:E, {1}, {0}!A:A , CONCATENATE(CHAR(62), CHAR(48)))";
In case anybody subsequently comes across this thread, I believe the problem is the greater than sign. It's getting confused with the end tag in XML. So it needs to be replaced by &gt (as hinted at by what you said your sheet3.xml contains).
I think the double quotes are fine as you have them - escaped with a preceding backslash.

Copy numeric codes to clipboard and paste to Excel without having them formatted as numbers

I have a .NET Windows Forms applications and I need to copy a list of 8-digit numeric codes into the clipboard to be pasted to Excel sheet.
string tabbedText = string.Join("\n", codesArray);
Clipboard.SetText(tabbedText);
The problem is that when a code begins with one or more zeros (ex. "00001234") it's pasted as number with the zeros trimmed.
Is there a way how to set clipboard text so that Excel accepts it as text?
I would treat this problem inside of Excel (and not in your application programaticaly). Format your cells to be treated as text, and then paste from clipboard. This way leading zeros are always pasted.
EDIT: This doesn't work in Excel, in that the apostrophe gets pasted in and shows up too. I'm leaving the answer here as an explicit statement that this approach won't help for Excel.
It does work for OpenOffice Calc though.
The standard way to 'tell' Excel to treat a string as a string is to prefix it with an apostrophe. Have you tried something like:
string tabbedText = "'" + string.Join("\n'", codesArray);
(note the extra apostrophe in there... it's a bit hard to see).
Of course, this may cause you issues if you're planning to use this value thereafter in Excel calculations but there are ways to handle that too.

C# Unknown Text Found

I'm creating a program to transfer text from a word document to a database. During some testing I came across some text inside a textbox after setting it's text to a table cell range as follows:
textBox1.Text = oDoc.Tables[1].Cell(1, 3).Range.Text;
What appeared in the form was:
What wasn't expected was the dot at the end of the text and I have no idea what it is supposed to represent. The dot can be highlighted but if you try and copy and paste it nothing appears. You can delete the dot manually. Can anyone help me identify what this is?
The identification bit shouldn't be too hard:
string text = oDoc.Tables[1].Cell(1, 3).Range.Text;
textBox1.Text = ((int) text[4]).ToString("x4");
That will give you the Unicode UTF-16 code unit for that character... you can then find out what it is on the Unicode web site. (I usually look at the Charts page or the directory of PDFs and guess which chart it will be in based on the numbering - it's not ideal, and there are probably better ways, but it's always worked well enough for me...)
Of course when you've identified it you'll still need to work out what the heck it's doing there... does the original Word document just have "HOLD"?

Issue in exporting data to CSV

I am doing export of data from database to Excel. After exporting when opening the csv file, the excel application is autoformating the values in excel.
One my cell contained the value -A1177, but it got converted to zero.
Can any one let me know is there any solution to avoid this or switch off this autoformat in excel.
thanks.
The easiest way to do it, is to simply wrap the value in quotes, e.g. asdasd,"-A1177",11/03/1984 in your CSV file. You can also change the format of the column to `Text'.
Otherwise, check out this excellent Stack Overflow Question and Answer
Change column Data Format when importing the text change it to Text
Insert a ' at the start of any formula in Excel and it will be considered a string.
So if possible, check when generating the CSV file if the first char in a given field can be understood as an Excel formula "opener" (off my mind {=, -, +}) and prepend a '.
You can do it for every field, but its only strictly necessary if the text can be misunderstood as a formula.

Categories