I want to Insert data from SqlServer to Excel using visual studio windows application(using c# language) and sqlserver 2005.In my sqlserver,I have stock table with the columns.the stock table rows increased every day depending upon stock items.I want to give startdate and enddate using datetimepicker control in windows app.when i click the submit button means it will show the records in excel.what can i do.please help me.
I have attached my excel sheet.please find that.
Loading
Satyapriya NayakPosted Jun 19, 2012, 4:42 AM
Here is a windows application using Ms-access database.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace Export_to_Excel_in_windows
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
DataSet ds;
OleDbDataAdapter oledbda;
//DataTable dt;
string str;
public Form1()
{
InitializeComponent();
}
private void btndisplay_Click(object sender, EventArgs e)
{
try
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from employee where CreatedDate between '" +dateTimePicker1.Value.Date.ToString() + "' and '" + dateTimePicker2.Value.Date.ToString() + "'";
//str ="select * from employee where CreatedDate>='" + dateTimePicker1.Value.Date.ToString() + "' and CreatedDate<='" + dateTimePicker2.Value.Date.ToString() + "'";
com = new OleDbCommand(str, con);
ds = new DataSet();
oledbda = new OleDbDataAdapter(com);
oledbda.Fill(ds, "employee");
con.Close();
DataGridView1.DataSource = ds;
DataGridView1.DataMember = "employee";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn_export_Click(object sender, EventArgs e)
{
//We have to add a reference to the Microsoft Excel object library.
//Right click on your project and select Add Reference menu. After that go to COM tab and select and add Microsoft Excel 12.0 object library.
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = true;
try
{
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
worksheet.Name = "Exported from DataGridView";
for (int i = 1; i < DataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = DataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < DataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < DataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = DataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
string fileName = String.Empty;
saveFileExcel.Filter = "Excel files |*.xls|All files (*.*)|*.*";
saveFileExcel.FilterIndex = 2;
saveFileExcel.RestoreDirectory = true;
if (saveFileExcel.ShowDialog() == DialogResult.OK)
{
fileName = saveFileExcel.FileName;
workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
else
return;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
app.Quit();
workbook = null;
app = null;
}
}
}
}
Thanks
gokilavasan mPosted Jul 13, 2012, 2:28 AM
I want to Insert and update data from sql to excel using stored procedure using web application.my stored procedurename-sp_AdhocCost.I attached my excel file(For update adhoc cost column).what can i do .please tell me.
Thanks & regards,
gokilavasan.M
gokilavasan mPosted Jun 20, 2012, 7:16 AM
Pravin GhadgePosted Jun 19, 2012, 8:41 AM
Just right click on that cell & then click on format cells->then select date format
gokilavasan mPosted Jun 19, 2012, 6:27 AM