3

I am exporting a DataSet to excel that was generated by running an SQL script within my C# program. The DataSet correctly stores certain columns as datetimes, but from my understanding it does not store formatting information. It appears then that the formatting is given when call

ws.FirstCell().InsertTable(dt, false);

Which gives it the default format dd/mm/yyyy. Unfortunately I can't just select the whole range with ClosedXML and change the number format since come columns contain non-date numbers.

I suppose I could probably go through my data set, find which columns are date times, and format those columns accordingly with closedXML. I would imagine there has to be a better way though. Does anyone know an easy option for doing this with closed XML (or interop even)? I was unable to find anything on this.

EDIT:

Appears this is the only way at the moment... if anyone needs it (and manages to find this post) its easy enough to do:

                //select which columns are dates and change default format
                int colNum = 1;  
                foreach (DataColumn col in dt.Columns)
                {
                    if (col.DataType == typeof(System.DateTime))
                    {
                        Console.WriteLine(colNum);
                        ws.Column(colNum).Style.NumberFormat.Format = "mmm-dd-yyyy";
                    }
                    colNum++;
                }
                ws.FirstCell().InsertTable(dt, false);

0 Answers0