Удаление пустых строк и столбцов в листе
Contents
[
Hide
]
Можно удалить все пустые строки и столбцы на листе. Это полезно, например, при генерации файла PDF из файла Microsoft Excel, если нужно конвертировать только строки и столбцы, содержащие данные.
Используйте следующие методы Aspose.Cells для удаления пустых строк и столбцов:
- Чтобы удалить пустые строки, используйте метод Cells.deleteBlankRows().
- Чтобы удалить пустые столбцы, используйте метод Cells.deleteBlankColumns().
Удаление пустых строк
This file contains 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DeletingBlankRows.class); | |
// Create a new Workbook. Open an existing excel file. | |
Workbook wb = new Workbook(dataDir + "Book1.xlsx"); | |
// Create a Worksheets object with reference to the sheets of the Workbook. | |
WorksheetCollection sheets = wb.getWorksheets(); | |
// Get first Worksheet from WorksheetCollection | |
Worksheet sheet = sheets.get(0); | |
// Delete the Blank Rows from the worksheet | |
sheet.getCells().deleteBlankRows(); | |
// Save the excel file. | |
wb.save(dataDir + "Output.xlsx"); | |
Удаление пустых столбцов
This file contains 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DeletingBlankColumns.class); | |
// Create a new Workbook. Open an existing excel file. | |
Workbook wb = new Workbook(dataDir + "Book1.xlsx"); | |
// Create a Worksheets object with reference to the sheets of the Workbook. | |
WorksheetCollection sheets = wb.getWorksheets(); | |
// Get first Worksheet from WorksheetCollection | |
Worksheet sheet = sheets.get(0); | |
// Delete the Blank Columns from the worksheet | |
sheet.getCells().deleteBlankColumns(); | |
// Save the excel file. | |
wb.save(dataDir + "Output.xlsx"); |