Aspose.Cellsを使用したテキストを列に変換する
可能な使用シナリオ
Microsoft Excelを使用してテキストを列に変換できます。この機能は、Data ToolsのDataタブにあります。列の内容を複数の列に分割するには、そのデータに特定の区切り文字(カンマやその他の文字)を含める必要があり、その区切り文字に基づいてMicrosoft Excelはセルの内容を複数のセルに分割します。Aspose.Cellsもこの機能をTextToColumnsメソッドを通じて提供しています。
Aspose.Cellsを使用したテキストを列に変換する
次のサンプルコードは、TextToColumns メソッドの使用方法を説明しています。最初に、最初のワークシートの列Aにいくつかの人名を追加します。名前と名字はスペース文字で区切られています。その後、列Aに対して TextToColumns を適用し、出力Excelファイルとして保存します。出力Excelファイル を開くと、スクリーンショットのように、名前が列Aに、名字が列Bにあるのが見えます。
サンプルコード
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ConvertTexttoCols.class) + "rows_cloumns/"; | |
//Create a workbook. | |
Workbook wb = new Workbook(); | |
//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("A1").putValue("John Teal"); | |
ws.getCells().get("A2").putValue("Peter Graham"); | |
ws.getCells().get("A3").putValue("Brady Cortez"); | |
ws.getCells().get("A4").putValue("Mack Nick"); | |
ws.getCells().get("A5").putValue("Hsu Lee"); | |
//Create text load options with space as separator. | |
TxtLoadOptions opts = new TxtLoadOptions(); | |
opts.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. | |
ws.getCells().textToColumns(0, 0, 5, opts); | |
//Save the workbook in xlsx format. | |
wb.save(dataDir + "outputTextToColumns.xlsx"); |