Tuesday 28 April, 2009

Reading/Writing Excel without Office.

//reading and writing into xls file without install office at local machine
OleDbConnection objconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/DataUser/User" + loginfolder + "/" + excelsheetname + ".xls") + ";Extended Properties=Excel 8.0;");
objconn.Open();

DataTable dtSheetSchema = new DataTable();
OleDbCommand ObjCmd;

string Str_Sql;
dtSheetSchema = objconn.GetSchema("Tables"); // for getting all table list

string Str_SheetName = "[" + workbookname + "]";
ObjCmd = new OleDbCommand();
ObjCmd.Connection = objconn;
//this code using for preparing sheet columns headings
if (k == 0)
{
Str_Sql = "Select " + str_header.ToString().Substring(0, str_header.Length - 1) + " into " + Str_SheetName + " From [Sheet1$]";
ObjCmd.CommandText = Str_Sql;
ObjCmd.ExecuteNonQuery();
}

Str_Sql = "";
//this code using for insert value in sheet columns
Str_Sql = "Insert into " + Str_SheetName + "(" + str_columns.ToString().Substring(0, str_columns.Length - 1) + ") values(" + str_colvalue.ToString().Substring(0, str_colvalue.Length - 1) + ")";
ObjCmd.CommandText = Str_Sql;
ObjCmd.ExecuteNonQuery();
str_colvalue.Remove(0, str_colvalue.Length);
}
objconn.Close();

1 comment:

  1. Its a nice code.

    I had searched on net nut finally I find solution of problem from this post.

    Can you please describe how to make mail merge without office and get printout of documents which I had merged.

    Thanks,
    Narender Singh

    ReplyDelete