Example #1
0
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 /// <param name="disposing">True if the instance needs to be disposed of.</param>
 protected virtual void Dispose(
     bool disposing)
 {
     if (_disposed)
     {
         return;
     }
     if (disposing)
     {
         _book?.Dispose();
         _book  = null;
         _sheet = null;
     }
     _disposed = true;
 }
Example #2
0
        /// <summary>
        /// Closes the writer and saves the written data to the stream. Automatically called
        /// when disposed.
        /// </summary>
        public void Close()
        {
            if (_book != null)
            {
                // Set the column widths if we are doing auto sizing
                PerformColumnResize();

                // Now save the Excel file to the output stream
                _book.Save(_stream, FileFormat.OpenXml);

                // Clean up and dispose of everything
                _book?.Dispose();
                _graphics?.Dispose();
                _sheet    = null;
                _book     = null;
                _graphics = null;
            }
        }
Example #3
0
        static public void ToExcel(C1TrueDBGrid _Dgd)
        {
            if (_Dgd.RowCount == 0)
            {
                return;
            }
            string filename = "";
            var    book     = new C1XLBook();

            try
            {
                SaveFileDialog dialog = new SaveFileDialog
                {
                    Filter   = "*.xls|*.xls",
                    FileName = "DgdExcel.xls"
                };
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    filename = dialog.FileName;
                    var sheet = book.Sheets[0];
                    sheet.Name = "Page1";
                    XLStyle            style = new XLStyle(book);
                    DateTimeFormatInfo dtfi  = CultureInfo.CurrentCulture.DateTimeFormat;
                    style.Format = XLStyle.FormatDotNetToXL(dtfi.ShortDatePattern);
                    var k = 0;
                    for (int i = 0; i < _Dgd.Columns.Count; i++)
                    {
                        if (_Dgd.Splits[0].DisplayColumns[i] == null)
                        {
                            break;
                        }
                        if (_Dgd.Splits[0].DisplayColumns[i].Visible)
                        {
                            sheet[0, k].Value = _Dgd.Columns[i].Caption;
                            k++;
                        }
                    }
                    for (int i = 0; i < _Dgd.RowCount; i++)
                    {
                        k = 0;
                        for (int j = 0; j < _Dgd.Columns.Count; j++)
                        {
                            if (_Dgd.Splits[0].DisplayColumns[j] == null)
                            {
                                break;
                            }
                            if (_Dgd.Splits[0].DisplayColumns[j].Visible)
                            {
                                var obj = _Dgd[i, j];
                                if (obj != null)
                                {
                                    if (obj.GetType() == typeof(DateTime))
                                    {
                                        sheet[i + 1, k].Style = style;
                                    }
                                    sheet[i + 1, k].Value = _Dgd[i, j];
                                }
                                else
                                {
                                    sheet[i + 1, k].Value = "";
                                }
                                k++;
                            }
                        }
                    }
                    book.Save(filename);
                }
            }
            catch (Exception ex)
            {
                MBox.ShowErr(ex.Message);
                return;
            }
            finally
            {
                book.Dispose();
            }
            if (MBox.ShowAsk(string.Format("数据导出成功。保存路径:{0}\r\n是否打开?", filename)))
            {
                System.Diagnostics.Process.Start(filename);
            }
        }