将 Excel 转换为 CSV,TSV 和 Txt
Contents
[
Hide
]
Aspose.Cells可以将excel、ods、json等格式文件转换为CSV、TSV、TXT。
将工作簿保存为文本或 CSV 格式
有时,您希望将包含多个工作表的工作簿转换或保存为文本格式。对于文本格式(例如 TXT、TabDelim、CSV 等),默认情况下 Microsoft Excel 和 Aspose.Cells 都只保存活动工作表的内容。
下面的代码示例说明了如何将整个工作簿保存为文本格式。加载源工作簿,它可以是任何 Microsoft Excel 或 OpenOffice 电子表格文件(例如 XLS、XLSX、XLSM、XLSB、ODS 等)和任意数量的工作表。
代码执行时,将工作簿中所有工作表的数据转换为TXT格式。
您可以修改相同的示例以将文件保存到 CSV。默认情况下,**TxtSaveOptions.Separator**是逗号,所以如果保存为 CSV 格式,请不要指定分隔符。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load your source workbook | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Text save options. You can use any type of separator | |
TxtSaveOptions opts = new TxtSaveOptions(); | |
opts.Separator = '\t'; | |
opts.ExportAllSheets = true; | |
// Save entire workbook data into file | |
workbook.Save(dataDir + "out.txt", opts); |
使用自定义分隔符保存文本文件
文本文件包含未格式化的电子表格数据。该文件是一种纯文本文件,其数据之间可以有一些自定义的分隔符。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir + "Book1.xlsx"; | |
// Create a Workbook object and opening the file from its path | |
Workbook wb = new Workbook(filePath); | |
// Instantiate Text File's Save Options | |
TxtSaveOptions options = new TxtSaveOptions(); | |
// Specify the separator | |
options.Separator = Convert.ToChar(";"); | |
// Save the file with the options | |
wb.Save(dataDir + "output.csv", options); | |