Obtener el rango máximo en una hoja de cálculo
Al leer datos de la hoja de cálculo, necesitamos conocer el área máxima.
Al copiar todos los datos de una hoja de cálculo, necesitamos conocer el área máxima.
Al exportar un área especificada a html y pdf, necesitamos conocer el área máxima.
Aspose.Cells para .Net contiene diferentes formas de encontrar el rango máximo en una hoja de cálculo.
Obteniendo el rango máximo
En Aspose.Cells, si los objetos row y column están inicializados, estas filas y columnas se contarán como el área máxima, incluso si no hay datos en filas o columnas vacías.
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); |
Obteniendo el rango máximo de datos
En la mayoría de los casos, solo necesitamos obtener todos los rangos que contienen todos los datos, incluso si las celdas vacías fuera del rango están formateadas. Y los ajustes sobre formas, tablas y tablas dinámicas se ignorarán.
// 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); |
Obteniendo el rango máximo de visualización
Cuando exportamos todos los datos de la hoja de cálculo a HTML, PDF o imágenes, necesitamos obtener un área que contenga todos los objetos visibles, incluidos los datos, estilos, gráficos, tablas y tablas dinámicas. Los siguientes códigos muestran cómo renderizar el rango de visualización máxima a 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); |
Aquí está el archivo de excel fuente.