예제 #1
0
        /// <summary>
        /// Set value of cell
        /// </summary>
        /// <param name="cellRef">Cell reference</param>
        /// <param name="value">Cell value</param>
        public void SetCellValue(CellReference cellRef, Object value)
        {
            var  cellType = GetCellValues(value == null ? null : value.GetType());
            Cell cell     = _sheet.Descendants <Cell>().FirstOrDefault(a => a.CellReference == cellRef.ToString());

            if (cell == null)
            {   // Cell not exist, must create new one
                cell = CreateCell(cellRef, cellType);
            }
            else
            {
                if (cell.CellValue != null)
                {
                    cell.RemoveChild(cell.CellValue);
                }
                if (cell.DataType != null)
                {
                    cellType = cell.DataType; // Don't change original data type
                }
            }
            switch (cellType)
            {
            case CellValues.Boolean:
            {
                if ((bool)value)
                {
                    cell.AppendChild(new CellValue("1"));
                }
                else
                {
                    cell.AppendChild(new CellValue("0"));
                }
                break;
            }

            case CellValues.Number:
            {
                cell.AppendChild(new CellValue(value.ToString()));
                break;
            }

            case CellValues.Date:
            {           // Convert serialized date to DateTime
                var dbl = ((DateTime)value).ToOADate();
                cell.AppendChild(new CellValue(dbl.ToString(CultureInfo.InvariantCulture)));
                break;
            }

            case CellValues.SharedString:
            {
                if (value != null)
                {
                    var i = InsertSharedStringItem(value.ToString());
                    if (cell.CellValue == null)
                    {
                        cell.CellValue = new CellValue(i.ToString());
                    }
                    if (cell.DataType == null)
                    {
                        cell.DataType = new EnumValue <CellValues>(cellType);
                    }
                }
                break;
            }
            }
        }
예제 #2
0
 /// <summary>
 /// Initialize a range with top left and buttom right corner cells
 /// </summary>
 /// <param name="doc">Excel document</param>
 /// <param name="sheet">Worksheet</param>
 /// <param name="tl">Cell reference for top left corner</param>
 /// <param name="br">Cell reference for bottom right corner</param>
 public ExcelOpenXMLRange(SpreadsheetDocument doc, Worksheet sheet, CellReference tl, CellReference br)
     : base(tl, br)
 {
     _doc   = doc;
     _sheet = sheet;
 }
예제 #3
0
 public CellNotExistException(CellReference reference)
 {
     Reference = reference;
 }