0

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;\"";

warning message

Ahmed Moheb
  • 764
  • 1
  • 10
  • 20
  • The post that you included contains numerous code examples. Which one are you using? For connection strings, see https://www.connectionstrings.com/ace-oledb-12-0/. How are you editing the data? What version of Excel are you using? Not sure what you mean by "upload to the application and try to read the file back". You may want to re-write your description of the issue and provide more code and/or images. Knowing the data types of the data in the Excel file may be helpful (an image would be helpful). – Tu deschizi eu inchid Feb 11 '21 at 14:46
  • @user9938 code added – Ahmed Moheb Feb 11 '21 at 15:12

0 Answers0