Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sorte a Datefield in ExcelSheet using C#

Posted on 2013-06-25
1
207 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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