Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

New Post: Cells.InsertDataTable Time values are recognized as String

$
0
0
I managed to find the solution to my query. But I did not utilize anymore the Cell.InsertData property. What I did was to parse each and every cell according to its data type before assigning it to a particular Excel cell.

For the time values, I used regular expressions to match, then used the boolean variable to parse and set the data type of the cell to either TimeSpan or DateTime depending on what I need.
 public void DataTableToExcel(DataTable dt, string usertemplate, string exceltab)
            {

            //open worksheet
            var workbook = new XLWorkbook(usertemplate);
            var worksheet = workbook.Worksheet(exceltab);

            //declare variables
            int totrow = dt.Rows.Count;
            int totcol = dt.Columns.Count;
            int numval;
            double dobval;
            DateTime dateval;
            TimeSpan timeval;

            //loop through each cells

                for (int i = 0; i < totrow; i++)
                    {
                    DataRow dataRow = dt.Rows[i];

                    for (int j = 0; j < dataRow.Table.Columns.Count; j++)
                        {
                        #region Regex 0:00:00
                        string input = dataRow[j].ToString();
                        string pattern = "^\\d{1}:\\d{2}:\\d{2}";
                        Match match = Regex.Match(input, pattern);
                        bool match1;
                        if (match.Success)
                            {
                            match1 = true;
                            }
                        else
                            {
                            match1 = false;
                            }
                        #endregion

                        #region Regex 00:00:00
                        string input2 = dataRow[j].ToString();
                        string pattern2 = "^\\d{2}:\\d{2}:\\d{2}";
                        Match match2 = Regex.Match(input2, pattern2);
                        bool match3;
                        if (match2.Success)
                            {
                            match3 = true;
                            }
                        else
                            {
                            match3 = false;
                            }
                        #endregion

                        if (int.TryParse(dataRow[j].ToString(), out numval))
                            {
                            var cell = worksheet.Cell(i+2, j + 1);
                            cell.Value = dataRow[j].ToString();
                            cell.DataType = XLCellValues.Number;
                            }
                        else if (double.TryParse(dataRow[j].ToString(), out dobval))
                            {
                            var cell = worksheet.Cell(i+2, j + 1);
                            cell.Value = dataRow[j].ToString();
                            cell.DataType = XLCellValues.Number;
                            }
                        else if (TimeSpan.TryParse(dt.Rows[i].ToString(), out timeval))
                            {
                            var cell = worksheet.Cell(i+2, j + 1);
                            cell.Value = dataRow[j].ToString();
                            cell.DataType = XLCellValues.TimeSpan;
                            continue;
                            }
                        else if (match1 == true) //0:00:00
                            {
                            string[] timeval1 = dataRow[j].ToString().Split(':');
                            TimeSpan t = new TimeSpan(Convert.ToInt16(timeval1[0].ToString()), Convert.ToInt16(timeval1[1].ToString()), Convert.ToInt16(timeval1[2].ToString()));
                            var cell = worksheet.Cell(i+2, j + 1);
                            cell.Value = t.ToString();
                            cell.DataType = XLCellValues.TimeSpan;
                            match1 = false;
                            }
                        else if (match3 == true) //00:00:00
                            {
                            string[] timeval1 = dataRow[j].ToString().Split(':');
                            int hour = Convert.ToInt16(timeval1[0].ToString());

                            if (hour <= 23)
                                {
                                TimeSpan t = new TimeSpan(Convert.ToInt16(timeval1[0].ToString()), Convert.ToInt16(timeval1[1].ToString()), Convert.ToInt16(timeval1[2].ToString()));
                                var cell = worksheet.Cell(i + 2, j + 1);
                                cell.Value = t.ToString();
                                cell.DataType = XLCellValues.TimeSpan;
                                }
                            else
                                {
                                WeirdDateTime d = new WeirdDateTime();
                                d = d.Parse(dataRow[j].ToString());
                                object a = d.ReturnValue;
                                var cell = worksheet.Cell(i + 2, j + 1);
                                cell.Value = a.ToString();
                                cell.DataType = XLCellValues.DateTime;
                                cell.Style.NumberFormat.NumberFormatId = 46;
                                }

                            match3 = false;
                            }
                        else if (DateTime.TryParse(dataRow[j].ToString(), out dateval))
                            {
                            var cell = worksheet.Cell(i + 2, j + 1);
                            cell.Value = dataRow[j].ToString();
                            cell.DataType = XLCellValues.DateTime;
                            }
                        else
                            {
                            var cell = worksheet.Cell(i+2, j + 1);
                            cell.Value = dataRow[j].ToString();
                            cell.DataType = XLCellValues.Text;
                            }
                        }
                    }

            workbook.Save();
            }

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>