added full excel functionality

This commit is contained in:
Wyatt J. Miller 2022-07-31 22:37:14 -04:00
parent 389f555633
commit 5c040f08c6

View File

@ -4,66 +4,130 @@ using DocumentFormat.OpenXml.Spreadsheet;
public class ExcelDataService : IDataService public class ExcelDataService : IDataService
{ {
private SpreadsheetDocument? _xlsxFile;
private Workbook? _xlsxWorkbook;
private WorkbookPart? _xlsxWorkbookPart;
private WorksheetPart? _xlsxWorksheetPart;
private Sheets? _xlsxSheets;
private Sheet? _xlsxSheet;
private Cell? _cell;
private string _fileName; private string _fileName;
private string _pathName; private string _pathName;
private string _dateTime; private string _dateTime;
public ExcelDataService() public ExcelDataService()
{ {
_dateTime = DateTime.Now.ToString("yyyyMMdd"); _dateTime = DateTime.Now.ToString("yyyyMMddhhmm");
_fileName = $"CommitReport-{_dateTime}.xlsx"; _fileName = $"CommitReport-{_dateTime}.xlsx";
_pathName = Directory.GetCurrentDirectory() + "/" + _fileName; _pathName = Directory.GetCurrentDirectory() + "/" + _fileName;
System.Console.WriteLine(_pathName);
_xlsxFile = SpreadsheetDocument.Create(_pathName, SpreadsheetDocumentType.Workbook);
_xlsxWorkbookPart = _xlsxFile.AddWorkbookPart();
_xlsxWorkbookPart.Workbook = new Workbook();
_xlsxWorksheetPart = _xlsxWorkbookPart.AddNewPart<WorksheetPart>();
_xlsxWorksheetPart.Worksheet = new Worksheet(new SheetData());
_xlsxSheets = _xlsxFile.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
_xlsxSheet = new Sheet() { Id = _xlsxFile.WorkbookPart.GetIdOfPart(_xlsxWorksheetPart), SheetId = 1, Name = "Commit Report" };
_xlsxSheets.Append(_xlsxSheet);
_xlsxWorkbookPart.Workbook.Save();
_xlsxFile.Close();
} }
public void WriteAll(SortedList<string, int> dictResults) public void WriteAll(SortedList<string, int> dictResults)
{ {
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(_pathName, true)) int index = 1;
if (!File.Exists(_pathName))
{ {
WorksheetPart worksheetPart = spreadSheet.WorkbookPart.WorksheetParts.First(); this.CreateSpreadsheetWorkbook(_pathName);
}
foreach (var d in dictResults)
{
var index = dictResults.GetEnumerator().Current;
_cell = InsertCellInWorksheet("A", (index.Value), worksheetPart);
_cell.CellValue = new CellValue(d.Key);
_cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
worksheetPart.Worksheet.Save();
//_cell = InsertCellInWorksheet("B", (index.Value), worksheetPart);
//_cell.CellValue = new CellValue(d.Value);
//_cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
//worksheetPart.Worksheet.Save();
}
foreach (var d in dictResults)
{
this.InsertText(_pathName, d.Key, index, "A");
this.InsertText(_pathName, d.Value.ToString(), index, "B");
index++;
} }
} }
private static Cell InsertCellInWorksheet(string columnName, int rowIndex, WorksheetPart worksheetPart) private void CreateSpreadsheetWorkbook(string filepath)
{
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
Create(filepath, SpreadsheetDocumentType.Workbook);
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());
Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.
GetIdOfPart(worksheetPart), SheetId = 1, Name = "Commit Report" };
sheets.Append(sheet);
workbookpart.Workbook.Save();
spreadsheetDocument.Close();
}
// Given a document name and text,
private void InsertText(string docName, string text, int iterator, string column)
{
// Open the document for editing.
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
{
// Get the SharedStringTablePart. If it does not exist, create a new one.
SharedStringTablePart shareStringPart;
if (spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0)
{
shareStringPart = spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();
}
else
{
shareStringPart = spreadSheet.WorkbookPart.AddNewPart<SharedStringTablePart>();
}
// Insert the text into the SharedStringTablePart.
int index = this.InsertSharedStringItem(text, shareStringPart);
// Grab the first worksheet created earlier.
WorksheetPart worksheetPart = spreadSheet.WorkbookPart.Workbook.WorkbookPart.WorksheetParts.First();
// Insert cell into the worksheet with the given column and row (iterator).
Cell cell = this.InsertCellInWorksheet(column, (uint)iterator, worksheetPart);
// Set the value of cell
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new EnumValue<CellValues>(CellValues.SharedString);
// Save the new worksheet.
worksheetPart.Worksheet.Save();
}
}
// Given text and a SharedStringTablePart, creates a SharedStringItem with the specified text
// and inserts it into the SharedStringTablePart. If the item already exists, returns its index.
private int InsertSharedStringItem(string text, SharedStringTablePart shareStringPart)
{
// If the part does not contain a SharedStringTable, create one.
if (shareStringPart.SharedStringTable == null)
{
shareStringPart.SharedStringTable = new SharedStringTable();
}
int i = 0;
// Iterate through all the items in the SharedStringTable. If the text already exists, return its index.
foreach (SharedStringItem item in shareStringPart.SharedStringTable.Elements<SharedStringItem>())
{
if (item.InnerText == text)
{
return i;
}
i++;
}
// The text does not exist in the part. Create the SharedStringItem and return its index.
shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text)));
shareStringPart.SharedStringTable.Save();
return i;
}
// Given a column name, a row index, and a WorksheetPart, inserts a cell into the worksheet.
// If the cell already exists, returns it.
private Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart)
{ {
Worksheet worksheet = worksheetPart.Worksheet; Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild<SheetData>(); SheetData sheetData = worksheet.GetFirstChild<SheetData>();
string cellReference = columnName + rowIndex; string cellReference = columnName + rowIndex;
// If the worksheet does not contain a row with the specified row index, insert one.
Row row; Row row;
if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0) if (sheetData.Elements<Row>().Where(r => r.RowIndex == rowIndex).Count() != 0)
{ {
@ -71,17 +135,33 @@ public class ExcelDataService : IDataService
} }
else else
{ {
row = new Row() { RowIndex = ((uint)rowIndex) }; row = new Row() { RowIndex = rowIndex };
sheetData.Append(row); sheetData.Append(row);
} }
Cell refCell = row.Descendants<Cell>().LastOrDefault(); // If there is not a cell with the specified column name, insert one.
if (row.Elements<Cell>().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)
{
return row.Elements<Cell>().Where(c => c.CellReference.Value == cellReference).First();
}
else
{
// Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
Cell refCell = null;
foreach (Cell cell in row.Elements<Cell>())
{
if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
{
refCell = cell;
break;
}
}
Cell newCell = new Cell() { CellReference = cellReference }; Cell newCell = new Cell() { CellReference = cellReference };
row.InsertAfter(newCell, refCell); row.InsertBefore(newCell, refCell);
worksheet.Save();
return newCell;
worksheet.Save();
return newCell;
}
} }
} }