0

I have a WPF that reads an excel file and removes hidden characters and writes the data as pipe delimited to a text file. Works pretty good. Now I am trying to keep the cell formatting that excel has. For test purposes I have a zip code field in excel. The cell has value 047300134 and a custom format of 00000-0000. I would like the data written to be 04730-0134. The only process I saw that using String.Format. Here is how I tried to code it.

dfCellFormat = range[NumRow, lngColCnt].DisplayFormat;
strNumberFormat = dfCellFormat.NumberFormat;
strCellData = Convert.ToString(values[NumRow, lngColCnt]);
strNumberFormat = "{0:" + strNumberFormat.Replace("0","#") + "}";
strCellData = String.Format(strNumberFormat, strCellData);

The result is just 047300134, the format is not applied.

Is there a way to accomplish what I am trying to do?

Thank you

Cass
  • 537
  • 1
  • 7
  • 24
  • I don't know what happened but someone had sent this link to me, http://stackoverflow.com/questions/8929799/how-to-get-cell-value-with-applied-formatting-formatted-cell-value-with-openxm/8955747#8955747. Now I do not see that comment. Anyway that was exactly what I needed. Thank you! – Cass Jun 30 '16 at 16:51
  • Can anyone tell me why this statement dfCellFormat = range[NumRow, lngColCnt].DisplayFormat; causes my process to be very slow? I have a file that has 2500 rows and 67 columns. Without this code it takes seconds, with it minutes, like 5. Any ideas? – Cass Jun 30 '16 at 17:34
  • I found this link http://stackoverflow.com/questions/8929799/how-to-get-cell-value-with-applied-formatting-formatted-cell-value-with-openxm/8955747#8955747 that solved my issue. – Cass Jul 08 '16 at 11:39

0 Answers0