Esempio n. 1
0
 public void CloseWorkbook(Workbook wb)
 {
     wb.Dispose();
 }
Esempio n. 2
0
        public Workbook OpenWorkbook(string relpath)
        {
            // get the absolute path
            var abspath = System.IO.Path.GetFullPath(relpath);

            // make sure that this is actually an Excel file
            var ft = MagicBytes(abspath);
            if (ft != CWFileType.XLS && ft != CWFileType.XLSX)
            {
                throw new WorkbookOpenException();
            }

            // This call is stupid.  See:
            // http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.open%28v=office.11%29.aspx
            _app.Workbooks.Open(abspath,                    // FileName (String)
                               XlUpdateLinks.Yes,           // UpdateLinks (XlUpdateLinks enum)
                               true,                        // ReadOnly (Boolean)
                               Missing.Value,               // Format (int?)
                               "thisisnotapassword",        // Password (String)
                               Missing.Value,               // WriteResPassword (String)
                               true,                        // IgnoreReadOnlyRecommended (Boolean)
                               Missing.Value,               // Origin (XlPlatform enum)
                               Missing.Value,               // Delimiter; if the filetype is txt (String)
                               Missing.Value,               // Editable; not what you think (Boolean)
                               false,                       // Notify (Boolean)
                               Missing.Value,               // Converter(int)
                               false,                       // AddToMru (Boolean)
                               Missing.Value,               // Local; really "use my locale?" (Boolean)
                               XlCorruptLoad.RepairFile);   // CorruptLoad (XlCorruptLoad enum)

            // Excel uses 1-based arrays
            var wb_idx = _app.Workbooks.Count;
            var wbref = _app.Workbooks[wb_idx];

            // if the open call above failed, stop now
            if (wbref == null)
            {
                throw new WorkbookOpenException();
            }

            // do not autorecover!
            wbref.EnableAutoRecover = false;

            // if this workbook has links, break them
            var links = (Array) wbref.LinkSources(Excel.XlLink.xlExcelLinks);
            if (links != null)
            {
                for (int i = 1; i <= links.Length; i++)
                {
                    wbref.BreakLink((string)links.GetValue(i), Excel.XlLinkType.xlLinkTypeExcelLinks);
                }
            }

            // create callback to remove workbook from _wbs on Dispose()
            string wbname = wbref.Name;
            Action dcb = () => _wbs.Remove(wbname);  // MUST use copy of wbref.Name here!

            // init wrapped workbook
            var wb = new Workbook(wbref, _app, dcb);

            // add to list
            _wbs.Add(wb.WorkbookName, wb);

            return wb;
        }