public ExcelSheet(Sheet sheet, ExcelDocument doc) { this.sheet = sheet; this.document = doc; this.worksheet = ((WorksheetPart)(doc.document.WorkbookPart.GetPartById(sheet.Id))).Worksheet; _rows = new ExcelRowCollection(this); }
public ExcelSheet(ExcelDocument doc, string name) { this.document = doc; WorksheetPart newWorksheetPart = doc.document.WorkbookPart.AddNewPart <WorksheetPart>(); worksheet = new Worksheet(new SheetData()); newWorksheetPart.Worksheet = worksheet; Sheets sheets = doc.document.WorkbookPart.Workbook.GetFirstChild <Sheets>(); if (sheets == null) { sheets = doc.document.WorkbookPart.Workbook.AppendChild <Sheets>(new Sheets()); } string relationshipId = doc.document.WorkbookPart.GetIdOfPart(newWorksheetPart); // Get a unique ID for the new worksheet. uint sheetId = 1; if (sheets.Elements <Sheet>().Count() > 0) { sheetId = sheets.Elements <Sheet>().Select(s => s.SheetId.Value).Max() + 1; } // Give the new worksheet a name. string sheetName = "Sheet"; if (String.IsNullOrEmpty(name)) { sheetName = getSheetName(sheetName, sheets.Elements <Sheet>(), 1); } else { sheetName = getSheetName(name, sheets.Elements <Sheet>()); } // Append the new worksheet and associate it with the workbook. sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName }; sheets.Append(sheet); _rows = new ExcelRowCollection(this); }
public ExcelSheet(ExcelDocument doc, string name) { this.document = doc; WorksheetPart newWorksheetPart = doc.document.WorkbookPart.AddNewPart<WorksheetPart>(); worksheet = new Worksheet(new SheetData()); newWorksheetPart.Worksheet = worksheet; Sheets sheets = doc.document.WorkbookPart.Workbook.GetFirstChild<Sheets>(); if (sheets == null) sheets = doc.document.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets()); string relationshipId = doc.document.WorkbookPart.GetIdOfPart(newWorksheetPart); // Get a unique ID for the new worksheet. uint sheetId = 1; if (sheets.Elements<Sheet>().Count() > 0) sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1; // Give the new worksheet a name. string sheetName = "Sheet"; if (String.IsNullOrEmpty(name)) sheetName = getSheetName(sheetName, sheets.Elements<Sheet>(), 1); else sheetName = getSheetName(name, sheets.Elements<Sheet>()); // Append the new worksheet and associate it with the workbook. sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName }; sheets.Append(sheet); _rows = new ExcelRowCollection(this); }