Excel Interop: Query into DataGridView, Save to spreadsheet, Open via Excel.

This requires ExcelInterop.cs

 

        private void btnListAllUsers_Click(object sender, EventArgs e)

        {

            SaveListOfAllUsers();

        }

 

        private void SaveListOfAllUsers()

        {

            DataTable dtUserList = new DataTable();

            // use an invisible DataGridView on the UI form.

            // The following declaration does not work:

            // DataGridView dgvUserList = new DataGridView();   

            dgvUserList.DataSource = null;

            dgvUserList.Rows.Clear();

            dgvUserList.Columns.Clear();

            dtUserList.Clear();

 

            // Get report data from database into DataTable.

            // Report data from database has column headers,

// and is in desired sequence.

            dtUserList = mq.QueryMUPReport();

 

            // Copy report to DataGridView.

            dgvUserList.DataSource = dtUserList;

 

            //Create array of headers for Excel output

            string[] aryHeaders = new string[dgvUserList.ColumnCount];

            for (int colnum = 0; colnum < dgvUserList.ColumnCount; colnum++)

            {

                aryHeaders[colnum] = dgvUserList.Columns[colnum].Name;

            }

 

            // Create a new Excel file with headers (delete existing file)

            ExcelInterop exc = new ExcelInterop();

            string excelFilename = “C:\\MesUserPermissions.xlsx”;

            try

This happens because the money is not in the product is Sildenafil Citrate, which is an excellent component that helps in responding to one’s stimulation. purchase levitra online In addition to being considered among the Healthiest Organic Supplements today, The World’s cialis generika 5mg Strongest Acai is also regarded a Healthy Aphrodisiac. Today, yohimbe extract is a common ingredient added for the preparation of ayurvedic medicines is a best recommended herbal cure for semen leakage problem. sildenafil in usa A few herbs and their health benefits: Fenugreek: * Fenugreek is an excellent home remedy for rheumatism. 1-2 teaspoonful of juice should be taken before meals. get cialis cheap * Celery is another effective home remedy for diabetes. 3.             {

                exc.ExcelCreateFile(excelFilename, aryHeaders, System.Type.Missing);

 

                // Save data from DataGrid to Excel

                writeDGToExcel(dgvUserList, exc);

 

                // Release the reference to Excel; release the open file.

                exc.ExcelTerminate();

 

                // Open the Excel spreadsheet.

                System.Diagnostics.Process.Start(excelFilename);

            }

            catch

            {

            }

        }

 

        private void writeDGToExcel(DataGridView dgv, ExcelInterop ei)

        {

            for (int rownum = 0; rownum < dgv.RowCount – 1; rownum++)

            {

                DataGridViewCellCollection dgvcc = dgv.Rows[rownum].Cells;

                string[] values = new string[dgvcc.Count];

                for (int colnum = 0; colnum < dgvcc.Count; colnum++)

                {

                    values[colnum] = dgvcc[colnum].Value.ToString();

                }

                try

                {

                    ei.ExcelWriteToWorksheet(values, rownum);

                }

                catch

                {

                }

            }

        }

 

This entry was posted in Parenting and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.