0

I have an Winforms application that contains a DataGridView. The user has requested the ability to copy data to and from Excel.I have got the thing working exactly as I want it to, but there is one thing that happens when going from my DataGridView to Excel.

My question is this, is there any way to retain leading zeros on a string that contains only digits when pasting into Excel?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Jesse
  • 915
  • 1
  • 11
  • 20
  • Not sure if this will help you at all, but in excel, if you prepend the value with an apostrophe, it treats it as text, which retains the 0's. The apostrophe: ' – user1274820 Mar 19 '15 at 20:52
  • @user1274820 It seems to retain the apostrophe as well. I want to keep the text only. If the user pre-formats the cells as text and then say keep destination format when they paste then everything works, but I'm dealing with non-technical users here, so if possible I'd like to do this without requiring extra steps from them. – Jesse Mar 20 '15 at 13:39
  • Check here http://stackoverflow.com/questions/18182029/how-to-export-datagridview-data-instantly-to-excel-on-button-click I think you can use xlWorksheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true); – user1274820 Mar 20 '15 at 14:54
  • The problem/difference is that I'm not automating the paste. The user is actually right clicking and pasting into excel. I think that this is really just a short coming of Excel. They assume that if the Text can be parsed to a number then assume number. I would think that it would be more logical to assume that if there are leading zeros then the user probably wants to treat it as text. Oh well it doesn't appear that there is anything that I can do. My user will just have to learn how to deal with this in Excel. Thanks for the input. – Jesse Mar 23 '15 at 16:08

0 Answers0