I created an excel sheet from a datatable using a function. I want to read an excel sheet programmatically using the following connection string. This line works fine for all other excel sheets, but not for the one I created using this function. I think this is due to a problem with the version of Excel.
OleDbConnection conn= new OleDbConnection("Data Source='" + path +"';provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;";);
Can anyone suggest a way that I can create an Excel sheet so that it can be read again using the above query. I cannot use the Microsoft InterOp library because it is not supported by my host. I even changed different encoding formats. However, it does not work.
public void ExportDataSetToExcel(DataTable dt)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Charset = "utf-8";
response.ContentEncoding = Encoding.GetEncoding("utf-8");
response.ContentType = "application/vnd.ms-excel";
Random Rand = new Random(); int iNum = Rand.Next(10000, 99999);
string extension = ".xls";
string filenamepath = AppDomain.CurrentDomain.BaseDirectory + "graphs\\" + iNum + ".xls";
string file_path = "graphs/" + iNum + extension;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + iNum + "\"");
string query = "insert into graphtable(graphtitle,graphpath,creategraph,year) VALUES('" + iNum.ToString() + "','" + file_path + "','" + true + "','" + DateTime.Now.Year.ToString() + "')";
try
{
int n = connect.UpdateDb(query);
if (n > 0)
{
resultLabel.Text = "Merge Successfull";
}
else
{
resultLabel.Text = " Merge Failed";
}
resultLabel.Visible = true;
}
catch { }
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
DataGrid dg = new DataGrid();
dg.DataSource = dt;
dg.DataBind();
dg.RenderControl(htw);
File.WriteAllText(filenamepath, sw.ToString());
response.Write(sw.ToString());
response.End();
}
}
}
source
share