简体   繁体   中英

How can I get an Excel worksheet's used range with OpenXML?

I need to get the used range of cells in a new worksheet. For example:

A1:AY55

In Excel VBA, this can be obtained through the aptly named UsedRange property. Is there an equivalent in OpenXML?

It can be found in the SheetDimension class which can be found as a property of a Worksheet . The following code will write the used range to the console:

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, false))
{
    WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
    //get the correct sheet
    Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet1").First();
    WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id) as WorksheetPart;
    Console.WriteLine(worksheetPart.Worksheet.SheetDimension.Reference);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM