c#中,当大量的CSV文件转化为.xlsx文件,教好的方法
时间: 2024-03-13 17:47:15 浏览: 211
以下是将大量CSV文件转换为XLSX文件的一些建议和示例代码:
1. 使用OpenXml SDK
OpenXml SDK是一组用于处理Office文档的.NET库。你可以使用它来创建、读取和修改XLSX文件。它提供了一个非常高效的API来处理大型XLSX文件,因此可以很好地处理大量的CSV文件。
以下是使用OpenXml SDK将一个CSV文件转换为XLSX文件的示例代码:
```csharp
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
public static void ConvertCsvToXlsx(string csvPath, string xlsxPath)
{
// Create a new XLSX file
using (SpreadsheetDocument document = SpreadsheetDocument.Create(xlsxPath, SpreadsheetDocumentType.Workbook))
{
// Add a new workbook
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
// Add a new worksheet
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Open the CSV file
using (StreamReader reader = new StreamReader(csvPath))
{
// Read each line of the CSV file and add it to the XLSX worksheet
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] values = line.Split(',');
Row row = new Row();
for (int i = 0; i < values.Length; i++)
{
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(values[i]);
row.Append(cell);
}
worksheetPart.Worksheet.GetFirstChild<SheetData>().AppendChild(row);
}
}
// Add a new sheet to the workbook
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };
sheets.Append(sheet);
}
}
```
2. 使用CsvHelper库
CsvHelper是一个.NET库,它提供了一组API来读写CSV文件。它非常高效,并且可以处理大量的CSV文件。你可以使用它来读取CSV文件,并将其转换为XLSX文件。
以下是使用CsvHelper将一个CSV文件转换为XLSX文件的示例代码:
```csharp
using System.Collections.Generic;
using System.IO;
using CsvHelper;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
public static void ConvertCsvToXlsx(string csvPath, string xlsxPath)
{
// Create a new XLSX file
using (SpreadsheetDocument document = SpreadsheetDocument.Create(xlsxPath, SpreadsheetDocumentType.Workbook))
{
// Add a new workbook
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
// Add a new worksheet
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Read the CSV file using CsvHelper
using (StreamReader reader = new StreamReader(csvPath))
{
CsvReader csvReader = new CsvReader(reader);
csvReader.Configuration.HasHeaderRecord = false;
while (csvReader.Read())
{
Row row = new Row();
for (int i = 0; i < csvReader.FieldHeaders.Length; i++)
{
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(csvReader.GetField(i));
row.Append(cell);
}
worksheetPart.Worksheet.GetFirstChild<SheetData>().AppendChild(row);
}
}
// Add a new sheet to the workbook
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };
sheets.Append(sheet);
}
}
```
这些示例代码可以将一个CSV文件转换为XLSX文件。如果你有多个CSV文件,可以使用循环来处理它们。
注意,以上示例代码可能不适用于所有类型的CSV文件。你需要根据你的CSV文件格式进行调整。
阅读全文