-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsvToXlsx.cs
127 lines (106 loc) · 3.8 KB
/
CsvToXlsx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.IO;
using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;
using ClosedXML.Excel;
class Program
{
static void Main(string[] args)
{
try
{
// Replace these with your actual input and output file paths
string inputCsvPath = @"input.csv";
string outputXlsxPath = @"output.xlsx";
// Convert CSV to XLSX
ConvertCsvToXlsx(inputCsvPath, outputXlsxPath);
Console.WriteLine($"Successfully converted {inputCsvPath} to {outputXlsxPath}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
static void ConvertCsvToXlsx(string inputCsvPath, string outputXlsxPath)
{
// Configure CSV reading with pipe delimiter
var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = "|",
HasHeaderRecord = true
};
// Read CSV data
using var reader = new StreamReader(inputCsvPath);
using var csv = new CsvReader(reader, csvConfig);
// Create a dynamic list of records
var records = csv.GetRecords<dynamic>().ToList();
// Create Excel workbook
using var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Data");
// Write headers
if (records.Any())
{
var headers = ((IDictionary<string, object>)records[0]).Keys.ToList();
for (int i = 0; i < headers.Count; i++)
{
worksheet.Cell(1, i + 1).Value = headers[i];
}
// Write data rows
for (int row = 0; row < records.Count; row++)
{
var record = (IDictionary<string, object>)records[row];
for (int col = 0; col < headers.Count; col++)
{
worksheet.Cell(row + 2, col + 1).Value = record[headers[col]];
}
}
}
// Save the workbook
workbook.SaveAs(outputXlsxPath);
}
static void ConvertCsvToXlsxStream(string inputCsvPath, string outputXlsxPath)
{
// Configure CSV reading with pipe delimiter
var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = "|",
HasHeaderRecord = true
};
// Create Excel workbook
using var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Data");
// Read CSV data using IEnumerable directly
using var reader = new StreamReader(inputCsvPath);
using var csv = new CsvReader(reader, csvConfig);
// Get the records as an IEnumerable
var records = csv.GetRecords<dynamic>();
// Flag to write headers only once
bool headersWritten = false;
// Iterate through records efficiently
foreach (var record in records)
{
// Write headers on first iteration
if (!headersWritten)
{
var headers = ((IDictionary<string, object>)record).Keys.ToList();
for (int i = 0; i < headers.Count; i++)
{
worksheet.Cell(1, i + 1).Value = headers[i];
}
headersWritten = true;
}
// Write data rows
int currentRow = worksheet.LastRowUsed()?.RowNumber() + 1 ?? 2;
var recordDict = (IDictionary<string, object>)record;
int colIndex = 1;
foreach (var value in recordDict.Values)
{
worksheet.Cell(currentRow, colIndex).Value = value;
colIndex++;
}
}
// Save the workbook
workbook.SaveAs(outputXlsxPath);
}
}