国产Excel开发组件Spire.XLS【转换】教程(6):将 Excel 转换为 CSV 并将 CSV 转换为 Excel

将学习如何在 C# 和 VB.NET 中使用Spire.XLS for .NET库以编程方式实现此功能。

CSV(逗号分隔值)文件是包含用逗号分隔的数据的纯文本文件。它广泛用于将数据从一个应用程序导入或导出到另一个应用程序。在某些情况下,您可能需要在 CSV 和 Excel 之间进行转换。在本文中,您将学习如何在 C# 和 VB.NET 中使用Spire.XLS for .NET库以编程方式实现此功能。

第 1 步为 .NET 安装 Spire.XLS

我在 MS Excel 中创建了一个新的 Excel 文件,并在第一张表中添加了一些具有不同格式的数据,这是创建文件的屏幕截图。

Excel 到图像
第 2 步:在 C# 和 VB.NET 中将 Excel 转换为 CSV

以下是将 Excel 转换为 CSV 的步骤:

  • 创建Workbook类的实例。
  • 使用Workbook.LoadFromFile()方法加载 Excel 文件。
  • 使用Workbook.Worksheets[index]属性通过索引获取所需的工作表。
  • 使用XlsWorksheet.SaveToFile()方法将工作表保存为 CSV 。您可以选择以下重载的 SaveToFile() 方法之一:
    • SaveToFile(字符串文件名,字符串分隔符)
    • SaveToFile(字符串文件名,字符串分隔符,编码编码)
    • SaveToFile(字符串文件名,字符串分隔符,布尔保留隐藏数据)

【C#】

using Spire.Xls;using System.Text;namespace ConvertAWorksheetToCsv{class Program{static void Main(string[] args){//Create an instance of Workbook classWorkbook workbook = new Workbook();//Load an Excel fileworkbook.LoadFromFile("Sample.xlsx");//Get the first worksheetWorksheet sheet = workbook.Worksheets[0];//Save the worksheet as CSVsheet.SaveToFile("ExcelToCSV.csv", ",", Encoding.UTF8);}}}

【VB.NET】

Imports Spire.XlsImports System.TextNamespace ConvertAWorksheetToCsvFriend Class ProgramPrivate Shared Sub Main(ByVal args As String())'Create an instance of Workbook classDim workbook As Workbook = New Workbook()'Load an Excel fileworkbook.LoadFromFile("Sample.xlsx")'Get the first worksheetDim sheet As Worksheet = workbook.Worksheets(0)'Save the worksheet as CSVsheet.SaveToFile("ExcelToCSV.csv", ",", Encoding.UTF8)End SubEnd ClassEnd Namespace

C#/VB.NET:将 Excel 转换为 CSV,反之亦然
第 3 步:在 C# 和 VB.NET 中将 CSV 转换为 Excel

以下是将 CSV 转换为 Excel 的步骤:

  • 创建Workbook类的实例。
  • 使用Workbook.LoadFromFile(string fileName, string separator, int startRow, int startColumn)方法加载 CSV 文件。
  • 使用Workbook.Worksheets[index]属性通过索引获取所需的工作表。
  • 使用Worksheet.AllocatedRange属性访问工作表的使用范围。然后将CellRange.IgnoreErrorOptions属性设置为IgnoreErrorType.NumberAsText以忽略可能的错误,同时将范围内的数字保存为文本。
  • 使用CellRange.AutoFitColumns()CellRange.AutoFitRows()方法自动调整列和行。
  • 使用Workbook.SaveToFile(string fileName, ExcelVersion version)方法将 CSV 保存到 Excel 。

【C#】

using Spire.Xls;namespace ConvertCsvToExcel{class Program{static void Main(string[] args){//Create an instance of Workbook classWorkbook workbook = new Workbook();//Load a CSV fileworkbook.LoadFromFile(@"ExcelToCSV.csv", ",", 1, 1);//Get the first worksheetWorksheet sheet = workbook.Worksheets[0];//Access the used range in the worksheetCellRange usedRange = sheet.AllocatedRange;//Ignore errors when saving numbers in the range as textusedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText;//Autofit columns and rowsusedRange.AutoFitColumns();usedRange.AutoFitRows();//Save the result fileworkbook.SaveToFile("CSVToExcel.xlsx", ExcelVersion.Version2013);}}}

【VB.NET】

Imports Spire.XlsNamespace ConvertCsvToExcelFriend Class ProgramPrivate Shared Sub Main(ByVal args As String())'Create an instance of Workbook classDim workbook As Workbook = New Workbook()'Load a CSV fileworkbook.LoadFromFile("ExcelToCSV.csv", ",", 1, 1)'Get the first worksheetDim sheet As Worksheet = workbook.Worksheets(0)'Access the used range in the worksheetDim usedRange As CellRange = sheet.AllocatedRange'Ignore errors when saving numbers in the range as textusedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText'Autofit columns and rowsusedRange.AutoFitColumns()usedRange.AutoFitRows()'Save the result fileworkbook.SaveToFile("CSVToExcel.xlsx", ExcelVersion.Version2013)End SubEnd ClassEnd Namespace

C#/VB.NET:将 Excel 转换为 CSV 并将 CSV 转换为 Excel

欢迎下载|体验更多E-iceblue产品

如需获取更多产品相关信息请咨询在线客服  


标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年4月20日
下一篇 2022年4月20日

相关推荐

发表回复

登录后才能评论