public static StringKeyList ReadStringTable(SharedStringTablePart sharedStringTablePart) { var dict = new StringKeyList(); using (var reader = OpenXmlReader.Create(sharedStringTablePart)) { while (reader.Read()) { if (reader.ElementType == typeof(Excel.SharedStringItem)) { dict.Add(new StringKey((Excel.SharedStringItem)reader.LoadCurrentElement())); } } } return(dict); }
public static void WriteCell(Excel.Cell cell, object value, StringKeyList sharedStrings) { if (value != null) { Type type = value.GetType(); if (type == typeof(int) || type == typeof(uint) || type == typeof(long) || type == typeof(ulong) || type == typeof(short) || type == typeof(ushort)) { cell.DataType = Excel.CellValues.Number; cell.CellValue = new Excel.CellValue(value.ToString()); } else if (value.GetType() == typeof(decimal) || value.GetType() == typeof(float) || value.GetType() == typeof(double)) { cell.DataType = Excel.CellValues.Number; cell.CellValue = new Excel.CellValue(((decimal)value).ToString(".00", CultureInfo.InvariantCulture.NumberFormat)); } else { if (cell.CellFormula != null) { cell.DataType = Excel.CellValues.String; cell.CellValue = new Excel.CellValue(value.ToString()); } else { var stringValue = value.ToString(); if (!sharedStrings.TryGetIndex(stringValue, out var index)) { index = sharedStrings.Add(new StringKey(stringValue)); } cell.DataType = Excel.CellValues.SharedString; cell.CellValue = new Excel.CellValue(index.ToString()); } //cell.DataType = Excel.CellValues.String; //cell.CellValue = new Excel.CellValue(value.ToString().Replace("", string.Empty)); } } }