![]() |
VOOZH | about |
dotnet add package ZidUtilities.CommonCode.Files --version 1.0.7
NuGet\Install-Package ZidUtilities.CommonCode.Files -Version 1.0.7
<PackageReference Include="ZidUtilities.CommonCode.Files" Version="1.0.7" />
<PackageVersion Include="ZidUtilities.CommonCode.Files" Version="1.0.7" />Directory.Packages.props
<PackageReference Include="ZidUtilities.CommonCode.Files" />Project file
paket add ZidUtilities.CommonCode.Files --version 1.0.7
#r "nuget: ZidUtilities.CommonCode.Files, 1.0.7"
#:package ZidUtilities.CommonCode.Files@1.0.7
#addin nuget:?package=ZidUtilities.CommonCode.Files&version=1.0.7Install as a Cake Addin
#tool nuget:?package=ZidUtilities.CommonCode.Files&version=1.0.7Install as a Cake Tool
File import and export utilities supporting multiple formats including Excel, CSV, TXT, and HTML.
Predefined Excel workbook themes:
.NET Framework 4.8
using ZidUtilities.CommonCode.Files;
using System.Data;
// Create a DataTable with sample data
DataTable dt = new DataTable("Employees");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Department", typeof(string));
dt.Columns.Add("Salary", typeof(decimal));
dt.Rows.Add(1, "John Doe", "IT", 75000);
dt.Rows.Add(2, "Jane Smith", "HR", 65000);
dt.Rows.Add(3, "Bob Johnson", "Sales", 70000);
// Create DataSet for export
DataSet ds = new DataSet();
ds.Tables.Add(dt);
// Configure and export to Excel
DataExporter exporter = new DataExporter
{
ExportType = ExportTo.XLSX,
ExportExcelStyle = ExcelStyle.Ocean,
WriteHeaders = true,
ExportWithStyles = true,
UseAlternateRowStyles = true,
AutoCellAdjust = WidthAdjust.ByHeaders,
Author = "Your Name",
Company = "Your Company",
Title = "Employee Report"
};
// Export to file
exporter.ExportDataToFile(ds, @"C:\temp\employees.xlsx");
DataExporter csvExporter = new DataExporter
{
ExportType = ExportTo.CSV,
Separator = ",",
WriteHeaders = true
};
csvExporter.ExportDataToFile(ds, @"C:\temp\employees.csv");
DataExporter htmlExporter = new DataExporter
{
ExportType = ExportTo.HTML,
ExportHtmlStyle = ExcelStyle.Corporate,
WriteHeaders = true
};
htmlExporter.ExportDataToFile(ds, @"C:\temp\employees.html");
DataExporter txtExporter = new DataExporter
{
ExportType = ExportTo.TXT,
DelimitedByLenght = true,
CharFiller = ' ',
Widhts = new List<int> { 10, 30, 20, 15 }, // Column widths
WriteHeaders = true
};
txtExporter.ExportDataToFile(ds, @"C:\temp\employees.txt");
using (MemoryStream stream = new MemoryStream())
{
DataExporter exporter = new DataExporter
{
ExportType = ExportTo.XLSX,
ExportExcelStyle = ExcelStyle.Default,
WriteHeaders = true,
ExportWithStyles = true
};
exporter.ExportDataToStream(ds, stream);
// Stream now contains the Excel file data
byte[] fileBytes = stream.ToArray();
}
DataExporter exporter = new DataExporter
{
ExportType = ExportTo.XLSX,
ExportExcelStyle = ExcelStyle.Forest
};
exporter.OnStartExportation += (firedAt, records, progress, exportType) =>
{
Console.WriteLine($"Export started at {firedAt} with {records} records");
};
exporter.OnProgress += (firedAt, records, progress, exportType) =>
{
Console.WriteLine($"Progress: {progress}%");
};
exporter.OnCompletedExportation += (firedAt, records, exportType, stream, path) =>
{
Console.WriteLine($"Export completed at {firedAt}. Saved to {path}");
};
exporter.ExportDataToFile(ds, @"C:\temp\output.xlsx");
DataExporter exporter = new DataExporter
{
ExportType = ExportTo.XLSX,
IgnoredColumns = new List<string> { "Password", "SSN", "InternalID" },
WriteHeaders = true
};
exporter.ExportDataToFile(ds, @"C:\temp\safe_export.xlsx");
DataExporter exporter = new DataExporter
{
ExportType = ExportTo.XLSX,
ExportWithStyles = true,
Remarks = new List<CellRemark>
{
new CellRemark
{
Row = 2,
Column = 4,
Comment = "This salary requires manager approval",
Style = ExcelCellStyle.Alert
}
}
};
exporter.ExportDataToFile(ds, @"C:\temp\employees_with_notes.xlsx");
Apply semantic styles to specific cells:
The DataImporter class provides robust data import capabilities from multiple file formats with automatic type conversion, error handling, and progress tracking.
Defines the source file format:
Defines how fields are separated:
Supported data types for automatic conversion:
The DataStructure class defines the schema of your imported data:
public class DataStructure
{
public string Name { get; set; } // Table name
public List<Field> Fields { get; set; } // Column definitions
}
public class Field
{
public string Name { get; set; } // Column name
public bool Nullable { get; set; } // Allow null values
public DataImporter.FieldType FieldType { get; set; } // Data type
public int Length { get; set; } // For fixed-width or max length
}
Track import progress with these events:
using ZidUtilities.CommonCode.Files;
using System.Data;
// Simple CSV import with automatic schema detection
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.CSV,
FileName = @"C:\data\employees.csv",
FileHeader = true, // First row contains column names
SeparatorChar = ',' // Comma separator
};
// Import the data
bool success = importer.ImportFromFile();
if (success && importer.WasCleanExecution)
{
DataTable result = importer.ImportResultDataTable;
Console.WriteLine($"Imported {result.Rows.Count} rows with {result.Columns.Count} columns");
// Access the data
foreach (DataRow row in result.Rows)
{
Console.WriteLine($"{row["Name"]} - {row["Email"]}");
}
}
else
{
// Handle errors
foreach (var error in importer.Errors)
{
Console.WriteLine($"Error at row {error.Location}: {error.Description}");
}
}
// Define the data structure
DataStructure schema = new DataStructure
{
Name = "Products",
Fields = new List<Field>
{
new Field("ProductID", nullable: false, DataImporter.FieldType.Integer),
new Field("ProductName", nullable: false, DataImporter.FieldType.String, length: 100),
new Field("Price", nullable: false, DataImporter.FieldType.FloatingPoint),
new Field("InStock", nullable: false, DataImporter.FieldType.Bit),
new Field("LastUpdated", nullable: true, DataImporter.FieldType.Date)
}
};
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.XLSX,
FileName = @"C:\data\products.xlsx",
SheetName = "ProductData", // Specific sheet name
FileHeader = true,
DataStructure = schema // Use custom schema
};
bool success = importer.ImportFromFile();
if (success)
{
DataTable products = importer.ImportResultDataTable;
// Process the data
foreach (DataRow row in products.Rows)
{
int id = Convert.ToInt32(row["ProductID"]);
string name = row["ProductName"].ToString();
float price = Convert.ToSingle(row["Price"]);
bool inStock = Convert.ToBoolean(row["InStock"]);
Console.WriteLine($"{id}: {name} - ${price} (In Stock: {inStock})");
}
}
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.TXT,
FileName = @"C:\data\data.txt",
Separator = DataImporter.Delimiter.Tab, // Tab-delimited
FileHeader = true
};
// Let the importer automatically detect the schema from headers
bool success = importer.ImportFromFile();
if (success && importer.WasCleanExecution)
{
DataTable result = importer.ImportResultDataTable;
// Display column information
Console.WriteLine("Columns:");
foreach (DataColumn col in result.Columns)
{
Console.WriteLine($" {col.ColumnName} ({col.DataType.Name})");
}
Console.WriteLine($"\nTotal rows: {result.Rows.Count}");
}
// Define fixed-width schema
DataStructure fixedSchema = new DataStructure
{
Name = "FixedWidthData",
Fields = new List<Field>
{
new Field("ID", nullable: false, DataImporter.FieldType.Integer, length: 10),
new Field("Name", nullable: false, DataImporter.FieldType.String, length: 30),
new Field("Department", nullable: false, DataImporter.FieldType.String, length: 20),
new Field("Salary", nullable: false, DataImporter.FieldType.FloatingPoint, length: 15)
}
};
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.TXT,
FileName = @"C:\data\fixed_width.txt",
Separator = DataImporter.Delimiter.Lenght, // Fixed-width columns
FillerChar = ' ', // Space padding
FileHeader = false, // No header row
DataStructure = fixedSchema
};
bool success = importer.ImportFromFile();
if (success)
{
DataTable result = importer.ImportResultDataTable;
Console.WriteLine($"Imported {result.Rows.Count} fixed-width records");
}
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.TXT,
FileName = @"C:\data\pipe_delimited.txt",
Separator = DataImporter.Delimiter.Separator,
SeparatorChar = '|', // Pipe separator
FileHeader = true
};
bool success = importer.ImportFromFile();
if (success)
{
DataTable result = importer.ImportResultDataTable;
foreach (DataRow row in result.Rows)
{
// Process pipe-delimited data
Console.WriteLine(string.Join(" | ", row.ItemArray));
}
}
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.XLSX,
FileName = @"C:\data\large_file.xlsx",
FileHeader = true
};
// Subscribe to events
importer.OnStartImportation += (firedAt, records, progress, importType) =>
{
Console.WriteLine($"Import started at {firedAt}");
Console.WriteLine($"Total records to process: {records}");
};
importer.OnProgress += (firedAt, records, progress, importType) =>
{
int percentage = (int)((progress * 100.0) / records);
Console.WriteLine($"Progress: {percentage}% ({progress}/{records} rows)");
};
importer.OnCompletedImportation += (firedAt, records, importType, pathResult) =>
{
Console.WriteLine($"Import completed at {firedAt}");
Console.WriteLine($"Processed {records} records from {pathResult}");
};
// Import asynchronously (runs on background thread)
bool started = importer.ImportFromFile(GoAsync: true);
if (started)
{
// Continue with other work while import runs in background
Console.WriteLine("Import running in background...");
// Wait for completion or do other work
while (importer.ImportResultDataTable == null)
{
System.Threading.Thread.Sleep(100);
}
Console.WriteLine($"Final result: {importer.ImportResultDataTable.Rows.Count} rows");
}
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.XLSX,
FileName = @"C:\data\workbook.xlsx",
// SheetName not specified - will use first sheet automatically
FileHeader = true
};
bool success = importer.ImportFromFile();
if (success)
{
DataTable result = importer.ImportResultDataTable;
Console.WriteLine($"Imported from sheet: {result.TableName}");
Console.WriteLine($"Rows: {result.Rows.Count}, Columns: {result.Columns.Count}");
}
DataStructure schema = new DataStructure
{
Name = "Users",
Fields = new List<Field>
{
new Field("UserID", nullable: false, DataImporter.FieldType.Integer),
new Field("Username", nullable: false, DataImporter.FieldType.String, length: 50),
new Field("Email", nullable: false, DataImporter.FieldType.String, length: 100),
new Field("Age", nullable: true, DataImporter.FieldType.Integer),
new Field("IsActive", nullable: false, DataImporter.FieldType.Bit)
}
};
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.CSV,
FileName = @"C:\data\users.csv",
FileHeader = true,
DataStructure = schema
};
bool success = importer.ImportFromFile();
if (success)
{
if (importer.WasCleanExecution)
{
Console.WriteLine("Import completed without errors!");
DataTable users = importer.ImportResultDataTable;
Console.WriteLine($"Successfully imported {users.Rows.Count} users");
}
else
{
Console.WriteLine("Import completed with errors:");
foreach (ErorrInfo error in importer.Errors)
{
Console.WriteLine($" Row {error.Location}: {error.Description}");
}
// You can still access the partial data
DataTable partialData = importer.ImportResultDataTable;
Console.WriteLine($"Partial import: {partialData.Rows.Count} rows imported successfully");
}
}
else
{
Console.WriteLine("Import failed to start. Check file path and format.");
}
The CSV importer properly handles quoted fields that contain commas:
// Sample CSV content:
// Name,Address,City
// "John Doe","123 Main St, Apt 4B",Springfield
// "Jane Smith","456 Oak Ave, Suite 200",Portland
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.CSV,
FileName = @"C:\data\addresses.csv",
FileHeader = true
};
bool success = importer.ImportFromFile();
if (success)
{
DataTable result = importer.ImportResultDataTable;
foreach (DataRow row in result.Rows)
{
// Commas inside quoted fields are preserved
Console.WriteLine($"{row["Name"]} lives at {row["Address"]}, {row["City"]}");
}
}
using ZidUtilities.CommonCode.DataAccess;
// Import from file
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.XLSX,
FileName = @"C:\data\orders.xlsx",
FileHeader = true
};
if (importer.ImportFromFile() && importer.WasCleanExecution)
{
DataTable importedData = importer.ImportResultDataTable;
// Insert into database using SqlConnector
SqlConnector connector = new SqlConnector(connectionString);
foreach (DataRow row in importedData.Rows)
{
string insertQuery = @"
INSERT INTO Orders (OrderID, CustomerName, Amount, OrderDate)
VALUES (@orderId, @customerName, @amount, @orderDate)";
var parameters = new Dictionary<string, string>
{
{ "@orderId", row["OrderID"].ToString() },
{ "@customerName", row["CustomerName"].ToString() },
{ "@amount", row["Amount"].ToString() },
{ "@orderDate", row["OrderDate"].ToString() }
};
var response = connector.ExecuteNonQuery(insertQuery, parameters);
if (!response.IsOK)
{
Console.WriteLine($"Failed to insert order {row["OrderID"]}: {response.Message}");
}
}
}
// If you have an existing DataTable as a template
DataTable template = GetTemplateTable(); // Your existing table
// Create DataStructure from it
DataStructure schema = new DataStructure(template);
// Use it for import
DataImporter importer = new DataImporter
{
ImportType = DataImporter.ImportFrom.CSV,
FileName = @"C:\data\newdata.csv",
FileHeader = true,
DataStructure = schema // Uses same structure as template
};
bool success = importer.ImportFromFile();
using ZidUtilities.CommonCode.Files;
using System;
using System.Data;
using System.IO;
public class ImportManager
{
public DataTable ImportFile(string filePath, bool showProgress = true)
{
// Determine file type from extension
string extension = Path.GetExtension(filePath).ToLower();
DataImporter.ImportFrom importType;
switch (extension)
{
case ".csv":
importType = DataImporter.ImportFrom.CSV;
break;
case ".txt":
importType = DataImporter.ImportFrom.TXT;
break;
case ".xls":
importType = DataImporter.ImportFrom.XLS;
break;
case ".xlsx":
importType = DataImporter.ImportFrom.XLSX;
break;
default:
throw new ArgumentException($"Unsupported file type: {extension}");
}
// Create importer
DataImporter importer = new DataImporter
{
ImportType = importType,
FileName = filePath,
FileHeader = true
};
// Add progress tracking if requested
if (showProgress)
{
importer.OnStartImportation += (time, records, prog, type) =>
{
Console.WriteLine($"Starting import of {records} records...");
};
importer.OnProgress += (time, records, prog, type) =>
{
int percentage = (int)((prog * 100.0) / records);
Console.Write($"\rProgress: {percentage}%");
};
importer.OnCompletedImportation += (time, records, type, path) =>
{
Console.WriteLine($"\nImport completed: {records} records");
};
}
// Perform import
bool success = importer.ImportFromFile();
if (!success)
{
throw new Exception("Import failed to start");
}
if (!importer.WasCleanExecution)
{
Console.WriteLine("\nWarning: Import completed with errors:");
foreach (var error in importer.Errors)
{
Console.WriteLine($" Row {error.Location}: {error.Description}");
}
}
return importer.ImportResultDataTable;
}
}
// Usage
ImportManager manager = new ImportManager();
DataTable data = manager.ImportFile(@"C:\data\sales.xlsx", showProgress: true);
Console.WriteLine($"Imported {data.Rows.Count} rows");
WidthAdjust.ByHeaders or WidthAdjust.ByFirst100Rows for exportsExportWithStyles = false for faster exportsGoAsync = true) for files over 10,000 rows| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET Framework | net48 net48 is compatible. net481 net481 was computed. |
Showing the top 1 NuGet packages that depend on ZidUtilities.CommonCode.Files:
| Package | Downloads |
|---|---|
|
ZidUtilities.CommonCode.ICSharpTextEditor
Provides a custom control that facilitates the usage of the control ICSharpCode.TextEditor.TextEditorControl. |
This package is not used by any popular GitHub repositories.
Proper reading of CSV files(fields with commas, quoted fields, escaped quotes, embedded new lines) .