Aspose.Cellsを使用したテキストを列に変換する
可能な使用シナリオ
Microsoft Excelを使用して、テキストを列に変換することができます。この機能は、DataタブのData Toolsから利用できます。列の内容を複数の列に分割するには、Microsoft Excelがセルの内容を複数のセルに分割する基準となる、コンマ(または他の文字)などの特定の区切り文字を含むデータが必要です。Aspose.Cellsもこの機能を提供します。Worksheet.Cells.TextToColumns()メソッドを介して。
Aspose.Cellsを使用したテキストを列に変換する
次のサンプルコードは、Worksheet.Cells.TextToColumns()メソッドの使用法を説明しています。このコードでは、まず第1ワークシートの列Aに人名を追加します。名前はスペース文字で区切られています。その後、列AにWorksheet.Cells.TextToColumns()メソッドを適用し、出力エクセルファイルとして保存します。出力エクセルファイルを開くと、名前が列Aに、姓が列Bに表示されます。
サンプルコード
// 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); | |
//Create a workbook. | |
Workbook wb = new Workbook(); | |
//Access first worksheet. | |
Worksheet ws = wb.Worksheets[0]; | |
//Add people name in column A. Fast name and Last name are separated by space. | |
ws.Cells["A1"].PutValue("John Teal"); | |
ws.Cells["A2"].PutValue("Peter Graham"); | |
ws.Cells["A3"].PutValue("Brady Cortez"); | |
ws.Cells["A4"].PutValue("Mack Nick"); | |
ws.Cells["A5"].PutValue("Hsu Lee"); | |
//Create text load options with space as separator. | |
TxtLoadOptions opts = new TxtLoadOptions(); | |
opts.Separator = ' '; | |
//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.Cells.TextToColumns(0, 0, 5, opts); | |
//Save the workbook in xlsx format. | |
wb.Save(dataDir + "outputTextToColumns.xlsx"); |