事实证明,两个小区分配了两个不同的日期;对于单元格A2和B2,我分别为它们分配了3/28/2015和3/29/2015(这来自c#,要清楚)。
然后,对于单元格A3,我应用公式。我的工作如下:
ws.Range("A3").FormulaR1C1 = "=SIFECHA(R[-1]C[0],R[-1]C[1],\"md\")";
当我尝试使用以下指令从c#访问所述单元格的值时:
ws.Cell("A3").Value;
我得到一个例外,似乎是由于数据转换失败......
Value ='((ClosedXML.Excel.XLCell)y).Value'抛出类型' System.Exception'
的例外代码:
var Dates = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("28/03/2015","29/03/2015"),
};
XLWorkbook workbook = await Task.FromResult(Dates.ToBook());
var ws = workbook.Worksheet("Report");
ws.Cell("A3").DataType = XLCellValues.Number; // a try failed
ws.Range("A3").FormulaR1C1 = "=ENTERO(SIFECHA(R[-1]C[0],R[-1]C[1],\"md\"))";
ws.Cell("A3").Select();
ws.Cell("A3").SetActive(true); //another try
ws.Columns().AdjustToContents();
var y = ws.ActiveCell.Value; //and here the code exploid
任何贡献对我都有很大的帮助。非常感谢你。对不起我的英语,我会讲西班牙语
答案 0 :(得分:2)
使用FormulaR1C1和Formula属性写入Excel的公式必须完全用美国英语语法编写。
答案 1 :(得分:0)
经过大量的尝试,他们迫使我使用Microsoft提供的Excel导入库来实现“蛮力”---&gt; using Microsoft.Office.Interop.Excel;
&lt; ---
这样的代码:
public static Application App;
public static Workbook Wb;
public static Worksheet Ws;
public static object GetGetTimeElapsed(string Fecha_Inicio, string Fecha_Fin)
{
try
{
if (App == null)
{
App = new Application();
Wb = App.Workbooks.Add();
Ws = (Worksheet)Wb.Worksheets.get_Item(1);
}
Ws.Cells[1, 1].Formula = "=DATEDIF(\"" + Fecha_Inicio + "\",\"" + Fecha_Fin + "\",\"MD\")";
Ws.Cells[1, 2].Formula = "=DATEDIF(\"" + Fecha_Inicio + "\",\"" + Fecha_Fin + "\",\"YM\")";
Ws.Cells[1, 3].Formula = "=DATEDIF(\"" + Fecha_Inicio + "\",\"" + Fecha_Fin + "\",\"Y\")";
double DaysElapsed = Ws.Cells[1, 1].Value;
double MonthsElapsed = Ws.Cells[1, 2].Value;
double YearsElapsed = Ws.Cells[1, 3].Value;
var List = new
{
Days = DaysElapsed,
Months = MonthsElapsed,
Years = YearsElapsed
};
return new { List.Days, List.Months, List.Years };
}
catch (Exception ex)
{
throw new BaseException(ex);
}
}
完美的工作,感谢您的贡献!