Esempio n. 1
0
        public T GetCellValue <T>(string SheetName, uint ColumnIdx, uint RowIdx)
        {
            T   res  = default;
            var cell = GetCell(SheetName, ColumnIdx, RowIdx);

            if (cell != null)
            {
                var cellValue = cell.CellValue != null ? cell.CellValue.Text : "";
                if (cell.DataType != null && cell.DataType == CellValues.SharedString)
                {
                    var idx  = int.Parse(cellValue, CultureInfo.InvariantCulture);
                    var tbl  = Document.WorkbookPart.SharedStringTablePart.SharedStringTable;
                    var item = (SharedStringItem)tbl.ChildElements.Skip(idx).First();
                    res = (T)Convert.ChangeType(item.Text.InnerText, typeof(T), CultureInfo.InvariantCulture);
                }
                else if ((cell.DataType != null && cell.DataType == CellValues.Date) || typeof(T) == typeof(DateTime))
                {
                    var d = (double)Convert.ChangeType(cellValue, typeof(double), CultureInfo.InvariantCulture);
                    res = (T)Convert.ChangeType(DateTime.FromOADate(d), typeof(T), CultureInfo.InvariantCulture);
                }
                else if ((cell.DataType != null && cell.DataType == CellValues.Boolean) || typeof(T) == typeof(bool))
                {
                    var d = (cellValue == "1");
                    res = (T)Convert.ChangeType(d, typeof(T), CultureInfo.InvariantCulture);
                }
                else
                {
                    res = (T)Convert.ChangeType(cellValue, typeof(T), CultureInfo.InvariantCulture);
                }
            }
            if (res == null)
            {
                throw new Exception($"Cell value not found in {SheetName}!{XLRefAddress.GetColumnName(ColumnIdx)}{RowIdx}");
            }
            return(res);
        }
Esempio n. 2
0
 private void ReplaceCell(uint columnIndex, uint rowIndex, Cell newCell)
 {
     ReplaceCell(XLRefAddress.GetColumnName(columnIndex), rowIndex, newCell);
 }
Esempio n. 3
0
 public Cell?GetCell(uint columnIndex, uint rowIndex)
 {
     return(GetCell(XLRefAddress.GetColumnName(columnIndex), rowIndex));
 }