1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
| public class ExcelFileManager : FileManager
{
private SpreadsheetDocument _package;
private Row _currentRow;
private SheetData _sheetData;
private int? _cellColumnN2;
private int _cellColumnN1;
private int _cellRow;
public ExcelFileManager(string path)
: base(path)
{
_package = SpreadsheetDocument.Create(path, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook);
_cellColumnN1 = 65;
_cellRow = 1;
CreateSpreadSheet();
}
private void CreateSpreadSheet()
{
var workbookPart1 = _package.AddWorkbookPart();
workbookPart1.Workbook = new Workbook();
var worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>();
worksheetPart1.Worksheet = new Worksheet();
var sheets = _package.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
var sheet = new Sheet()
{
Id = _package.WorkbookPart.
GetIdOfPart(worksheetPart1),
SheetId = 1,
Name = "Users"
};
sheets.Append(sheet);
_sheetData = new SheetData();
_currentRow = new Row();
_sheetData.Append(_currentRow);
worksheetPart1.Worksheet.Append(_sheetData);
}
public override void AddHeader(IEnumerable<string> columnsName)
{
if (columnsName == null || !columnsName.Any())
throw new ArgumentNullException("columnsName");
try
{
AddRow(columnsName.ToArray());
}
catch (Exception e)
{
Tracing.Application.TraceError(-1, "An error occured when adding header to excel file", e);
throw;
}
}
public override void AddRow(params object[] values)
{
if (values == null || !values.Any())
throw new ArgumentNullException("values");
try
{
var listCells = new List<OpenXmlElement>();
foreach (var item in values)
{
listCells.Add(CreateCellRow(item));
}
_currentRow.Append(listCells);
AddNewRow();
}
catch (Exception e)
{
Tracing.Application.TraceError(-1, "An error occured when adding row to excel file", e);
throw;
}
}
public override void AddCell(object value)
{
try
{
var cell = CreateCellRow(value);
_currentRow.Append(cell);
}
catch (Exception e)
{
Tracing.Application.TraceError(-1, "An error occured when adding cell to excel file", e);
throw;
}
}
public override void AddNewRow()
{
_currentRow = new Row();
_sheetData.Append(_currentRow);
IncreaseRow();
}
#region Utils
private Cell CreateCellRow(object value)
{
Tracing.Application.TraceInformation("Add value \"{0}\" in cell {1}", value, GetCellLocation());
Cell cell = new Cell();
if (value == null)
{
cell.CellValue = new CellValue(string.Empty);
cell.DataType = CellValues.String;
}
else
{
cell.CellValue = new CellValue(value.ToString());
cell.DataType = GetCellType(value);
}
cell.CellReference = GetCellLocation();
IncreaseCellColumn();
return cell;
}
private CellValues GetCellType(object value)
{
CellValues type = CellValues.String;
if (value.GetType() == typeof(DateTime) || value.GetType() == typeof(DateTime?))
type = CellValues.Date;
if (value.GetType() == typeof(bool) || value.GetType() == typeof(bool?))
type = CellValues.Boolean;
if (IsNumericType(value.GetType()))
type = CellValues.Number;
return type;
}
public bool IsNumericType(Type type)
{
TypeCode typeCode = Type.GetTypeCode(type);
//The TypeCode of numerical types are between SByte (5) and Decimal (15).
return (int)typeCode >= 5 && (int)typeCode <= 15;
}
private void IncreaseCellColumn()
{
if (_cellColumnN1 < 90)
_cellColumnN1++;
else
{
_cellColumnN1 = 65;
if (!_cellColumnN2.HasValue)
_cellColumnN2 = 65;
else
_cellColumnN2++;
}
}
private void IncreaseRow()
{
_cellRow++;
_cellColumnN1 = 65;
_cellColumnN2 = null;
}
private string GetCellLocation()
{
Char? c1 = null;
if (_cellColumnN2.HasValue)
c1 = (char)_cellColumnN2;
Char c2 = (char)_cellColumnN1;
return string.Format("{0}{1}{2}", c1, c2, _cellRow);
}
#endregion
#region IDisposable
public override void Dispose()
{
if (_package != null)
{
_package.WorkbookPart.Workbook.Save();
_package.Close();
}
}
#endregion
} |
Partager