static public WorkbookAdaptor Open(string workbookPath, bool readOnly = true)
 {
     if (workbookPath == null)
     {
         throw new ArgumentNullException("Path to workbook cannot be null.");
     }
     if (!File.Exists(workbookPath))
     {
         throw new ArgumentException("Workbook does not exist");
     }
     try
     {
         SpreadsheetDocument doc = SpreadsheetDocument.Open(workbookPath, !readOnly);
         var wb = new WorkbookAdaptor();
         wb.Document = doc;
         SharedStringTable sst = doc.WorkbookPart.SharedStringTablePart.SharedStringTable;
         wb.SharedStrings = new SharedStrings(sst);
         wb.Sheets        = doc.WorkbookPart.Workbook.GetFirstChild <Sheets>();
         return(wb);
     }
     catch (Exception ex)
     {
         throw new ApplicationException("Error opening workbook", ex);
     }
 }
Beispiel #2
0
        public WorksheetAdaptor(WorkbookAdaptor workbookAdaptor, Worksheet worksheet)
        {
            if (workbookAdaptor == null)
            {
                throw new ArgumentNullException("WorkbookAdaptor cannot be null in WorksheetAdaptor");
            }
            if (worksheet == null)
            {
                throw new ArgumentNullException("Workseet cannot be null in WorksheetAdaptor");
            }
            WorkbookAdaptor = workbookAdaptor;
            Worksheet       = worksheet;
            SheetData       = worksheet.GetFirstChild <SheetData>() as SheetData;
            SheetDimension dimension = worksheet.GetFirstChild <SheetDimension>() as SheetDimension;

            UsedRange = new CellRange(dimension.Reference.Value);
            SheetData sheetData = worksheet.GetFirstChild <SheetData>() as SheetData;

            foreach (Row row in sheetData)
            {
                foreach (Cell cell in row.ChildElements.OfType <Cell>())
                {
                    var c = new TableCell(cell, WorkbookAdaptor.SharedStrings);
                    _cellMap[c.Reference] = c;
                }
            }
        }
Beispiel #3
0
 protected virtual void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing)
         {
             Worksheet       = null;
             SheetData       = null;
             UsedRange       = null;
             WorkbookAdaptor = null;
         }
         disposedValue = true;
     }
 }