Удаление пустых строк и столбцов в листе
Contents
[
Hide
]
Можно удалить все пустые строки и столбцы из листа. Это полезно, например, при создании файла PDF из файла Microsoft Excel и требуется конвертировать только строки и столбцы, содержащие данные или связанные объекты.
Используйте следующие методы Aspose.Cells для удаления пустых строк и столбцов:
- Для удаления пустых строк используйте метод Cells.DeleteBlankRows(). Обратите внимание, для удаляемых пустых строк требуется, чтобы Row.IsBlank был равен true, а также не должно быть видимых комментариев для любой ячейки в этих строках, и не должно быть сводной таблицы, диапазон которой пересекается с ними.
- Чтобы удалить пустые столбцы, используйте метод Cells.DeleteBlankColumns().
C# код для удаления пустых строк
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open an existing excel file. | |
Workbook wb = new Workbook(dataDir+ "SampleInput.xlsx"); | |
// Create a Worksheets object with reference to | |
// The sheets of the Workbook. | |
WorksheetCollection sheets = wb.Worksheets; | |
// Get first Worksheet from WorksheetCollection | |
Worksheet sheet = sheets[0]; | |
// Delete the Blank Rows from the worksheet | |
sheet.Cells.DeleteBlankRows(); | |
// Save the excel file. | |
wb.Save(dataDir+ "mybook.out.xlsx"); |
C# код для удаления пустых столбцов
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open an existing excel file. | |
Workbook wb = new Workbook(dataDir+ "SampleInput.xlsx"); | |
// Create a Worksheets object with reference to | |
// The sheets of the Workbook. | |
WorksheetCollection sheets = wb.Worksheets; | |
// Get first Worksheet from WorksheetCollection | |
Worksheet sheet = sheets[0]; | |
// Delete the Blank Columns from the worksheet | |
sheet.Cells.DeleteBlankColumns(); | |
// Save the excel file. | |
wb.Save(dataDir+ "mybook.out.xlsx"); |