-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringArrayToExcel.cs
44 lines (37 loc) · 1.28 KB
/
StringArrayToExcel.cs
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
public static class Conventer {
public static byte[] getExcelByte(string[, ] data) {
using(MemoryStream ms = new MemoryStream()) {
using(SpreadsheetDocument sd = SpreadsheetDocument.Create(ms, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook)) {
var workbook = sd.AddWorkbookPart();
workbook.Workbook = new Workbook();
var worksheet = workbook.AddNewPart < WorksheetPart > ();
worksheet.Worksheet = new Worksheet(new SheetData());
var sheets = sd.WorkbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() {
Id = sd.WorkbookPart.GetIdOfPart(worksheet),
SheetId = 1,
Name = "main"
};
sheets.Append(sheet);
SheetData sheetData = worksheet.Worksheet.GetFirstChild < SheetData > ();
for (int i = 0; i < data.GetLength(0); i++) {
for (int j = 0; j < data.GetLength(1); j++) {
Row row;
row = new Row() {
RowIndex = (uint)(i + 1)
};
sheetData.Append(row);
Cell newCell = new Cell() {
CellReference = numToStr(j + 1) + "" + (i + 1)
};
row.InsertBefore(newCell, null);
newCell.CellValue = new CellValue(data[i, j]);
newCell.DataType = new EnumValue < CellValues > (CellValues.String);
}
}
sd.Close();
return ms.ToArray();
}
}
}
}