View CSV file date as raw string in Excel - excel

I have a CSV file which has time column and its values are String in dd MMM HH:mm format.
Name,Time
Farms,20 Dec 14:00
When I open the CSV file with Excel, Excel automatically converts these Timevalues to date format, thereby modifying to add year value on its own. So 20 Dec 14:00 is converted to 20/12/22 14:00.
What I want is that Excel should display the time column values as raw string, i,e. 20 Dec 14:00.
Some similar questions, suggetsed to modify the CSV file by adding = symbol before the time column values, so that excel treat it as string. But this is not what I want.
I don't want to make any changes to CSV file, but want a way by making some changes in Excel itself.

Instead of opening the .CSV file by directly double-clicking, or dragging it onto an EXCEL instance, try using File->Open method, and use the text import Wizard, choose the comma delimiter, and when when it asks for the type of the column, choose text (you can choose multiple columns and choose text).
Alternatively, create a new empty worksheet, format the first column as text, open the file in a good text editor (like Notepad++), copy whole text (Ctrl-A, Ctrl-C), Alt-tab to EXCEL, place the cursor in A1, and paste (Ctrl-V). Now you can use the Data->Text to Columns wizard, and again you can choose text as type.
It can also be done in VBA, but performing the above steps are acceptable then why.

Related

Excel keeps changing my text values to date in my CSV file

I have an exported CSV file that needs to be reimported.
It has an address column having data like -
01-03, 08-01, 01-05
Upon opening the file in excel, it reads these values like dates.
I changed the column format to text after extracting the values into text as =TEXT(column,"dd-mm") and pasting the values, which shows the correct value 01-05.
However, when I close and re-open the file they are converted back to Dates again as 16-Mar, 01-May etc.

Excel: date formatting

I downloaded a csv file with dates in one column. The dates are formatted dd/mm/yyyy, but when i open the file Excel auto formats the dates it recognises as an actual date and leaves dates it doesn't recognise as a text.
So for example, 01-01-2010 would be 1 January 2010, but excel reads it as January 1 2010 and formats it as such. Something like 30-07-2010 is not recognised and is formatted as general text.
So now I have groups of rows which are formatted to dates as dd/mm/yyyy and groups of rows that are simply xx-xx-xxxx.
Is there any way that can I easily convert stuff like 30-07-2010 to something excel recognises as a date input?
The file I have is downloaded and there is no way to select a file that formats dates as mm-dd-yyyy
Thanks a lot!
Don't open the CSV file with Excel. Create a new Excel spreadsheet first and then go to the Data Tab --> Get External Data --> From Text.
Find the file in question and open it. In Step 3 the import wizard will ask you for the Column Data Format. Select the 'Date' radio button and in the drop-down to the right, select "MDY". Click Finish to import.

Saving to CSV in Excel loses regional date format

I have a .xls I need to convert to .csv
The file contains some date columns. The format on the date is "*14/03/2001" which, according to Excel means the date responds to regional date and time settings specified for the OS.
Opening in Excel you see:
20/01/2013
01/05/2013
Save as... CSV
Open in notepad:
01/20/2013
05/01/2013
I have temporarily fixed by setting date formats to "14/03/2001" (no *) but even some other custom formats with no *, like "d/mm/yyyy h:mm" get mangled when saved to CSV.
Although keeping this in mind http://xkcd.com/1179/
In the end I decided to use the format YYYYMMDD in all CSV files, which doesn't convert to date in Excel, but can be read by all our applications correctly.
You can save your desired date format from Excel to .csv by following this procedure, hopefully an excel guru can refine further and reduce the number of steps:
Create a new column DATE_TMP and set it equal to the =TEXT( oldcolumn, "date-format-arg" ) formula.
For example, in your example if your dates were in column A the value in row 1 for this new column would be:
=TEXT( A1, "dd/mm/yyyy" )
Insert a blank column DATE_NEW next to your existing date column.
Paste the contents of DATE_TMP into DATE_NEW using the "paste as value" option.
Remove DATE_TMP and your existing date column, rename DATE_NEW to your old date column.
Save as csv.
Change the date and time settings for your computer in the "short date" format under calendar settings. This will change the format for everything yyyy-mm-dd or however you want it to display; but remember it will look like that even for files saved on your computer.
At least it works.
You need to do a lot more work than 1. click export 2. Open file.
I think that when the Excel CSV documentation talks about OS and regional settings being interpreted, that means that Excel will do that when it opens the file (which is in their "special" csv format). See this article, "Excel formatting and features are not transferred to other file formats"
Also, Excel is actually storing a number, and converting to a date string only for display. When it exports to CSV, it is converting it to a different date string. If you want that date string to be non-default, you will need to convert your Excel cells to strings before performing your export.
Alternately, you could convert your dates to the number value that Excel is saving. Since that is a time code, it actually will obey OS and regional settings, assuming you import it properly. Notepad will only show you the 10-digit number, though.
If you use a Custom format, rather than one of the pre-selected Date formats, the export to CSV will keep your selected format. Otherwise it defaults back to the US format
Place an apostrophe in front of the date and it should export in the correct format. Just found it out for myself, I found this thread searching for an answer.
A not so scalable fix that I used for this is to copy the data to a plain text editor, convert the cells to text and then copy the data back to the spreadsheet.
Change the date range to "General" format and save the workbook once, and change them back to date format (eg, numberformat = "d/m/yyyy") before save & close the book. savechanges parameter is true.
You can send your date to excel not like the "date" but like just a "string" with empty space at the start and end of the string.
Your problem is happening due to excel see your '11/02/2023' like a date, but you can hack it and change this date to a simple string like ' 11/02/2023 '.
Old: '11/02/2023'
New (solution): ' 11/02/2023 '

concatenate long strings of text for uploading on website

We have used the concatenate function on excel to add a string of text on excel. We are trying combine around 10 fields with a total of 300 characters. The concatenate function works on excel and once we paste the values to remove the formula, the correct text strings are created. However, the problem arise when we try to use this information as a CSV, tab delimited file to import into our webstore, the concatenate text is not recognised. When we inspect the format of the cell, the characters are displayed as a bunch of ############, rather than the text and I believe this is the reason why it is not allowing us to import the file. Small text strings work, however long strings do not work. We have to use open calc spreadsheet to concatenate, however this has the same problem. We have saved the file at UT8.
You shouldn't have a cell to inspect if you are saving as a csv. You should have a text file to examine. You also don't need to paste the formulas as values, as none of the formulas will remain after you save as a text file.
After you get the Excel file ready with the concatenate formulas, save the sheet as either a tab-delimited .txt or comma-delimited .csv.
Open it up in notepad to verify that the values are still what you expect them to be rather than the ##### characters.
At that point, your import should work.

Excel CSV - Number cell format

I produce a report as an CSV file.
When I try to open the file in Excel, it makes an assumption about the data type based on the contents of the cell, and reformats it accordingly.
For example, if the CSV file contains
...,005,...
Then Excel shows it as 5.
Is there a way to override this and display 005?
I would prefer to do something to the file itself, so that the user could just double-click on the CSV file to open it.
I use Excel 2003.
There isn’t an easy way to control the formatting Excel applies when opening a .csv file. However listed below are three approaches that might help.
My preference is the first option.
Option 1 – Change the data in the file
You could change the data in the .csv file as follows ...,=”005”,...
This will be displayed in Excel as ...,005,...
Excel will have kept the data as a formula, but copying the column and using paste special values will get rid of the formula but retain the formatting
Option 2 – Format the data
If it is simply a format issue and all your data in that column has a three digits length. Then open the data in Excel and then format the column containing the data with this custom format 000
Option 3 – Change the file extension to .dif (Data interchange format)
Change the file extension and use the file import wizard to control the formats.
Files with a .dif extension are automatically opened by Excel when double clicked on.
Step by step:
Change the file extension from .csv to .dif
Double click on the file to open it in Excel.
The 'File Import Wizard' will be launched.
Set the 'File type' to 'Delimited' and click on the 'Next' button.
Under Delimiters, tick 'Comma' and click on the 'Next' button.
Click on each column of your data that is displayed and select a 'Column data format'. The column with the value '005' should be formatted as 'Text'.
Click on the finish button, the file will be opened by Excel with the formats that you have specified.
Don't use CSV, use SYLK.
http://en.wikipedia.org/wiki/SYmbolic_LinK_(SYLK)
It gives much more control over formatting, and Excel won't try to guess the type of a field by examining the contents. It looks a bit complicated, but you can get away with using a very small subset.
This works for Microsoft Office 2010, Excel Version 14
I misread the OP's preference "to do something to the file itself." I'm still keeping this for those who want a solution to format the import directly
Open a blank (new) file (File -> New from workbook)
Open the Import Wizard (Data -> From Text)
Select your .csv file and Import
In the dialogue box, choose 'Delimited', and click Next.
Choose your delimiters (uncheck everything but 'comma'), choose your Text qualifiers (likely {None}), click Next
In the Data preview field select the column you want to be text. It should highlight.
In the Column data format field, select 'Text'.
Click finished.
You can simply format your range as Text.
Also here is a nice article on the number formats and how you can program them.
Actually I discovered that, at least starting with Office 2003, you can save an Excel spreadsheet as an XML file.
Thus, I can produce an XML file and when I double-click on it, it'll be opened in Excel.
It provides the same level of control as SYLK, but XML syntax is more intuitive.
Adding a non-breaking space in the cell could help.
For instance:
"firstvalue";"secondvalue";"005 ";"othervalue"
It forces Excel to treat it as a text and the space is not visible.
On Windows you can add a non-breaking space by tiping alt+0160.
See here for more info: http://en.wikipedia.org/wiki/Non-breaking_space
Tried on Excel 2010.
Hope this can help people who still search a quite proper solution for this problem.
I had this issue when exporting CSV data from C# code, and resolved this by prepending the leading zero data with the tab character \t, so the data was interpreted as text rather than numeric in Excel (yet unlike prepending other characters, it wouldn't be seen).
I did like the ="001" approach, but this wouldn't allow exported CSV data to be re-imported again to my C# application without removing all this formatting from the import CSV file (instead I'll just trim the import data).
I believe when you import the file you can select the Column Type. Make it Text instead of Number. I don't have a copy in front of me at the moment to check though.
Load csv into oleDB and force all inferred datatypes to string
i asked the same question and then answerd it with code.
basically when the csv file is loaded the oledb driver makes assumptions, you can tell it what assumptions to make.
My code forces all datatypes to string though ... its very easy to change the schema.
for my purposes i used an xslt to get ti the way i wanted - but i am parsing a wide variety of files.
I know this is an old question, but I have a solution that isn't listed here.
When you produce the csv add a space after the comma but before your value e.g. , 005,.
This worked to prevent auto date formatting in excel 2007 anyway .
The Text Import Wizard method does NOT work when the CSV file being imported has line breaks within a cell. This method handles this scenario(at least with tab delimited data):
Create new Excel file
Ctrl+A to select all cells
In Number Format combobox, select Text
Open tab delimited file in text editor
Select all, copy and paste into Excel
Just add ' before the number in the CSV doc.
This has been driving me crazy all day (since indeed you can't control the Excel column types before opening the CSV file), and this worked for me, using VB.NET and Excel Interop:
'Convert .csv file to .txt file.
FileName = ConvertToText(FileName)
Dim ColumnTypes(,) As Integer = New Integer(,) {{1, xlTextFormat}, _
{2, xlTextFormat}, _
{3, xlGeneralFormat}, _
{4, xlGeneralFormat}, _
{5, xlGeneralFormat}, _
{6, xlGeneralFormat}}
'We are using OpenText() in order to specify the column types.
mxlApp.Workbooks.OpenText(FileName, , , Excel.XlTextParsingType.xlDelimited, , , True, , True, , , , ColumnTypes)
mxlWorkBook = mxlApp.ActiveWorkbook
mxlWorkSheet = CType(mxlApp.ActiveSheet, Excel.Worksheet)
Private Function ConvertToText(ByVal FileName As String) As String
'Convert the .csv file to a .txt file.
'If the file is a text file, we can specify the column types.
'Otherwise, the Codes are first converted to numbers, which loses trailing zeros.
Try
Dim MyReader As New StreamReader(FileName)
Dim NewFileName As String = FileName.Replace(".CSV", ".TXT")
Dim MyWriter As New StreamWriter(NewFileName, False)
Dim strLine As String
Do While Not MyReader.EndOfStream
strLine = MyReader.ReadLine
MyWriter.WriteLine(strLine)
Loop
MyReader.Close()
MyReader.Dispose()
MyWriter.Close()
MyWriter.Dispose()
Return NewFileName
Catch ex As Exception
MsgBox(ex.Message)
Return ""
End Try
End Function
When opening a CSV, you get the text import wizard. At the last step of the wizard, you should be able to import the specific column as text, thereby retaining the '00' prefix. After that you can then format the cell any way that you want.
I tried with with Excel 2007 and it appeared to work.
Well, excel never pops up the wizard for CSV files. If you rename it to .txt, you'll see the wizard when you do a File>Open in Excel the next time.
Put a single quote before the field. Excel will treat it as text, even if it looks like a number.
...,`005,...
EDIT: This is wrong. The apostrophe trick only works when entering data directly into Excel. When you use it in a CSV file, the apostrophe appears in the field, which you don't want.
http://support.microsoft.com/kb/214233

Resources