/// <summary> /// This method exports transactions into xlsx file. /// </summary> /// <param name="type">Type of transaction.</param> /// <param name="status">Status of transaction.</param> /// <param name="prop">Properties which should be returned.</param> /// <returns>XLSX file with transaction's data.</returns> public byte[] ExportXLSXFile(string type, string status, TransactionExportProperties prop) { var transactions = _transactionService.FilterTransactions(status, type); using (var workbook = new XLWorkbook()) { var worksheet = workbook.Worksheets.Add("Transactions"); var listOfPropertyNames = typeof(Transaction).GetProperties().Select(f => f.Name).ToList(); int currentRow = 1; int currentColumn = 1; foreach (var propertyName in listOfPropertyNames) { if ((bool)prop.GetType().GetProperty(propertyName).GetValue(prop, null)) { worksheet.Cell(currentRow, currentColumn++).Value = propertyName; } } foreach (var transaction in transactions) { currentColumn = 1; currentRow++; foreach (var propertyName in listOfPropertyNames) { if ((bool)prop.GetType().GetProperty(propertyName).GetValue(prop, null)) { worksheet.Cell(currentRow, currentColumn++).Value = transaction.GetType().GetProperty(propertyName).GetValue(transaction, null); } } } using (var stream = new MemoryStream()) { workbook.SaveAs(stream); return(stream.ToArray()); } } }
public IActionResult GetTransactionsInXLSX([FromQuery] string transStatus, [FromQuery] string transType, [FromQuery] TransactionExportProperties prop) { byte[] content; string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; try { content = _fileService.ExportXLSXFile(transType, transStatus, prop); } catch (Exception) { return(StatusCode(500)); } return(File(content, contentType)); }