Solved

CSV file strips off leading 0 when reading in Excel.

Posted on 2007-03-19
4
474 Views
Last Modified: 2013-12-17
I have an Excel spreadsheet that I save as a CSV file.  There is one value that I save with a zero padding in the beginning of the field.  When I load up the CSV file in Excel it strips off the leading 0.  But if I look at it in notepad it is there.  The problem I have is that when I try to read it in Excel Automation (using c#) it very much acts as it does when opening the file in Excel, i.e., stripping off the leading 0.  The field is a key to a database table and will not work without the leading 0.  Is there a way in Excel or preferrably C# to read the true value?

Thanks
0
Comment
Question by:techhound
  • 2
4 Comments
 
LVL 18

Expert Comment

by:p912s
ID: 18751041
>>in notepad it is there
Then it is there...

How are you reading it with C#? When I read files using VB it reads them with the leading 0. Or are you using an Excel object to open the file?

Look at the solution in the following link. It used the FSO to read thru a file a line at a time.

  http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_22455373.html

0
 

Author Comment

by:techhound
ID: 18751142
I am using Excel to read the file.
0
 
LVL 18

Expert Comment

by:p912s
ID: 18751211
>>I am using Excel to read the file.
Is the data generate4d in Excel? You can change the data to be '0001 in Excel to retain leading 0's.
0
 
LVL 9

Accepted Solution

by:
BTognietti earned 500 total points
ID: 18752160
Assuming Column A is the affected column, add this to your Excel workbook;

Private Sub Workbook_Open()
    Columns("A:A").Select
    Selection.NumberFormat = "@"
End Sub

To add the Sub;

Open the workbook > Alt + F11 > Double-click ThisWorkbook from the Left Pane > Paste the code

Good luck,

Bud
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question