Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorte a Datefield in ExcelSheet using C#

Posted on 2013-06-25
1
Medium Priority
?
217 Views
Last Modified: 2013-06-25
Hi Experts,

i try to sort an excelsheet based on a column polulated with date entries.

The problem seems to be, that the dates are handled as text.

What i want:
01.01.2013
02.01.2013
..
01.02.2013
02.02.2013

What i get:
01.01.2013
01.02.2013
..
02.01.2013
02.02.2013

The column (to be precise: The cells in the range to sort) is formated as a "Date" field in the worksheet.

Here is the Code i use:
Excel.Range range = oSheet.get_Range("A4:W" + iLastUsedRow, Type.Missing);

range.Sort(range.Columns[1, Type.Missing], Excel.XlSortOrder.xlAscending,Excel.XlYesNoGuess.xlNo,Type.Missing,Type.Missing,Excel.XlSortOrientation.xlSortColumns,Excel.XlSortMethod.xlPinYin,Excel.XlSortDataOption.xlSortTextAsNumbers);
 
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);

Open in new window


Any help is greatly appreciated!
BR
Tom
0
Comment
Question by:oczos
1 Comment
 

Accepted Solution

by:
oczos earned 0 total points
ID: 39274882
Now that i have written down the problem, i instantly see a possible answer:

This may have to do with localization?

Here in europe the date is generally of a "DD.MM.YYYY" format.

Since my routine changes globalization to "en-US" (otherwise the file wont open) maybe excel treats the date in a "MM.DD.YYYY" format and therefore the current result is "correct"?

UPDATE:
I have done a little testing and found out that the localization indeed was the problem.

When i chance it to "de-DE" (German) before sorting, it works like i need it.

Might not be the sexiest solution out there, but fits me needs.

Question closed
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

824 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