Beispiel #1
0
        public static void Append(string fileName, string sheetName, DataTable table)
        {
            IWorkbook workbook = CreateInputWorkbook(fileName);

            if (workbook == null)
            {
                throw new Exception("Workbook cannot be null!");
            }

            workbook.Append(sheetName, table);
            Append(workbook, fileName);
        }
Beispiel #2
0
 public static IWorkbook Append(IWorkbook workbook, string sheetName, DataTable table)
 {
     workbook.Append(sheetName, table);
     return(workbook);
 }
        protected IRichEditDocumentServer AddSpreadTable(ArgumentCollection arguments)
        {
            if (arguments.Count <= 0)
            {
                throw new Exception("'DOCVARIABLE SPREADTABLE' requires filename as first argument.");
            }
            if (arguments.Count <= 1)
            {
                throw new Exception("'DOCVARIABLE SPREADTABLE' requires table or range as second argument.");
            }

            var fileName = arguments[0].Value;

            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new Exception("DOCVARIABLE SPREADTABLE does not contain valid filename.");
            }

            object snippet = null;

            if (Snippets?.ContainsKey(fileName) ?? false)
            {
                snippet = Snippets[fileName];
                if (snippet is SCSpreadsheetContext)
                {
                }
                else
                {
                    throw new Exception($"Specified snippet '{fileName}' is not supported. Snippet shall be Spreadsheet.");
                }
            }
            else if (string.Compare(fileName, "$SPREAD", true) == 0)
            {
                //Do nothing
            }
            else
            {
                fileName = Project.Current.MapPath(fileName);
            }

            if (snippet == null && string.Compare(fileName, "$SPREAD", true) != 0 && !File.Exists(fileName))
            {
                throw new Exception($"File '{fileName}' does not exist.");
            }

            var tableName = arguments[1].Value;

            bool rebuild = false;

            if (arguments.Count > 2)
            {
                var properties = Utils.SplitNameValueString(arguments[2].Value, ';');

                foreach (var prop in properties)
                {
                    if (string.IsNullOrWhiteSpace(prop.Key))
                    {
                        continue;
                    }

                    switch (prop.Key.ToLower())
                    {
                    case "rebuild":
                    case "recalculate":
                    case "recalc":
                        var valueRebuild = prop.Value;
                        if (string.IsNullOrWhiteSpace(valueRebuild))
                        {
                            valueRebuild = bool.TrueString;
                        }
                        rebuild = bool.Parse(valueRebuild);
                        break;
                    }
                }
            }

            bool      needDispose = true;
            IWorkbook workbook    = null;

            try
            {
                //using var workbook = SpreadsheetUtils.CreateWorkbook();
                if (snippet is SCSpreadsheetContext spreadsheetContext)
                {
                    workbook = SpreadsheetUtils.CreateWorkbook();
                    workbook.Append(spreadsheetContext.Workbook);
                }
                else if (string.Compare(fileName, "$SPREAD", true) == 0)
                {
                    needDispose = false;
                    workbook    = this.DefaultSpreadsheet;

                    if (workbook == null)
                    {
                        throw new Exception("Current script does not support default (Host) spreadsheet.");
                    }
                }
                else
                {
                    workbook = SpreadsheetUtils.CreateWorkbook();
                    workbook.LoadDocument(fileName);
                }

                IRichEditDocumentServer result = null;
                if (workbook == this.DefaultSpreadsheet && NeedSynchronizeDefaultSpreadsheet)
                {
                    SCDispatcherService.UIDispatcherServer.Invoke(() => ProcessWorkbook(workbook, tableName, rebuild, out result));
                }
                else
                {
                    ProcessWorkbook(workbook, tableName, rebuild, out result);
                }
                return(result);
            }
            finally
            {
                if (needDispose && workbook != null)
                {
                    workbook.Dispose();
                }
            }