首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpreadsheetLight不接受timespan类型

SpreadsheetLight不接受timespan类型
EN

Stack Overflow用户
提问于 2018-05-25 15:59:29
回答 1查看 82关注 0票数 0

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

下面是我的代码:

代码语言:javascript
复制
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); 
  }
EN

回答 1

Stack Overflow用户

发布于 2019-03-14 19:20:47

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

代码语言:javascript
复制
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));

如果您需要几周、几个月等时间,那么开发一种划分天的方法并输出此结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50524281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档