Exemple #1
0
 /// <summary>
 /// 释放对象
 /// </summary>
 public void Dispose()
 {
     try
     {
         _workbook.Dispose();
     }
     catch
     {
     }
     finally
     {
         _workbook = null;
     }
 }
        private bool _disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                    Workbook.Dispose();
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                _disposedValue = true;
            }
        }
Exemple #3
0
    /// <summary>
    /// GridView转换成Excel文档流,并保存到文件
    /// </summary>
    /// <param name="IWorkbook">输出的EXCEL文件</param>
    /// <param name="fileName">保存的路径</param>
    public static void SaveFileToExcel(IWorkbook wb, Page page, string fileName)
    {
        // 產生 Excel 資料流。
        MemoryStream ms = new MemoryStream();

        wb.Write(ms);

        // 設定強制下載標頭。
        page.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=" + fileName));
        // 輸出檔案。
        page.Response.BinaryWrite(ms.ToArray());

        ms.Close();
        ms.Dispose();

        wb.Dispose();
    }
Exemple #4
0
 /// <summary>
 /// 释放资源
 /// </summary>
 public void Dispose()
 {
     try
     {
         if (workbook != null)
         {
             workbook.Close();
             workbook.Dispose();
         }
         //删除临时文件
         if (File.Exists(filePath))
         {
             File.Delete(filePath);
         }
     }
     catch
     {
     }
 }
Exemple #5
0
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (fs != null)
                    {
                        fs.Dispose();
                        workbook.Dispose();
                        fs.Close();
                        workbook.Close();
                    }
                }

                fs       = null;
                disposed = true;
            }
        }
        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();
                }
            }
        protected virtual string GenerateTableHtml(SpreadTableOptions options)
        {
            string    htmlTable;
            IWorkbook workbook        = null;
            bool      disposeWorkbook = false;

            try
            {
                if (options._Spreadsheet != null)
                {
                    workbook = options._Spreadsheet.Workbook;
                }
                else if (!string.IsNullOrWhiteSpace(options._FileName))
                {
                    disposeWorkbook = true;

                    string fileName = Project.Current.MapPath(options._FileName);
                    if (!File.Exists(fileName))
                    {
                        throw new Exception($"File '{fileName}' does not exist.");
                    }

                    workbook = SpreadsheetUtils.CreateWorkbook();
                    workbook.LoadDocument(fileName);
                }
                else
                {
                    workbook = Host?.Spreadsheet?.Workbook;
                }

                if (workbook == null)
                {
                    throw new Exception("Spreadsheet is not specified");
                }

                var range = SpreadsheetUtils.GetWorkbookRange(workbook, options._TableName);

                var optionsHtml = new DevExpress.XtraSpreadsheet.Export.HtmlDocumentExporterOptions()
                {
                    SheetIndex = workbook.Sheets.IndexOf(range.Worksheet),
                    Range      = range.GetReferenceA1(),
                    Encoding   = Encoding.Unicode
                };

                using var stream = new MemoryStream();
                workbook.ExportToHtml(stream, optionsHtml);
                stream.Seek(0, SeekOrigin.Begin);

                using var reader = new StreamReader(stream, Encoding.UTF8);
                htmlTable        = reader.ReadToEnd();
            }
            finally
            {
                if (disposeWorkbook)
                {
                    workbook?.Dispose();
                }
            }

            return(htmlTable);
        }