SpreadsheetLight不接受时间跨度类型

问题描述 投票:0回答:1

我正在使用spreadsheetlight将数据导出到excel。我有定义字符串的时间值,我想转换或解析为Timespan但是spreadsheetlight不接受Timespan值。如何使用spreadsheetlight定义Timespan?

这是我的代码:

var longTimeStyle = Document.CreateStyle();
longTimeStyle.Alignment.Horizontal = HorizontalAlignmentValues.Right;
longTimeStyle.FormatCode = CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern;

    for (int i = 0; i < shiftStatus.Count; i++)
  {
     Document.SetCellStyle(i + 2, 7, longTimeStyle);
     //shiftStatus[i].PreperationTime is string
     Document.SetCellValue(i + 2, 7, shiftStatus[i].PreperationTime); 
  }
export-to-excel timespan spreadsheetlight
1个回答
0
投票

以下是我对此问题的解决方案:

public string ConvertTimeSpanForExcel(TimeSpan ts)
{

  return String.Format($"{ts.Days} d. {ts.Hours} h. {ts.Minutes} min. {ts.Seconds} sec.");

}

Document.SetCellStyle(i + 2, 7, ConvertTimeSpanForExcel(shiftStatus[i].PreperationTime));

如果您需要数周,数月等,则开发一种划分天数并输出此结果的方法。

© www.soinside.com 2019 - 2024. All rights reserved.