This is a migrated thread and some comments may be shown as answers.

Export to excel

0 Answers 133 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vivek
Top achievements
Rank 1
Vivek asked on 30 Sep 2015, 09:13 AM

This is my code to export to excel .But in this code while i press export button then it will export the excel,and shows a message as it is saved ,but it is not opening automatically by the code given ,instead os that it shows file cannot be find.How can i fix it? 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using Telerik.WinControls;
using System.IO;
using System.Collections.Specialized;
using System.Web;
using Telerik.WinControls.UI;
using System.Reflection;
using Telerik.WinControls.Export;
using Telerik.WinControls.UI.Export;
using Telerik.WinControls.Data;


namespace Export2html_pdf_excel
{
    public partial class ExportForm : Telerik.WinControls.UI.RadForm
    {
        SaveFileDialog save = new SaveFileDialog();
        public ExportForm()
        {
            InitializeComponent();

        }

        private void RadForm1_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                SqlConnection con = new SqlConnection("server=QB_DT_2\\sqlexpress;database=nikeho;user id=sa;password=admin123");
                SqlDataAdapter sda = new SqlDataAdapter("select top 1000 * from idtable", con);
                sda.Fill(dt);
                GridView.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }

        private void radRadioButtonExport_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {

            if (this.RBtnExcel.IsChecked)
            {
                save.Filter = "Excel (*.xls)|*.xls";
            }


        }



        private void Export_Click(object sender, EventArgs e)
        {
            if (save.ShowDialog() != DialogResult.OK)             
             {                
                 return;             
             }              
            if (save.FileName.Equals(String.Empty))             
            {                 
                RadMessageBox.SetThemeName(this.GridView.ThemeName);                 
                RadMessageBox.Show("Please enter a file name.");                 
                return;             
            }              
            string fileName = this.save.FileName;             
            bool openExportFile = false;              
            if (this.RBtnExcel.IsChecked)             
            {                 
                RunExportToExcel(fileName, ref openExportFile);            
            }                        

            if (openExportFile==true)
            {
                try
                {
                    System.Diagnostics.Process.Start(fileName);
                }

                catch (Exception ex)
                {
                    string message = String.Format("The file cannot be opened on your system.\nError message: {0}", ex.Message);
                    RadMessageBox.Show(message, "Open File", MessageBoxButtons.OK, RadMessageIcon.Error);

                }
            }
          }


        private void RunExportToExcel(string fileName, ref bool openExportFile)         
        {             
            ExportToExcelML excelExporter = new ExportToExcelML(this.GridView);

            try        
            {               
                this.Cursor = Cursors.WaitCursor;    
                excelExporter.RunExport(fileName);           
                RadMessageBox.SetThemeName(this.GridView.ThemeName);   
                DialogResult dr = RadMessageBox.Show("The data in the grid was exported successfully. Do you want to open the file?",                     "Export to Excel", MessageBoxButtons.YesNo, RadMessageIcon.Question);           
                if (dr == DialogResult.Yes)        
                {                 
                    openExportFile = true;    
                    //System.Diagnostics.Process.Start(fileName);
                }        
            }           
            catch (IOException ex)       
            {               
                RadMessageBox.SetThemeName(this.GridView.ThemeName);   
                RadMessageBox.Show(this, ex.Message, "I/O Error", MessageBoxButtons.OK, RadMessageIcon.Error);    
            }           
            finally        
            {             
                this.Cursor = Cursors.Default;   
            }         
        }

No answers yet. Maybe you can help?

Tags
General Discussions
Asked by
Vivek
Top achievements
Rank 1
Share this question
or