ワークシート内の最大範囲を取得する

最大範囲を取得する

Aspose.Cellsで、rowcolumnオブジェクトが初期化されている場合、これらの行と列は、空の行や列にデータがなくても最大範囲としてカウントされます。

Workbook workbook = new Workbook("Book1.xlsx");
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.Worksheets;
Worksheet sheet = worksheets[0];
//Gets the max data range.
int maxRow = sheet.Cells.MaxRow;
int maxColumn = sheet.Cells.MaxColumn;
//The range is A1:B3.
Range range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1);
sheet.Cells["A10"].PutValue(null);
maxRow = sheet.Cells.MaxRow;
maxColumn = sheet.Cells.MaxColumn;
//The range is udpated to A1:B10.
range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1);

最大データ範囲を取得する

ほとんどの場合、空のセルが範囲外にある場合でも、すべてのデータを含むすべての範囲を取得する必要があります。 そして、図形、テーブル、ピボットテーブルに関する設定は無視されます。

// Instantiate a new Workbook.
Workbook workbook = new Workbook("Book1.xlsx");
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.Worksheets;
Worksheet sheet = worksheets[0];
//Gets the max data range.
int maxRow = sheet.Cells.MaxDataRow;
int maxColumn = sheet.Cells.MaxDataColumn;
//The range is A1:B3.
Range range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1);
sheet.Cells["A10"].PutValue(null);
maxRow = sheet.Cells.MaxDataRow;
maxColumn = sheet.Cells.MaxDataColumn;
//The range is still A1:B3.
range = sheet.Cells.CreateRange(0, 0, maxRow + 1, maxColumn + 1);

最大表示範囲を取得する

ワークシートからすべてのデータをHTML、PDF、または画像にエクスポートする場合、データ、スタイル、グラフィック、表、およびピボットテーブルを含むすべての可視オブジェクトを含むエリアを取得する必要があります。 次のコードは、最大表示範囲をhtmlにレンダリングする方法を示しています:

// Instantiate a new Workbook.
Workbook workbook = new Workbook("Book1.xlsx");
// Get all the worksheets in the book.
WorksheetCollection worksheets = workbook.Worksheets;
//Gets the max display range.
Range range = worksheets[0].Cells.MaxDisplayRange;
//Save the range to html
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.ExportActiveWorksheetOnly = true;
saveOptions.ExportArea = CellArea.CreateCellArea(range.FirstRow, range.FirstColumn, range.FirstRow + range.RowCount - 1, range.FirstColumn + range.ColumnCount - 1);
//Save the range.
workbook.Save("html.html", saveOptions);

以下はソースエクセルファイルです。