forked from EvotecIT/OfficeIMO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcelSheet.cs
77 lines (66 loc) · 2.7 KB
/
ExcelSheet.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace OfficeIMO.Excel {
public class ExcelSheet {
private readonly Sheet _sheet;
public string Name {
get {
return _sheet.Name;
}
set {
_sheet.Name = value;
}
}
private readonly UInt32Value Id;
private readonly WorksheetPart _worksheetPart;
private readonly SpreadsheetDocument _spreadSheetDocument;
private readonly ExcelDocument _excelDocument;
public ExcelSheet(ExcelDocument excelDocument, SpreadsheetDocument spreadSheetDocument, Sheet sheet) {
_excelDocument = excelDocument;
_sheet = sheet;
_spreadSheetDocument = spreadSheetDocument;
var list = _spreadSheetDocument.WorkbookPart.WorksheetParts.ToList();
foreach (var worksheetPart in list) {
var id = spreadSheetDocument.WorkbookPart.GetIdOfPart(worksheetPart);
if (id == _sheet.Id) {
_worksheetPart = worksheetPart;
}
}
}
public ExcelSheet(ExcelDocument excelDocument, WorkbookPart workbookpart, SpreadsheetDocument spreadSheetDocument, string name) {
_excelDocument = excelDocument;
_spreadSheetDocument = spreadSheetDocument;
UInt32Value id = excelDocument.id.Max() + 1;
if (name == "") {
name = "Sheet1";
}
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = null;
if (spreadSheetDocument.WorkbookPart.Workbook.Sheets != null) {
sheets = spreadSheetDocument.WorkbookPart.Workbook.Sheets;
} else {
sheets = spreadSheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
}
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() {
Id = spreadSheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = id,
Name = name
};
sheets.Append(sheet);
this._sheet = sheet;
this.Name = name;
this.Id = sheet.SheetId;
this._worksheetPart = worksheetPart;
excelDocument.id.Add(id);
}
}
}