スプレッドシートを CSV 形式にエクスポートする際に、先頭の空白の行と列をトリミングする
Contents
[
Hide
]
考えられる使用シナリオ
Excel または CSV ファイルの先頭に空白の列または行がある場合があります。たとえば、この行を考えてみましょう
,,,data1,data2
ここでは、最初の 3 つのセルまたは列が空白です。このような CSV ファイルを Microsoft Excel で開くと、Microsoft Excel はこれらの先頭の空白行と列を破棄します。
デフォルトでは、Aspose.Cells は保存時に先頭の空白の列と行を破棄しませんが、Microsoft Excel のようにそれらを削除したい場合は、Aspose.Cells が提供します**TxtSaveOptions.TrimLeadingBlankRowAndColumn**財産。に設定してください**真実**保存時に先頭の空白の行と列はすべて破棄されます。
Aspose.Cells for .NET 20.4 のリリース前は、デフォルト値の**TxtSaveOptions.TrimLeadingBlankRowAndColumn**だった**間違い** 20.4 リリース以降、デフォルト値の**TxtSaveOptions.TrimLeadingBlankRowAndColumn**は**真実。**
スプレッドシートを CSV 形式にエクスポートする際に、先頭の空白の行と列をトリミングする
次のサンプル コードは、ソースエクセルファイル 2 つの先頭の空白列があります。最初にExcelファイルを変更せずにCSV形式で保存してから設定します**TxtSaveOptions.TrimLeadingBlankRowAndColumn**プロパティへ**真実**再度保存します。スクリーンショットは、ソースエクセルファイル, トリミングなしで CSV ファイルを出力、 そしてそのCSV ファイルをトリミングして出力.
サンプルコード
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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
//Load source worbook | |
Workbook wb = new Workbook(dataDir + "sampleTrimBlankColumns.xlsx"); | |
//Save in csv format | |
wb.Save(dataDir + "outputWithoutTrimBlankColumns.csv", SaveFormat.Csv); | |
//Now save again with TrimLeadingBlankRowAndColumn as true | |
TxtSaveOptions opts = new TxtSaveOptions(); | |
opts.TrimLeadingBlankRowAndColumn = true; | |
//Save in csv format | |
wb.Save(dataDir + "outputTrimBlankColumns.csv", opts); | |