private static void AddRow(SpreadsheetDocument spreadSheetDoc, DataTable table) { workbookPart = spreadSheetDocument.WorkbookPart; IEnumerable<sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<sheets>().Elements<sheet>(); string relationshipId = sheets.Where(s => s.Name == sheetName).SingleOrDefault<sheet>().Id.Value; worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId); workSheet = worksheetPart.Worksheet; sheetData = workSheet.GetFirstChild<sheetdata>(); lastRow = sheetData.ChildElements.Count - 1; var sheet = workbookPart.Workbook.Descendants<sheet>().FirstOrDefault(); if (sheet == null) throw new Exception("No sheed found in the template file. Please add the sheet"); int rowIndex = lastRow + 1, colIndex = 0; List<string> columns = new List<string>(); foreach (System.Data.DataColumn column in table.Columns) { columns.Add(column.ColumnName); } foreach (System.Data.DataRow dsrow in table.Rows) { Row row = new Row(); foreach (String col in columns) { DocumentFormat.OpenXml.Spreadsheet.Cell cell = new Cell(); cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); row.AppendChild<cell>(cell); } sheetData.InsertAt<row>(row, rowIndex); } workbookPart.Workbook.Save(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)