将文本转换为列

将文本转换为列

您可以使用Microsoft Excel将文本转换为列。此功能在“数据”选项卡的“数据工具”下可用。为了将一列的内容拆分为多列,数据应包含特定的分隔符,如逗号(或任何其他字符),基于该分隔符,Microsoft Excel将单元格的内容拆分为多个单元格。Aspose.Cells也通过TextToColumns方法提供此功能。以下代码段演示了如何使用TextToColumns方法通过使用空格作为分隔符将文本转换为列。

output_directory = "Examples/SampleFiles/OutputDirectory/"
# Create Workbook
workbook = Workbook()
# Access the first worksheet
worksheet = workbook.getWorksheets().get(0)
# Add people name in column A. Fast name and Last name are separated by space.
worksheet.getCells().get("A1").putValue("John Teal")
worksheet.getCells().get("A2").putValue("Peter Graham")
worksheet.getCells().get("A3").putValue("Brady Cortez")
worksheet.getCells().get("A4").putValue("Mack Nick")
worksheet.getCells().get("A5").putValue("Hsu Lee")
# Create text load options with space as separator.
txtLoadOptions = TxtLoadOptions()
txtLoadOptions.setSeparator(' ')
# Split the column A into two columns using TextToColumns() method.
# Now column A will have first name and column B will have second name.
worksheet.getCells().textToColumns(0, 0, 5, txtLoadOptions)
# Save the excel file.
workbook.save(output_directory + "outputTextToColumns.xlsx")