lunes, 30 de enero de 2012

Export database to excel without MS Excel

try
{
if (File.Exists(@"c:\exportado.xls"))
File.Delete(@"c:\exportado.xls");

using(System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\exportado.xls';Extended Properties='Excel 8.0;HDR=Yes'"))
using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("", con))
{
cmd.Connection = con;
con.Open();

string sql = "CREATE TABLE [Precios] ( columna1 VARCHAR(100) )";
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();

//sql = "UPDATE [Precios$A1:A1] SET columna1 = '12345678'";
sql = "INSERT INTO [Precios] (columna1) VALUES('12345678')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

con.Close();
}

MessageBox.Show("Insert Terminado");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}