I am using Response.write to export my datatable to an excel file like this thread find it below, To open the created file, you get a warning like attached which is ok for now, when you open the file edit the data, save, and try to read the file in the application, I get no records in the file.
dt = city.GetAllCity();//your datatable
string attachment = "attachment; filename=city.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");
int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
I am using the following code to read the file
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;IMEX=1\"";
using (OleDbConnection connection = new OleDbConnection(string.Format(connectionString, file)))
connection.Open();
var tableSchema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); //I get zero tables/sheets here
I tried opening the file using the following connection properties, but fail to read records too. I am also aware of 3rd parties doing that functionality, but I am trying to get my current code to read the file and get the new data without using 3rd parties.
connectionString2 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"HTML Import;\"";