---
title: "Write to Excel Sheet"  
description: "using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.Windows;  using System.Windows.Controls;  usi"  
author: "Karuppaiah T"  
published: 2013-08-11  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/563/write-to-excel-sheet  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Write to Excel Sheet

```
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");        }            }}
```

\

---

Original Source: https://www.mindstick.com/blog/563/write-to-excel-sheet

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
