Design as follows
Button(one button)
code as follows
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Server=(local);initial catalog=Rental;Trusted_Connection=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con;
string sql = null;
string data = null;
int i = 0;
int j = 0;
Excel.Application xlapp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorksheet;
object misvalue = System.Reflection.Missing.Value;
xlapp = new Excel.ApplicationClass();
xlWorkBook = xlapp.Workbooks.Add(misvalue);
xlWorksheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//cnn = new SqlConnection(connectionstring);
con.Open();
sql = "Select * from mis";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorksheet.Cells[i + 1, j + 1] = data;
}
}
MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
when i run i get this error:Use of unassigned local variable 'con'
Please help from the above code what is the mistake.and send the code.
Thanks,
Rao
Lizzy LandyPosted Sep 7, 2012, 2:44 AM
private void btnRun_Click(object sender, System.EventArgs e)
{
//Create Workbook
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
//Insert DataTable
sheet.InsertDataTable((DataTable)this.dataGrid1.DataSource,true,2,1,-1,-1);
}
private void Form1_Load(object sender, System.EventArgs e)
{
//Get DataTable
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"..\..\..\..\..\..\Data\DataTableSample.xls");
Worksheet sheet = workbook.Worksheets[0];
this.dataGrid1.DataSource = sheet.ExportDataTable();
}
Santhosh Kumar JayaramanPosted Aug 28, 2012, 4:58 AM
public partial class _Default : System.Web.UI.Page
{
SqlConnection con ;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con== new SqlConnection("Server=(local);initial catalog=Rental;Trusted_Connection=True");
string sql = null;
string data = null;
int i = 0;
int j = 0;
Excel.Application xlapp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorksheet;
object misvalue = System.Reflection.Missing.Value;
xlapp = new Excel.ApplicationClass();
xlWorkBook = xlapp.Workbooks.Add(misvalue);
xlWorksheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//cnn = new SqlConnection(connectionstring);
con.Open();
sql = "Select * from mis";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorksheet.Cells[i + 1, j + 1] = data;
}
}
MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}