Переглянути джерело

Changing library for generating excel file.

master
NikolaJovanovic 3 роки тому
джерело
коміт
5651ae3207

BIN
.vs/ClockifyReport/DesignTimeBuild/.dtbcache.v2 Переглянути файл


BIN
.vs/ClockifyReport/FileContentIndex/14b647f6-6b40-49c2-989f-5e14879f7a26.vsidx Переглянути файл


BIN
.vs/ClockifyReport/FileContentIndex/1bd1dae0-da29-473c-8039-ca9489e78e6c.vsidx Переглянути файл


BIN
.vs/ClockifyReport/FileContentIndex/8b10d275-bcfb-4ac6-a641-eb7373191b35.vsidx Переглянути файл


BIN
.vs/ClockifyReport/FileContentIndex/923b5bc0-c666-4a56-9628-e1cd7759aab2.vsidx Переглянути файл


BIN
.vs/ClockifyReport/FileContentIndex/a1b5fca2-32b4-471c-bdac-d77ab2793376.vsidx Переглянути файл


BIN
.vs/ClockifyReport/FileContentIndex/a4e685af-2ebb-4075-9f3a-309eb376d110.vsidx Переглянути файл


BIN
.vs/ClockifyReport/FileContentIndex/c1f04c47-302c-4b86-ad38-2a9f31ed5bf9.vsidx Переглянути файл


BIN
.vs/ClockifyReport/FileContentIndex/de52d55d-4860-4363-b682-d3b80493c461.vsidx Переглянути файл


BIN
.vs/ClockifyReport/FileContentIndex/merges/873d0248-7810-44ac-8788-3739deeecc8e.vsidx Переглянути файл


BIN
.vs/ClockifyReport/v16/.suo Переглянути файл


BIN
.vs/ClockifyReport/v16/TestStore/0/000.testlog Переглянути файл


BIN
.vs/ClockifyReport/v16/TestStore/0/testlog.manifest Переглянути файл


BIN
.vs/ClockifyReport/v17/.suo Переглянути файл


BIN
.vs/ProjectEvaluation/clockifyreport.metadata.v2 Переглянути файл


BIN
.vs/ProjectEvaluation/clockifyreport.projects.v2 Переглянути файл


+ 62
- 97
ClockifyReport/Business/ReportGenerator.cs Переглянути файл

using GemBox.Spreadsheet;
using Spire.Xls;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using ClockifyReport.Models; using ClockifyReport.Models;
using System.Drawing;


namespace BlackRockReportFunction.Bussines namespace BlackRockReportFunction.Bussines
{ {
public const string DefaultFontName = "DejaVu Sans"; public const string DefaultFontName = "DejaVu Sans";
public const int DefaultFontSize = 20; public const int DefaultFontSize = 20;


public static ExcelFile GenerateReportContent(Root reportObject)
public static Workbook GenerateReportContent(Root reportObject)
{ {
string licenseKey = Environment.GetEnvironmentVariable("GemBoxLicenseKey");
SpreadsheetInfo.SetLicense(licenseKey);

var excelFile = new ExcelFile();

var excelFile = new Workbook();
var ws = excelFile.Worksheets.Add("Content"); var ws = excelFile.Worksheets.Add("Content");


var sectionStyle = new CellStyle
{
Font =
{
Name = DefaultFontName,
Weight = ExcelFont.BoldWeight,
Size = DefaultFontSize * 12,
},
VerticalAlignment = VerticalAlignmentStyle.Center,
HorizontalAlignment = HorizontalAlignmentStyle.Left,
};

var mainDetailsStyle = new CellStyle
{
Font =
{
Name = DefaultFontName,
Weight = ExcelFont.BoldWeight,
Size = DefaultFontSize * 10,
},
VerticalAlignment = VerticalAlignmentStyle.Center,
};

var normalDetailsStyle = new CellStyle
{
Font =
{
Name = DefaultFontName,
Weight = ExcelFont.NormalWeight,
Size = DefaultFontSize * 10,
},
VerticalAlignment = VerticalAlignmentStyle.Center,
};

AddReportItems(reportObject, ws, sectionStyle, mainDetailsStyle, normalDetailsStyle);
AddReportItems(reportObject, ws);


// Autofit // Autofit
AutoFitReport(excelFile); AutoFitReport(excelFile);
return excelFile; return excelFile;
} }


public static void AddReportItems(Root reportObject, ExcelWorksheet ws, CellStyle sectionStyle, CellStyle mainDetailsStyle, CellStyle normalDetailsStyle)
public static void AddReportItems(Root reportObject, Worksheet ws)
{ {

TimeSpan t, time, time1; TimeSpan t, time, time1;
int row = 0;
int row = 1;
string[] sectionNames = { "User ", "Description ", "Time (h) ", "Time (decimal) " }; string[] sectionNames = { "User ", "Description ", "Time (h) ", "Time (decimal) " };
decimal totalAmountSum = 0; decimal totalAmountSum = 0;
List<string> usersDecimalHours = new List<string>(); List<string> usersDecimalHours = new List<string>();


for (int i = 0; i < sectionNames.Length; i++) for (int i = 0; i < sectionNames.Length; i++)
{ {
ws.Cells[row, i].Style = sectionStyle;
ws.Cells[row, i].Value = sectionNames[i];
ws.Cells[row, i].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Cells[row, i].Style.Borders[IndividualBorder.Bottom].LineStyle = LineStyle.Thick;
ws.Cells[row, i].Style.Borders[IndividualBorder.Bottom].LineColor = System.Drawing.Color.LightGray;
ws.Range[row, i + 1].Style.Font.FontName = "DejaVu Sans";
ws.Range[row, i + 1].Style.Font.Size = 240;
ws.Range[row, i + 1].Value = sectionNames[i];
ws.Range[row, i + 1].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
ws.Range[row, i + 1].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thick;
ws.Range[row, i + 1].Style.Borders[BordersLineType.EdgeBottom].Color = Color.LightGray;
} }


row++; row++;


foreach (var reportPerson in reportObject.groupOne) foreach (var reportPerson in reportObject.groupOne)
{ {
ws.Cells[row, 0].Style = mainDetailsStyle;
ws.Cells[row, 0].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Cells[row, 0].Value = reportPerson.name;
ws.Range[row, 1].Style.Font.FontName = "DejaVu Sans";
ws.Range[row, 1].Style.Font.Size = 200;
ws.Range[row, 1].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
ws.Range[row, 1].Value = reportPerson.name;


ws.Cells[row, 1].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Range[row, 2].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;


time = TimeSpan.FromSeconds(reportPerson.duration); time = TimeSpan.FromSeconds(reportPerson.duration);
var sumOfRecordHours = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(time.TotalHours), time.Minutes, time.Seconds); var sumOfRecordHours = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(time.TotalHours), time.Minutes, time.Seconds);
ws.Cells[row, 2].Style = mainDetailsStyle;
ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
ws.Cells[row, 2].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Cells[row, 2].Value = sumOfRecordHours;

ws.Cells[row, 3].Style = mainDetailsStyle;
ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
ws.Cells[row, 3].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Range[row, 3].Style.Font.FontName = "DejaVu Sans";
ws.Range[row, 3].Style.Font.Size = 200;
ws.Range[row, 3].Style.HorizontalAlignment = HorizontalAlignType.Right;
ws.Range[row, 3].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
ws.Range[row, 3].Value = sumOfRecordHours;

ws.Range[row, 4].Style.Font.FontName = "DejaVu Sans";
ws.Range[row, 4].Style.Font.Size = 200;
ws.Range[row, 4].Style.HorizontalAlignment = HorizontalAlignType.Right;
ws.Range[row, 4].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
var userDecimalHours = Convert.ToDecimal(TimeSpan.FromSeconds(reportPerson.duration).TotalHours).ToString("0.00"); ; var userDecimalHours = Convert.ToDecimal(TimeSpan.FromSeconds(reportPerson.duration).TotalHours).ToString("0.00"); ;
ws.Cells[row, 3].Value = userDecimalHours;
ws.Range[row, 4].Value = userDecimalHours;
usersDecimalHours.Add(userDecimalHours); usersDecimalHours.Add(userDecimalHours);



row++; row++;


foreach (var personRecord in reportPerson.children) foreach (var personRecord in reportPerson.children)
{ {
ws.Cells[row, 0].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Range[row, 1].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;


ws.Cells[row, 1].Style = normalDetailsStyle;
ws.Cells[row, 1].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Cells[row, 1].Value = personRecord.name;
ws.Range[row, 2].Style.Font.FontName = "DejaVu Sans";
ws.Range[row, 2].Style.Font.Size = 200;
ws.Range[row, 2].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
ws.Range[row, 2].Value = personRecord.name;


ws.Cells[row, 2].Style = normalDetailsStyle;
ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
ws.Cells[row, 2].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Range[row, 3].Style.Font.FontName = "DejaVu Sans";
ws.Range[row, 3].Style.Font.Size = 200;
ws.Range[row, 3].Style.HorizontalAlignment = HorizontalAlignType.Right;
ws.Range[row, 3].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
t = TimeSpan.FromSeconds(personRecord.duration); t = TimeSpan.FromSeconds(personRecord.duration);
ws.Cells[row, 2].Value = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(t.TotalHours), t.Minutes, t.Seconds);

ws.Cells[row, 3].Style = normalDetailsStyle;
ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
ws.Cells[row, 3].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Cells[row, 3].Value = Convert.ToDecimal(TimeSpan.FromSeconds(personRecord.duration).TotalHours).ToString("0.00");

ws[row, 3].Value = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(t.TotalHours), t.Minutes, t.Seconds);


ws.Range[row, 4].Style.Font.FontName = "DejaVu Sans";
ws.Range[row, 4].Style.Font.Size = 200;
ws.Range[row, 4].Style.HorizontalAlignment = HorizontalAlignType.Right;
ws.Range[row, 4].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
ws.Range[row, 4].Value = Convert.ToDecimal(TimeSpan.FromSeconds(personRecord.duration).TotalHours).ToString("0.00");


row++; row++;
} }
} }




var totalSum = reportObject.totals.Select(total => total.totalTime).FirstOrDefault(); var totalSum = reportObject.totals.Select(total => total.totalTime).FirstOrDefault();


ws.Cells[row, 0].Style = mainDetailsStyle;
ws.Cells[row, 0].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Cells[row, 0].Value = "Total";
ws.Range[row, 1].Style.Font.FontName = "DejaVu Sans";
ws.Range[row, 1].Style.Font.Size = 200;
ws.Range[row, 1].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
ws.Range[row, 1].Value = "Total";


ws.Cells[row, 2].Style = mainDetailsStyle;
ws.Cells[row, 2].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
ws.Cells[row, 2].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Range[row, 3].Style.Font.FontName = "DejaVu Sans";
ws.Range[row, 3].Style.Font.Size = 200;
ws.Range[row, 3].Style.HorizontalAlignment = HorizontalAlignType.Right;
ws.Range[row, 3].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
time1 = t = TimeSpan.FromSeconds(totalSum); time1 = t = TimeSpan.FromSeconds(totalSum);
ws.Cells[row, 2].Value = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(time1.TotalHours), time1.Minutes, time1.Seconds);

ws.Cells[row, 3].Style = mainDetailsStyle;
ws.Cells[row, 3].Style.HorizontalAlignment = HorizontalAlignmentStyle.Right;
ws.Cells[row, 3].Style.Borders[IndividualBorder.Right].LineStyle = LineStyle.Thin;
ws.Cells[row, 3].Value = Convert.ToDecimal(TimeSpan.FromSeconds(totalSum).TotalHours).ToString("0.00"); ;

ws.Range[row, 3].Value = string.Format("{0:00}:{1:D2}:{2:D2}", Math.Floor(time1.TotalHours), time1.Minutes, time1.Seconds);


ws.Range[row, 4].Style.Font.FontName = "DejaVu Sans";
ws.Range[row, 4].Style.Font.Size = 200;
ws.Range[row, 4].Style.HorizontalAlignment = HorizontalAlignType.Right;
ws.Range[row, 4].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
ws.Range[row, 4].Value = Convert.ToDecimal(TimeSpan.FromSeconds(totalSum).TotalHours).ToString("0.00");
} }


public static void AutoFitReport(ExcelFile excelFile)
public static void AutoFitReport(Workbook excelFile)
{ {
foreach (var sheet in excelFile.Worksheets) foreach (var sheet in excelFile.Worksheets)
{ {
var columnCount = sheet.CalculateMaxUsedColumns();
var columnCount = sheet.Columns.Count();


for (int i = 0; i < columnCount; i++)
for (int i = 1; i < columnCount; i++)
{ {
sheet.Columns[i].AutoFit(1, sheet.Rows[0], sheet.Rows[sheet.Rows.Count - 1]);
sheet.Columns[i].AutoFitRows();
} }
} }
} }

+ 1
- 3
ClockifyReport/ClockifyReport.csproj Переглянути файл

<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion> <AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.12.0" /> <PackageReference Include="Azure.Storage.Blobs" Version="12.12.0" />
<PackageReference Include="Azure.Storage.Queues" Version="12.10.0" /> <PackageReference Include="Azure.Storage.Queues" Version="12.10.0" />
<PackageReference Include="GemBox.Spreadsheet" Version="37.3.30.1110" />
<PackageReference Include="MailKit" Version="3.3.0" /> <PackageReference Include="MailKit" Version="3.3.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" /> <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid" Version="3.0.2" /> <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid" Version="3.0.2" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.2.0" /> <PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.2.0" />
<PackageReference Include="RestSharp" Version="108.0.1" /> <PackageReference Include="RestSharp" Version="108.0.1" />
<PackageReference Include="Spire.XLS" Version="12.7.1" />
<PackageReference Include="System.Core" Version="3.5.21022.801" /> <PackageReference Include="System.Core" Version="3.5.21022.801" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" /> <PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="System.IO.Packaging" Version="6.0.0" /> <PackageReference Include="System.IO.Packaging" Version="6.0.0" />

+ 1
- 1
ClockifyReport/Function1.cs Переглянути файл

} }
[Function("Function1")] [Function("Function1")]
[QueueOutput("clockify-queue")] [QueueOutput("clockify-queue")]
public string Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
public string Run([TimerTrigger("*/20 * * * * *")]TimerInfo myTimer, ILogger log)
{ {
InitializeClockifyIntegration(); // Clockify API Integration InitializeClockifyIntegration(); // Clockify API Integration

+ 3
- 6
ClockifyReport/Function2.cs Переглянути файл

using Azure.Storage.Blobs; using Azure.Storage.Blobs;
using BlackRockReportFunction.Bussines; using BlackRockReportFunction.Bussines;
using ClockifyReport.Models; using ClockifyReport.Models;
using GemBox.Spreadsheet;
using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Spire.Xls;
//using Microsoft.Azure.WebJobs; //using Microsoft.Azure.WebJobs;


namespace ClockifyReport namespace ClockifyReport
_logger.LogInformation(connection); _logger.LogInformation(connection);


var blobClient = new BlobContainerClient(connection, containerName); var blobClient = new BlobContainerClient(connection, containerName);
var blob = blobClient.GetBlobClient($"BlackRockReport_{DateTime.Now.ToString("f")}.xlsx");
var blob = blobClient.GetBlobClient($"BlackRockReport_{DateTime.Now.ToString("f")}.xls");


MemoryStream memoryStream = new MemoryStream(); MemoryStream memoryStream = new MemoryStream();
reportFile.Save(memoryStream, new XlsxSaveOptions
{
Type = XlsxType.Xlsx
});
reportFile.SaveToStream(memoryStream);


memoryStream.Position = 0; memoryStream.Position = 0;
await blob.UploadAsync(memoryStream); await blob.UploadAsync(memoryStream);

Завантаження…
Відмінити
Зберегти