Convert Text to Columns using Aspose.Cells with C++
Possible Usage Scenarios
You can convert your Text to Columns using Microsoft Excel. This feature is available from Data Tools under the Data tab. In order to split the contents of a column to multiple columns, the data should contain a specific delimiter such as a comma (or any other character) based on which Microsoft Excel splits the contents of a cell to multiple cells. Aspose.Cells also provides this feature via Worksheet.Cells.TextToColumns() method.
Convert Text to Columns using Aspose.Cells
The following sample code explains the usage of Worksheet.Cells.TextToColumns() method. The code first adds some people names in column A of the first worksheet. The first and last name is separated by a space character. Then it applies Worksheet.Cells.TextToColumns() method on column A and saves it as an output Excel file. If you open the output Excel file, you will see, first names are in column A while last names are in column B as shown in this screenshot.
Sample Code
#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\\");
// Create a workbook
Workbook wb;
// Access first worksheet
Worksheet ws = wb.GetWorksheets().Get(0);
// Add people name in column A. Fast name and Last name are separated by space.
ws.GetCells().Get(u"A1").PutValue(u"John Teal");
ws.GetCells().Get(u"A2").PutValue(u"Peter Graham");
ws.GetCells().Get(u"A3").PutValue(u"Brady Cortez");
ws.GetCells().Get(u"A4").PutValue(u"Mack Nick");
ws.GetCells().Get(u"A5").PutValue(u"Hsu Lee");
// Create text load options with space as separator
TxtLoadOptions opts;
opts.SetSeparator(u' ');
// Split the column A into two columns using TextToColumns() method
// Now column A will have first name and column B will have second name
ws.GetCells().TextToColumns(0, 0, 5, opts);
// Save the workbook in xlsx format
wb.Save(outDir + u"outputTextToColumns.xlsx");
std::cout << "Text to columns conversion completed successfully!" << std::endl;
Aspose::Cells::Cleanup();
}