Замена строки, содержащей дату и время, двойными кавычками с префиксом = (знак равенства) - PullRequest
0 голосов
/ 30 марта 2020

Всякий раз, когда записываете дату и время на лист Excel, формат ячейки по умолчанию обрабатывает его как дату и время и отображает в формате dd/mm/yyyy hh:mm, а не как dd-MMM-yyyy HH:mm:ss

Но возвращается в листе Excel, когда дата и время заключены в " " (двойные кавычки) и начинаются с префикса = (знак равенства), он обрабатывает его как строку, содержащую некоторые значения, и отображается точно так же, как и вместо отображения по умолчанию dd/mm/yyyy hh:mm формат.

Таким образом, чтобы получить строку, содержащую данные таблицы в формате dd/mm/yyyy hh:mm, необходимо заменить дату и время, заключенные в двойные кавычки, ="dd-MMM-yyyy HH:mm:ss"

Например: 25-Feb-2020 15:27:58 необходимо заменить на ="25-Feb-2020 15:27:58"

<table>
   \n        
   <thead>
      <tr>
         <th style=\"\">
            <div class=\"th-inner \">Login Name</div>
            <div class=\"fht-cell\"></div>
         </th>
         <th style=\"\">
            <div class=\"th-inner sortable\">Registered</div>
            <div class=\"fht-cell\"></div>
         </th>
         <th style=\"\">
            <div class=\"th-inner \">Registered Date <br>Time</div>
            <div class=\"fht-cell\"></div>
         </th>
         <th style=\"\">
            <div class=\"th-inner sortable\">User Response Count</div>
            <div class=\"fht-cell\"></div>
         </th>
         <th style=\"\">
            <div class=\"th-inner \">Test Start Date Time</div>
            <div class=\"fht-cell\"></div>
         </th>
         <th style=\"\">
            <div class=\"th-inner \">Test End Date Time</div>
            <div class=\"fht-cell\"></div>
         </th>
         <th style=\"\">
            <div class=\"th-inner \">Time Remaining</div>
            <div class=\"fht-cell\"></div>
         </th>
         <th style=\"\">
            <div class=\"th-inner \">User Status</div>
            <div class=\"fht-cell\"></div>
         </th>
      </tr>
   </thead>
   <tbody>
      <tr data-index=\"9\">
         <td style=\"\">njuser14</td>
         <td style=\"\">Yes</td>
         <td style=\"\">-</td>
         <td style=\"\">0</td>
         <td style=\"\">25-Feb-2020 15:27:58</td>
         <td style=\"\">25-Feb-2020 15:28:03</td>
         <td style=\"\">179</td>
         <td style=\"\">Paused</td>
      </tr>
      <tr data-index=\"10\">
         <td style=\"\">njuser15</td>
         <td style=\"\">Yes</td>
         <td style=\"\">-</td>
         <td style=\"\">0</td>
         <td style=\"\">25-Feb-2020 15:27:32</td>
         <td style=\"\">25-Feb-2020 15:27:42</td>
         <td style=\"\">179</td>
         <td style=\"\">Paused</td>
      </tr>
   </tbody>
</table>

В приведенных ниже фрагментах кода BatchDetails, содержащих строку, содержащую данные таблицы, необходимо заменить и затем назначить для сеанса

[System.Web.Services.WebMethod()]
public static string PersistBatchDetails(object BatchDetails)
{
    try
    {
        HttpContext.Current.Session["DashboardBatchInfo"] = BatchDetails;
    }
    catch (Exception ex){}
    return "S001";
}

Попытка написания кода с использованием DotNetFiddler

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string text = "<table>\n  <thead><tr><th style=\"\"><div class=\"th-inner \">Login Name</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner sortable\">Registered</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Registered Date <br>Time</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner sortable\">User Response Count</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Test Start Date Time</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Test End Date Time</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Time Remaining</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">User Status</div><div class=\"fht-cell\"></div></th></tr></thead><tbody><tr data-index=\"9\"><td style=\"\">njuser14</td><td style=\"\">Yes</td><td style=\"\">-</td><td style=\"\">0</td><td style=\"\">29-Feb-2020 15:27:58</td><td style=\"\">29-Feb-2020 15:28:03</td><td style=\"\">179</td><td style=\"\">Paused</td></tr><tr data-index=\"10\"><td style=\"\">njuser15</td><td style=\"\">Yes</td><td style=\"\">-</td><td style=\"\">0</td><td style=\"\">29-Feb-2020 15:27:32</td><td style=\"\">29-Feb-2020 15:27:42</td><td style=\"\">179</td><td style=\"\">Paused</td></tr></tbody></table>";
        string text2 = " dasd arew 2017-03-11 12:25:56 2017-03-11 12:25:56 das tfgwe 2017-03-11 12:25:56 ";
        string pattern = @"\d{4}\-\d{2}\-\d{2}\s\d{2}\:\d{2}\:\d{2}";
        Regex r = new Regex(pattern);
        var res = r.Replace(text, new MatchEvaluator(ConvertDateFormat));
        var res2 = r.Replace(text2, new MatchEvaluator(ConvertDateFormat));
        Console.WriteLine(res);
        Console.WriteLine("-------------------------------------------------------");
        Console.WriteLine(res2);
    }

    static string ConvertDateFormat(Match m)
    {
        var mydate = DateTime.Parse(m.Value);
        return mydate.ToString("=yyyy-MM-dd hh:mm:ss");
    }
}

// 29-Feb-2020 15:27:58 need to be replaced with ="29-Feb-2020 15:27:58"

Результаты:

<table>
  <thead><tr><th style=""><div class="th-inner ">Login Name</div><div class="fht-cell"></div></th><th style=""><div class="th-inner sortable">Registered</div><div class="fht-cell"></div></th><th style=""><div class="th-inner ">Registered Date <br>Time</div><div class="fht-cell"></div></th><th style=""><div class="th-inner sortable">User Response Count</div><div class="fht-cell"></div></th><th style=""><div class="th-inner ">Test Start Date Time</div><div class="fht-cell"></div></th><th style=""><div class="th-inner ">Test End Date Time</div><div class="fht-cell"></div></th><th style=""><div class="th-inner ">Time Remaining</div><div class="fht-cell"></div></th><th style=""><div class="th-inner ">User Status</div><div class="fht-cell"></div></th></tr></thead><tbody><tr data-index="9"><td style="">njuser14</td><td style="">Yes</td><td style="">-</td><td style="">0</td><td style="">29-Feb-2020 15:27:58</td><td style="">29-Feb-2020 15:28:03</td><td style="">179</td><td style="">Paused</td></tr><tr data-index="10"><td style="">njuser15</td><td style="">Yes</td><td style="">-</td><td style="">0</td><td style="">29-Feb-2020 15:27:32</td><td style="">29-Feb-2020 15:27:42</td><td style="">179</td><td style="">Paused</td></tr></tbody></table>
-------------------------------------------------------
 dasd arew =2017-03-11 12:25:56 =2017-03-11 12:25:56 das tfgwe =2017-03-11 12:25:56

1 Ответ

0 голосов
/ 31 марта 2020

Решение: DotNetFiddler

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string text = "<table>\n  <thead><tr><th style=\"\"><div class=\"th-inner \">Login Name</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner sortable\">Registered</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Registered Date <br>Time</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner sortable\">User Response Count</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Test Start Date Time</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Test End Date Time</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">Time Remaining</div><div class=\"fht-cell\"></div></th><th style=\"\"><div class=\"th-inner \">User Status</div><div class=\"fht-cell\"></div></th></tr></thead><tbody><tr data-index=\"9\"><td style=\"\">njuser14</td><td style=\"\">Yes</td><td style=\"\">-</td><td style=\"\">0</td><td style=\"\">29-Feb-2020 15:27:58</td><td style=\"\">29-Feb-2020 15:28:03</td><td style=\"\">179</td><td style=\"\">Paused</td></tr><tr data-index=\"10\"><td style=\"\">njuser15</td><td style=\"\">Yes</td><td style=\"\">-</td><td style=\"\">0</td><td style=\"\">29-Feb-2020 15:27:32</td><td style=\"\">29-Feb-2020 15:27:42</td><td style=\"\">179</td><td style=\"\">Paused</td></tr></tbody></table>";
        string text2 = " dasd arew 29-Mar-2020 12:25:56 29-Mar-2020 12:25:56 das tfgwe 29-Feb-2020 12:25:56 ";
        //string pattern = @"\d{4}\-\d{2}\-\d{2}\s\d{2}\:\d{2}\:\d{2}";
        //string pattern = @"\d{4}\-^[a-zA-Z]{3}-\d{2}\s\d{2}\:\d{2}\:\d{2}";
        //string pattern = @"\d{4}\-([JAN|FEB|MAR|APR|MAY|JUN|JULY|AUG|SEP|OCT|NOV|DEC])-\d{2}\s\d{2}\:\d{2}\:\d{2}";
        string pattern = @"\d{4}\-\d{2}\-\d{2}\s\d{2}\:\d{2}\:\d{2}|\d{2}\-[a-zA-Z]{3}\-\d{4}\s\d{2}\:\d{2}\:\d{2}";

        Regex r = new Regex(pattern);
        var res = r.Replace(text, new MatchEvaluator(ConvertDateFormat));
        var res2 = r.Replace(text2, new MatchEvaluator(ConvertDateFormat));
        Console.WriteLine(res);
        Console.WriteLine("-------------------------------------------------------");
        Console.WriteLine(res2);
    }

    static string ConvertDateFormat(Match m)
    {
        var mydate = DateTime.Parse(m.Value);
        //return mydate.ToString("=yyyy-MM-dd hh:mm:ss");
        return string.Format("=\"{0}\"", mydate.ToString("yyyy-MM-dd hh:mm:ss"));
    }
}

// 29-Feb-2020 15:27:58 need to be replaced with ="29-Feb-2020 15:27:58"
...