blog

Home / DeveloperSection / Blogs / Write to Excel Sheet

Write to Excel Sheet

Karuppaiah T2798 11-Aug-2013

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
namespace Excel_Writer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
       
        private void button1_Click(object sender, RoutedEventArgs e)
        {
           
            OleDbConnection excelConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='F:\\test\\test.xlsx';Extended Properties=Excel 8.0;");
            try
            {
                
                string sqlStquery = "Insert into [details$](SNo,Date,Address1,Address2,Address3,Address4,Address5,Address6,ClientName,Date1,LandSqfeet,Amt,ContractAmt,LandCost,RegistrationCost,TotalAmount,Sqfeet,Area,PlotNo,DocumentPreparationDate) Values('" + txtSNo.Text + "','" + txtDate.Text + "','" + txtAddress1.Text + "','" + txtAddress2.Text + "','" + txtAddress3.Text + "','" + txtAddress4.Text + "','" + txtAddress5.Text + "','" + txtAddress6.Text + "','" + txtClientName.Text + "','" + txtDate1.Text + "','" + txtLandSqFeet.Text + "','" + txtAmt.Text + "','" + txtContractAmt.Text + "','" + txtLandCost.Text + "','" + txtRegCost.Text + "','" + txtTotalAmt.Text + "','" + txtSqFeet.Text + "','" + txtArea.Text + "','" + txtPlotNo.Text + "','" + txtDocPreDate.Text + "')";
                // your connection string
                excelConnection.Open();
                OleDbDataAdapter dbadapter = new OleDbDataAdapter(sqlStquery, excelConnection);
                // insert data to excel
                dbadapter.SelectCommand.ExecuteNonQuery();
                dbadapter.Dispose();
                excelConnection.Close();
                excelConnection.Dispose();
                MessageBox.Show("Data Saved Successfully.");
            }
            catch (Exception ex)
            {
                excelConnection.Close();
                excelConnection.Dispose();
                MessageBox.Show("Error :" + ex.ToString());
            }
            // cmd = new OleDbCommand("Insert into [details$](SNo,Date,Address1,Address2,Address3,Address4,Address5,Address6,ClientName,Date1,LandSqfeet,Amt,ContractAmt,LandCost,RegistrationCost,TotalAmount,Sqfeet,Area,PlotNo,DocumentPreparationDate) values('"+txtSNo.Text+"','"+txtDate.Text+"','"+txtAddress1.Text+"','"+txtAddress2.Text+"','"+txtAddress3.Text+"','"+txtAddress4.Text+"','"+txtAddress5.Text+"','"+txtAddress6.Text+"','"+txtClientName.Text+"','"+txtDate1.Text+"','"+txtLandSqFeet.Text+"','"+txtAmt.Text+"','"+txtContractAmt.Text+"','"+txtLandCost.Text+"','"+txtRegCost.Text+"','"+txtTotalAmt.Text+"','"+txtSqFeet.Text+"','"+txtArea.Text+"','"+txtPlotNo.Text+"','"+txtDocPreDate.Text+"')", con);
            //con.Open();
            //int n = cmd.ExecuteNonQuery();
            //con.Close();
            //if (n > 0)
            //{
            //    MessageBox.Show("record inserted");
            //    //loaddata();
            //}
            //else
            //    MessageBox.Show("insertion failed");
        }
        
    }
}


Updated 18-Sep-2014
I Am Beginner of C# Programmer

Leave Comment

Comments

Liked By