Beispiel #1
0
        // save a bool with several sheets in it
        void _btnMultiSheet_Click(object sender, System.EventArgs e)
        {
            // choose file
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.DefaultExt = "xls";
            dlg.FileName   = "*.xls";
            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            // delete file if it's already there
            if (File.Exists(dlg.FileName))
            {
                File.Delete(dlg.FileName);
            }

            // prepare to load data from database
            string mdbfile = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\ComponentOne Samples\Common\c1nwind.mdb";
            string conn    = @"provider=microsoft.jet.oledb.4.0;data source=" + mdbfile + ";";

            System.Data.OleDb.OleDbDataAdapter da;

            // save several sheets
            string[] tables = "Categories|Products|Employees|Customers|Orders".Split('|');
            foreach (string table in tables)
            {
                // load data into grid
                string sql = "select * from " + table;
                da = new System.Data.OleDb.OleDbDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                _flex.DataSource = dt;
                _flex.Update();

                // save into a sheet
                _flex.SaveExcel(dlg.FileName, table, FileFlags.VisibleOnly | FileFlags.IncludeFixedCells);
            }
        }