Files
jdescopingtool/OLD/WebInterface/Helpers/ExcelTemplateGenerator.cs
T
Joseph Doherty 26ff8d9b4f Initial commit: JDE Scoping Tool migration project
Set up repository with legacy .NET Framework 4.8 source (OLD/),
new .NET 10 Blazor solution (NEW/), OpenSpec specifications,
documentation, and project configuration.
2026-01-02 07:43:29 -05:00

100 lines
3.8 KiB
C#
Executable File

using System.Collections.Generic;
using System.Drawing;
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
namespace WebInterface.Helpers
{
/// <summary>
/// Lot # / work order # template generator
/// </summary>
public class ExcelTemplateGenerator
{
/// <summary>
/// Generates Excel spreadsheet data entry template
/// </summary>
/// <typeparam name="T">Data type of source data</typeparam>
/// <param name="sourceData">Source data to display in template</param>
/// <param name="headerText">Column header text</param>
/// <returns>Generated spreadsheet</returns>
public static byte[] Generate<T>(List<T> sourceData, string headerText)
{
//Create worksheet to hold data
ExcelPackage package = new ExcelPackage();
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Data Entry Template");
//Write header
worksheet.Cells[1, 1].Value = headerText;
worksheet.Cells[1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
worksheet.Cells[1, 1].Style.Font.Bold = true;
worksheet.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.Gainsboro);
worksheet.Column(1).Width = 45;
//Write uploaded data
if (sourceData != null)
{
int row = 2;
foreach (T element in sourceData)
{
worksheet.Cells[row++, 1].Value = element;
}
}
//Write result
MemoryStream fileStream = new MemoryStream();
package.SaveAs(fileStream);
fileStream.Position = 0;
byte[] data = fileStream.ToArray();
return data;
}
/// <summary>
/// Generates Excel spreadsheet data entry template
/// </summary>
/// <param name="sourceData">Source data to display in template</param>
/// <param name="headerText">Column header text</param>
/// <returns>Generated spreadsheet</returns>
public static byte[] Generate(object[][] sourceData, string[] headerText)
{
//Create worksheet to hold data
ExcelPackage package = new ExcelPackage();
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Data Entry Template");
int numColumns = headerText.Length;
//Write header
ExcelRange header = worksheet.Cells[1, 1, 1, numColumns];
header.LoadFromArrays(new List<object[]> { headerText });
header.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
header.Style.Font.Bold = true;
header.Style.Fill.PatternType = ExcelFillStyle.Solid;
header.Style.Fill.BackgroundColor.SetColor(Color.Gainsboro);
//Write uploaded data
if (sourceData != null && sourceData.Length > 0)
{
ExcelRange dataRange = worksheet.Cells[2, 1, sourceData.Length + 2, numColumns];
dataRange.LoadFromArrays(sourceData);
}
//Size columns
for (int col = 1; col <= numColumns; col++)
{
worksheet.Column(col).Width = 65;
worksheet.Column(col).Style.Numberformat.Format = "@";
}
//Write result
MemoryStream fileStream = new MemoryStream();
package.SaveAs(fileStream);
fileStream.Position = 0;
byte[] data = fileStream.ToArray();
return data;
}
}
}