我正在尝试从excel中读取特定单元格的文本值。该值是一个将多个单元格相加的公式。
到目前为止,我已经尝试过:
worksheet.Cells["E42"].Style.Numberformat.Format = "hh:mm"; //and [hh]:mm
result = worksheet.Cells["E42"].Text;
使用FromAoDate将worksheet.Cells["E42"].Value
转换为DateTime对我也不起作用。
最好的结果是08:20,这比实际值少了24小时。
编辑: 建议的结果
worksheet.Cells["E42"].Style.Numberformat.Format = "[hh]:mm";
result = worksheet.Cells["E42"].Text; //returns "08"
worksheet.Cells["E42"].Style.Numberformat.Format = "hh:mm";
result = worksheet.Cells["E42"].Text; //returns "08:20"
worksheet.Cells["E42"].Style.Numberformat.Format = "hh:mm";
result = worksheet.Cells["E42"].Text;
result = TimeSpan.TryParse((string)result, out TimeSpan tres); //returns 08:20:00
worksheet.Cells["E42"].Style.Numberformat.Format = "[hh]:mm";
result = worksheet.Cells["E42"].Text;
result = TimeSpan.TryParse((string)result, out TimeSpan tres); //returns 8.00:00:00
//converting to DateTime has similar results
result = DateTime.FromOADate(Convert.ToDouble(worksheet.Cells["E42"].Value));
//returns 31.12.1899 08:20:00
答案 0 :(得分:0)
这将为您提供一个TimeSpan对象,该对象应该可以满足您的需求。
string result = worksheet.Cells["E42"].Text;
TimeSpan timeResult;
string[] split = result.Split(':');
timeResult = new TimeSpan(int.Parse(split[0]), int.Parse(split[1]), 0);
答案 1 :(得分:0)
我还没有找到合适的解决方案,但是有办法。
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ADSO.ViewModel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:syncfusion="using:Syncfusion.UI.Xaml.Charts"
x:Class="ADSO.MainPage"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.DataContext>
<local:VoltageViewModel/>
</Page.DataContext>
<Grid>
<syncfusion:SfChart HorizontalAlignment="Center" VerticalAlignment="Center" Margin="20,20,20,20">
<syncfusion:SfChart.PrimaryAxis>
<syncfusion:DateTimeAxis Header="Time" Interval="10" IntervalType="Minutes" Minimum="9/22/2018" Maximum="9/23/2018"/>
</syncfusion:SfChart.PrimaryAxis>
<syncfusion:SfChart.SecondaryAxis>
<syncfusion:NumericalAxis Header="Voltage (V)"/>
</syncfusion:SfChart.SecondaryAxis>
<syncfusion:LineSeries ItemsSource="{Binding Path=Data}" XBindingPath="Timestamp" YBindingPath="Value" >
</syncfusion:LineSeries>
</syncfusion:SfChart>
</Grid>
var worksheet = package.Workbook.Worksheets[2];
var value = worksheet.Cells["E42"].GetValue<DateTime>(); //the cell contains 32:20 in excel
//returns 31.12.1899 08:20:00
var value2 = worksheet.Cells["E39"].GetValue<DateTime>(); //the cell contains 00:00 in excel
// which returns 30.12.1899 00:00:00
var t = value.Subtract(value2); //returns 1.08:20:00, which translates to 32:20:00
日期似乎转换为00:00
和DateTime.Parse("30.12.1899 00:00:00");
到33:20
,因此通过减去我们可以恢复实际时间跨度。
这是一个示例实现:
31.12.1899 08:20:00