用C++将CSV、TSV和TXT转换为Excel
Contents
[
Hide
]
使用Aspose.Cells for C++,你可以将CSV文件转换为Excel、OpenOffice、PDF、JSON及其他许多格式。
打开 CSV 文件
逗号分隔值文件(CSV)包含以逗号分隔的记录。数据存储为表格形式,每列由逗号字符分隔并用双引号括起来。如果字段值包含双引号字符,则用一对双引号转义。你也可以用Microsoft Excel导出电子表格数据为CSV文件。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Instantiate LoadOptions specified by the LoadFormat
LoadOptions loadOptions4(LoadFormat::Csv);
// Create a Workbook object and open the file from its path
Workbook wbCSV(srcDir + u"Book_CSV.csv", loadOptions4);
std::cout << "CSV file opened successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
打开CSV文件及替换无效字符
在Excel中打开带特殊字符的CSV文件时,字符会自动被替换。Aspose.Cells API也会执行相同操作,示例代码如下。
// Fix BOM issue
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String filename = srcDir + u"[20180220142533][ASPOSE_CELLS_TEST].csv";
TxtLoadOptions options;
options.SetSeparator(u';');
options.SetCheckExcelRestriction(false);
options.SetConvertNumericData(false);
options.SetConvertDateTimeData(false);
LoadFilter* filter = new LoadFilter(LoadDataFilterOptions::CellData);
options.SetLoadFilter(filter);
Workbook workbook(filename, options);
Worksheet worksheet = workbook.GetWorksheets().Get(0);
std::cout << worksheet.GetName().ToUtf8() << std::endl;
std::cout << worksheet.GetName().GetLength() << std::endl;
std::cout << "CSV file opened successfully!" << std::endl;
delete filter;
Aspose::Cells::Cleanup();
return 0;
}
使用首选解析器
并非总是需要使用默认解析器设置打开CSV文件。有时导入CSV文件后不能得到预期输出,例如日期格式不符合预期或空字段处理不同。为此,提供了 TxtLoadOptions.PreferredParsers ,允许你自定义解析器以根据需求解析不同数据类型。以下示例演示了使用优先解析器的方法。
可以从以下链接下载示例源文件和输出文件,以测试此功能。
outputsamplePreferredParser.xlsx
#include <iostream>
#include <memory>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
Aspose::Cells::Startup();
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
TxtLoadOptions loadOptions(LoadFormat::Csv);
loadOptions.SetSeparator(u',');
loadOptions.SetConvertDateTimeData(true);
Workbook workbook(srcDir + u"sample.csv", loadOptions);
Worksheet worksheet = workbook.GetWorksheets().Get(0);
Cells cells = worksheet.GetCells();
Cell cell = cells.Get(u"A1");
std::cout << "A1: " << static_cast<int>(cell.GetType())
<< " - " << cell.GetDisplayStringValue().ToUtf8() << std::endl;
cell = cells.Get(u"B1");
std::cout << "B1: " << static_cast<int>(cell.GetType())
<< " - " << cell.GetDisplayStringValue().ToUtf8() << std::endl;
workbook.Save(outDir + u"outputsample.xlsx");
std::cout << "OpeningCSVFilesWith executed successfully." << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
使用自定义分隔符打开文本文件
文本文件用于在不格式化的情况下保存电子表格数据。 文件是一种可以具有一些自定义分隔符的纯文本文件。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input CSV file
U16String filePath = srcDir + u"Book11.csv";
// Instantiate Text File's LoadOptions
TxtLoadOptions txtLoadOptions;
// Specify the separator
txtLoadOptions.SetSeparator(u',');
// Create a Workbook object and open the file from its path
Workbook wb(filePath, txtLoadOptions);
// Save file
wb.Save(outDir + u"output.txt");
std::cout << "File saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
打开制表符分隔文件
制表符分隔(文本)文件包含电子表格数据,但没有任何格式。数据以行和列排布,就像在表格和电子表格中一样。本质上,制表符分隔文件是一种特殊的纯文本文件,每列之间用制表符分隔。
#include <iostream>
#include "Aspose.Cells.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\\");
// Instantiate LoadOptions specified by the LoadFormat
LoadOptions loadOptions(LoadFormat::TabDelimited);
// Create a Workbook object and open the file from its path
Workbook wbTabDelimited(srcDir + u"Book1TabDelimited.txt", loadOptions);
std::cout << "Tab delimited file opened successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
打开制表符分隔数值(TSV)文件
制表符分隔值(TSV)文件包含电子表格数据,但没有任何格式。与制表符分隔文件相同,数据以行和列排布。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Instantiate LoadOptions specified by the LoadFormat
LoadOptions loadOptions(LoadFormat::Tsv);
// Create a Workbook object and open the file from its path
Workbook workbook(srcDir + u"SampleTSVFile.tsv", loadOptions);
// Using the Sheet 1 in Workbook
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Accessing a cell using its name
Cell cell = worksheet.GetCells().Get(u"C3");
// Output the cell name and value
std::cout << "Cell Name: " << cell.GetName().ToUtf8() << " Value: " << cell.GetStringValue().ToUtf8() << std::endl;
Aspose::Cells::Cleanup();
return 0;
}