3.5.2 commit

This commit is contained in:
Doug Macintosh
2026-03-08 16:20:06 -04:00
parent a59a047cd1
commit bfe37f6426
26 changed files with 1998 additions and 102714 deletions

View File

@@ -4,13 +4,12 @@ using System.Data;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Spire.Xls;
namespace bdf
{
class CXLFile
{
//public static List<XLWorkbook> ServiceOrders = new List<XLWorkbook> { };
public static List<Spire.Xls.Workbook> S_ServiceOrders = new List<Spire.Xls.Workbook> { };
@@ -110,7 +109,7 @@ namespace bdf
Dictionary<string, (int version, string file)> best =
new Dictionary<string, (int, string)>();
foreach (var file in files)
foreach (var file in files) //todo should this be filtered instead?
{
string name = Path.GetFileName(file);
var match = regex.Match(name);
@@ -133,7 +132,7 @@ namespace bdf
if (!best.ContainsKey(serial)) // || ver > best[serial].version)
{
best[serial] = (ver, file);
Logger.Log(5, " Adding: {0} ver {1}", serial, ver);
Logger.Log(5, " Adding: {0} ver {1}", serial, ver); //todo open up file and verify so version from internals?
}
else if (ver > best[serial].version)
{
@@ -148,233 +147,5 @@ namespace bdf
return best.Values.Select(v => v.file).ToArray();
}
/*
public static void getXLS(string task, string dir)
{
//string folderPath = @"C:\Data\ExcelFiles"; // Folder to scan
string folderPath = dir;
string filePrefix1 = "ROGERS_";
string filePrefix2 = task + "_SO";
//List<XLWorkbook> ServiceOrders = new List<XLWorkbook> { };
try
{
// Required for ExcelDataReader in .NET 4
System.Text.Encoding.RegisterProvider(
System.Text.CodePagesEncodingProvider.Instance);
// Get all Excel files that match the prefix
//var excelFiles = Directory.GetFiles(folderPath, filePrefix1 + "*" + filePrefix2 + "*.xls*", SearchOption.TopDirectoryOnly);
var excelFiles = getLatestVersionFiles(dir, filePrefix1, filePrefix2);
if (excelFiles.Length == 0)
{
Console.WriteLine("No Excel files found with prefix: " + filePrefix1 + "*" + filePrefix2);
return;
}
DataSet _dataSet = null;
foreach (var filePath in excelFiles)
{
Logger.Log(1, "Scanning: {0}", Path.GetFileName(filePath));
// DataSet dataSet = ReadExcelFile(filePath);
// STEP 1: Read file into DataSet using ExcelDataReader
try
{
_dataSet = ReadExcelFile(filePath);
//_dataSet = LoadOneDriveDataSet(filePath);
}
catch
{
try
{
_dataSet = LoadOneDriveDataSet(filePath);
}
catch (Exception e)
{
Logger.Log(0, "Error trying to access file {0}. ({1})", filePath, e.Message);
}
}
// STEP 2: Convert DataSet into a ClosedXML workbook for manipulation
var workbook = new XLWorkbook();
//using (var workbook = new XLWorkbook())
{
foreach (DataTable table in _dataSet.Tables)
{
var worksheet = workbook.Worksheets.Add(table.TableName);
// Insert headers
for (int col = 0; col < table.Columns.Count; col++)
{
worksheet.Cell(1, col + 1).Value = table.Columns[col].ColumnName;
worksheet.Cell(1, col + 1).Style.Font.Bold = true;
}
// Insert rows
for (int row = 0; row < table.Rows.Count; row++)
{
for (int col = 0; col < table.Columns.Count; col++)
{
//Logger.Log(5, "type is {0}", table.Rows[row][col].GetType().Name);
string valuetype = table.Rows[row][col].GetType().Name;
if (valuetype == "Int32")
{
worksheet.Cell(row + 2, col + 1).Value = (Int32)table.Rows[row][col];
}
else if (valuetype == "String")
{
worksheet.Cell(row + 2, col + 1).Value = (String)table.Rows[row][col];
}
else if (valuetype == "Decimal")
{
worksheet.Cell(row + 2, col + 1).Value = (Decimal)table.Rows[row][col];
}
else if (valuetype == "Double")
{
worksheet.Cell(row + 2, col + 1).Value = (Double)table.Rows[row][col];
}
else if (valuetype == "DBNull")
{
worksheet.Cell(row + 2, col + 1).Value = "";
}
else
{
worksheet.Cell(row + 2, col + 1).Value = "fix me";
}
//worksheet.Cell(row + 2, col + 1).Value = table.Rows[row][col];
}
}
worksheet.Columns().AdjustToContents();
//Logger.Log(0, "Sheet{0} Value={1}", workbook.Worksheets.Count, workbook.Worksheet(workbook.Worksheets.Count).Cell("C2").Value);
}
// Example: Save modified workbook (optional)
string outputFile = Path.Combine(folderPath, "" + Path.GetFileName(filePath) + "x");
//workbook.SaveAs(outputFile);
Logger.Log(7, "Processed file: {0}", outputFile);
//Logger.Log(0, "Book has {0} sheets", workbook.Worksheets.Count );
CXLFile.ServiceOrders.Add(workbook);
//Logger.Log("test: " + workbook.Worksheet(1).Cell("C2").Value);
}
//Logger.Log("Finished: " + Path.GetFileName(filePath));
//Logger.Log(new string('-', 50));
}
Logger.Log("All SO Excel files processed successfully.");
}
catch (Exception ex)
{
Logger.Log(0, "SO processing error: {0}" + ex.Message);
//Logger.Log("Press any key to exit...");
//Console.ReadKey();
}
//
//return ServiceOrders;
}
/// <summary>
/// Reads an Excel file into a DataSet using ExcelDataReader.
/// Each worksheet becomes a DataTable.
/// </summary>
static DataSet ReadExcelFile(string filePath)
{
// Required for ExcelDataReader to process XLS files
System.Text.Encoding.RegisterProvider(
System.Text.CodePagesEncodingProvider.Instance);
using (var stream = System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read))
{
IExcelDataReader reader;
// Auto-detect format (XLS or XLSX)
if (Path.GetExtension(filePath).Equals(".xls", StringComparison.OrdinalIgnoreCase))
reader = ExcelReaderFactory.CreateBinaryReader(stream);
else
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
// Convert to DataSet (with first row as header)
var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = _ => new ExcelDataTableConfiguration()
{
UseHeaderRow = true
}
});
reader.Close();
return result;
}
}
public static DataSet LoadOneDriveDataSet(string path)
{
if (!System.IO.File.Exists(path))
throw new FileNotFoundException("OneDrive file not found.", path);
// Required for ExcelDataReader to process XLS files
System.Text.Encoding.RegisterProvider(
System.Text.CodePagesEncodingProvider.Instance);
FileStream stream = null;
// OneDrive sync engine frequently locks files briefly:
const int maxRetries = 12;
const int retryDelayMs = 150;
for (int i = 0; i < maxRetries; i++)
{
try
{
stream = new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite | FileShare.Delete
);
break; // opened successfully
}
catch (IOException)
{
if (i == maxRetries - 1)
throw;
System.Threading.Thread.Sleep(retryDelayMs);
}
}
// STEP 1: Read the workbook into a DataSet (ExcelDataReader)
DataSet dataSet;
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
dataSet = reader.AsDataSet(new ExcelDataSetConfiguration
{
ConfigureDataTable = _ => new ExcelDataTableConfiguration
{
UseHeaderRow = true
}
});
}
return dataSet;
}
*/
}
}