Excel cell value becomes DBNull when read with OLE - c#

I have an excel sheet that I am reading in.
One of the columns contians names of departments, like "$.30 stuff".
Notice how the value starts with a dollar sign.
This is a problem as all those values are consistently read in as DBNull.
In my connection string I have set
...;Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;ImportMixedTypes=Text;
So from my understanding the IMEX=1 should read values as text.
When I change the name to pure text without special Excel characters, the code works.
But is there a way to read in values with a leading dollar sign?

Related

Using Regex.Replace function in c# program to replace a string(data) with "data"?

I want to make use of Regex.Replace function to replace data in the format
05-11
to
"05-11"
so that excel can read it as a string.
Excel is converting the data to 05-Nov even though that particular column is defined as char.
In my application code, I have the below piece of code to replace any data that starts with a dash (-) with double quotes, "data"
var newString = Regex.Replace(data, #"^(-.*)$", "=\"$0\"");
How can I make use of this function to replace any data which are like
'05-11', '15-2019'
with
"05-11", "15-2019"
for the excel to read them as a string not as date format.
Unfortunately Excel does not accept " as an indicator of a text column, the only way to be sure is to proceed the value with a single quote.
var newstring = Regex.Replace(data, #"\b""?(\d\d-(?:\d\d)?\d\d)""?\b", "\"=\"\"$0\"\"\"");
This finds possibly double-quoted date strings that constitute the whole column value, and outputs it quoted with an equals sign and double quotes.
Unfortunately this special formatting is lost if you save as CSV from inside Excel and try to reload.
See this question for details.

EPPlus: Locale settings in cell value and formula

I want to export formulas from a C# application to an Excel Sheet using the EPPlus library.
The locale settings of the C# application as well as the Excel Application are identical, which means that the decimal separator is a comma in my case.
While the assignment
sheet.Cells[row, column].Value = value;
will correctly be used by Excel (for example if value is a double),
the same will not work with
sheet.Cells[row, column].Formula = formula;
where formula is a string beginning with "=" and containing string converted doubles correctly containing a comma as a decimal separator.
In the latter case, Excel will say that the document is broken, while it will accept formulas containing a dot as a decimal separator.
I do not want to start replacing commas with dots or vice versa, but keeping the correct locale chain into the Excel formula.
Can this be done somehow? I think I need a setting through EPPlus to tell Excel that the locale setting in the formula is the same as in the normal cell content (where everything works as expected).
The localization option available in EPPlus is NumberFormat property to set your desired number format.
In your case, you could try:
sheet.Cells[row, column].Numberformat.Format="#,##0.00"; //set your format of dots & commas

Can we create format file from bcp command line as a different field terminator for each field?

We are using non-XML format file to import data into Sql Server database through bcp utility. We have used comma(,) as a field separator in the bcp.
Now we are getting issue when user provides comma(,) between the string field like name. Field get terminates by comma(,) and rest of the words goes in to the next field. We can not change the separator at this point of time. So we are looking for some other solutions.
We found a way that users will enclose all the string values columns in double quotes. we can scan first ten rows of the file and see if there is a double quote(“) in the line. If a double quote is found we have to update the format file so that doubles quotes get treated as text qualifiers and everything with in double quotes are treated as a column value.
So my question is can we code this programmatically if we find double quotes we can make field separator as ",\"" and then for the next record the terminator will be "\"," and if there will be no double quote we can keep our terminator as ",".
Or simply I want to ask how can we create different field terminator for different fields ? I went through some goggling and found in the bcp command line we have only -t that creates the field terminator and this will keep the same for all the fields.

Excel adds extra quotes on CSV export

I've recently created an application which adds items to a Database by CSV. After adding items I realized that lots of my values had extra quotes (") that weren't needed and this was messing up my ordering.
The problem is that when exporting to a CSV from Excel, Excel adds extra quotes to all of my values that already have a quote in them. I've shown the difference below:
Original Item: Drill Electric Reversible 1/2" 6.3A
Exported Item: "Drill Electric Reversible 1/2"" 6.3"
Note: the CSV export is adding three (3) extra quotes ("). Two on the ends, and one after the original intended quote.
Is there a setting I can change, or a formatting property I can set on the Excel File/Column? Or do I have to live with it and remove these quotes in my back-end code before adding them to the Database?
This is entirely normal. The outer quotes are added because this is a string. The inner quote is doubled to escape it. Same kind of thing you'd see in a SQL query for example. Use the TextFieldParser class to have tried and true framework code care of the parsing of this for you automatically.
That's standard.
The values within a CSV file should have quotes around them (otherwise commas and linebreaks inside a field may be misinterpreted).
The way to escape a quote within a field is to double it, just as you are seeing.
I suggest you read about the basic rules of CSV:
CSV is a delimited data format that has fields/columns separated by the comma character and records/rows terminated by newlines. Fields that contain a special character (comma, newline, or double quote), must be enclosed in double quotes. If a line contains a single entry which is the empty string, it may be enclosed in double quotes. If a field's value contains a double quote character it is escaped by placing another double quote character next to it. The CSV file format does not require a specific character encoding, byte order, or line terminator format.
(emphasis mine)
You could try exporting from Excel as TAB delimited files. I find it easier to parse.
Replace all characters Right Double Quotation Mark by characters Left Double Quotation Mark. They look similar, Excel will be confused and let the text unchanged.
This solution will only help if your end output is HTML. This is the javascript solution so obviously you'll need to redo this in C# or whichever language you're working in:
base = base.replace(/""/gi, '"');
base = base.replace(/'/gi, ''');
Apply this before you parse the CSV.
Another approach would be to use the Unicode Character "DOUBLE PRIME"
http://www.fileformat.info/info/unicode/char/2033/index.htm
in your Excel data. To export from Excel into a UTF-8 or UTF-16 .csv you'll have to provide a schema.ini with an appropriate CharacterSet property. Obviously, the tool you use to import the .csv into your database has to be Unicode aware too.
Depending on the DBMS a more direct way of data transfer (SELECT/INSERT ... INTO ... IN ) can be used, thereby eliminating the .csv entirely.

access to excel conversion

I have an access database in which the field type is text and it contains 26 numbers. I converted it to excel, but it has only the first 5 numbers
Text in database: 91234612345678912345678912
Text in excel : 9.12346E+25
How to get the entire field from database to excel?
If you are performing the export yourself, you could prefix the field with an apostrophe (') to force Excel to treat the field as text rather than a number. Unfortunately, this has the side-effect of not allowing mathematical operations to be performed on the field, as it is no longer a number. It also puts that annoying green triangle in the upper-left of the cell warning you that you have converted a number to text.
Unless you explicitly specify the format of the excel cell to be "Text" it will be treated as a number by excel.
When converting the database value make sure to set the "Cell Formatting" to "Text", alternatively if you can paste some sample code then may be someone can refine it and help.
HTH
can't you export it as it is, and then highlight the column and change the type to text? Won't that convert it to the right value?

Categories

Resources