使用C++将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.GetSeparator()为逗号,所以在保存为CSV格式时无需指定分隔符。
#include <iostream>
#include "Aspose.Cells.h"
#include "Aspose.Cells/TxtSaveOptions.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Load your source workbook
Workbook workbook(inputFilePath);
// Text save options. You can use any type of separator
TxtSaveOptions opts;
opts.SetSeparator(u'\t');
opts.SetExportAllSheets(true);
// Save entire workbook data into file
U16String outputFilePath = srcDir + u"out.txt";
workbook.Save(outputFilePath, opts);
std::cout << "Workbook saved successfully as text file!" << std::endl;
Aspose::Cells::Cleanup();
}
使用自定义分隔符保存文本文件
文本文件包含无格式的电子表格数据。该文件是一种纯文本文件,可以在其数据之间具有一些自定义分隔符。
#include <iostream>
#include "Aspose.Cells.h"
#include "Aspose.Cells/TxtSaveOptions.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of input excel file
U16String filePath = srcDir + u"Book1.xlsx";
// Create a Workbook object and opening the file from its path
Workbook workbook(filePath);
// Instantiate Text File's Save Options
TxtSaveOptions options;
// Specify the separator
options.SetSeparator(u';'); // Using U16String for the char
// Save the file with the options
workbook.Save(srcDir + u"output.csv", options);
std::cout << "File saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}