Hi I've called the text to column function in C# but it didn't turned out the way I wanted it to be.
I have this data in the first cell of my worksheet.
Guest;0;12/10/2010 03:46:34 PM;66082
If I run Text to Column manually from Excel, I get.
Guest 0 12/10/2010 15:46 66082
However if I ran through my C# code, I get.
Guest 0 10/12/2010 15:46 66082
The Date format have switched from "DD/MM/YYYY" to "MM/DD/YYYY"
Here's my C# Code
((Range)ws.Cells[1,1]).EntireColumn.TextToColumns(
Type.Missing, Excel.XlTextParsingType.xlDelimited,
Excel.XlTextQualifier.xlTextQualifierNone, Type.Missing,
Type.Missing, true, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
I even recorded the Macro from Excel.
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1))
The only thing that I couldn't try out would be FieldInfo. Had no idea how to convert it to C#. Not much help from Google either. I'm kinda suspecting it is the xlGeneralFormat.
Any Ideas? Thanks.
object fieldInfo = new int[4,2] {{1,1},{2,1},{3,1},{4,1}};
Ok I realised that the problem does not lies with the FieldInfo, it runs fine even if I omit it out.
The problem lies with this.
wb.Close(true, saveFileDialog1.FileName, Type.Missing);
I saved it into another file. And found that the original file was in the correct format I wanted. But the newly saved file was in the wrong format.
I commented out the close line, and saved it manually. The old file is still in delimited form while the new file is still in the wrong format.
Apparently object FileName only works if it's "saveFileDialog1.FileName". In other words it works only if it's the same file name. I tried Type.Missing and @"C:\lalala.xls" and it didn't text to column.
I just realise the format in Text to column doesn't even work -.- Ok I give up. I'm going to convert DateTime to Excel Serial DateTime. An interesting problem nonetheless.