ワークシート データのエクスポート中に重複する列の名前を自動的に変更する
Contents
[
Hide
]
考えられる使用シナリオ
ワークシートからデータ テーブルにデータをエクスポートしているときに、列が重複するという問題に直面することがあります。 DataTable は重複する列を持つことができないため、ワークシート データをデータ テーブルにエクスポートする前に、重複する列の名前を変更する必要があります。 Aspose.Cells は、指定した戦略に従って、重複する列の名前を自動的に変更できますExportTableOptions.RenameStrategy財産。指定すればRename戦略.Digit、列は、column1、column2、column3 などのように名前が変更されます。Rename戦略.Letter の場合、列は columnA、columnB、columnC などのように名前が変更されます。
ワークシート データのエクスポート中に重複する列の名前を自動的に変更する
次のサンプル コードは、ワークシートの最初の 3 つの列にいくつかのデータを追加しますが、すべての列の名前は同じです。人々 .次に、指定してワークシートからデータテーブルにデータをエクスポートしますRename戦略.レター戦略。次に、Aspose.Cells によって生成されたデータ テーブルの列名を出力します。次のスクリーンショットは、ビジュアライザーでデータがエクスポートされたデータ テーブルを示しています。ご覧のとおり、重複した列の名前が PeopleA、PeopleB などに変更されました。
サンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Create a workbook. | |
Workbook wb = new Workbook(); | |
//Access first worksheet. | |
Worksheet ws = wb.Worksheets[0]; | |
//Write the same column name in columns A, B and C. | |
string columnName = "People"; | |
ws.Cells["A1"].PutValue(columnName); | |
ws.Cells["B1"].PutValue(columnName); | |
ws.Cells["C1"].PutValue(columnName); | |
//Insert data in column A, B and C. | |
ws.Cells["A2"].PutValue("Data"); | |
ws.Cells["B2"].PutValue("Data"); | |
ws.Cells["C2"].PutValue("Data"); | |
//Create ExportTableOptions and specify that you want to rename | |
//duplicate column names automatically via RenameStrategy property. | |
ExportTableOptions opts = new ExportTableOptions(); | |
opts.ExportColumnName = true; | |
opts.RenameStrategy = RenameStrategy.Letter; | |
//Export data to data table, duplicate column names will be renamed automatically. | |
System.Data.DataTable dataTable = ws.Cells.ExportDataTable(0, 0, 4, 3, opts); | |
//Now print the column names of the data table generated by Aspose.Cells while exporting worksheet data. | |
for (int i = 0; i < dataTable.Columns.Count; i++) | |
{ | |
Console.WriteLine(dataTable.Columns[i].ColumnName); | |
} | |
コンソール出力
参考までに、上記のサンプル コードのコンソール出力を次に示します。
People
PeopleA
PeopleB