无法 return XLWorkbook 中工作表中单元格的值
Failed to return the value of a cell in a Worksheet in an XLWorkbook
事实证明,两个单元格分配了两个不同的日期;对于单元格 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
任何贡献都会对我有很大帮助。非常感谢你。对不起我的英语,我说西班牙语
使用 FormulaR1C1 和公式 属性 写入 Excel 的公式必须完全用美国英语语法编写。
经过大量尝试,他们强迫我使用 Microsoft 提供的 Excel 导入库来实现 "brute force" ---> using Microsoft.Office.Interop.Excel;
<---
像这样的代码:
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);
}
}
工作完美,感谢您的贡献!
事实证明,两个单元格分配了两个不同的日期;对于单元格 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
任何贡献都会对我有很大帮助。非常感谢你。对不起我的英语,我说西班牙语
使用 FormulaR1C1 和公式 属性 写入 Excel 的公式必须完全用美国英语语法编写。
经过大量尝试,他们强迫我使用 Microsoft 提供的 Excel 导入库来实现 "brute force" ---> using Microsoft.Office.Interop.Excel;
<---
像这样的代码:
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);
}
}
工作完美,感谢您的贡献!