I am trying to convert a DataTable into XLS file.
Code below
Snippet1
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=New.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
for (int i = 1; i < gvGridView.HeaderRow.Cells.Count; i++)
{
gvGridView.HeaderRow.Cells[i].Text = Table.Columns[i - 1].ColumnName;
}
for (int i = 0; i < gvGridView.Rows.Count; i++)
{
//Apply text style to each Row
gvGridView.Rows[i].Attributes.Add("class", "textmode");
}
gvGridView.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
This code seems fine. It produced an xls file. And it can be opened correctly. However I tried to reverse engineer this process. In short, converting back this particular xls file into a datatable. Code below:
Snippet2
int idx = filen.IndexOf(".");
string tf = filen.Remove(idx, 4);
OleDbConnection MyConnection = null;
DataSet DtSet = null;
OleDbDataAdapter MyCommand = null;
//Connection for MS Excel 2003 .xls format
MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties=Excel 8.0;");
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [table$]", MyConnection);
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
dt = DtSet.Tables[0];
MyConnection.Close();
if (dt.Rows.Count > 0)
{
theGridView.DataSource = dt;
theGridView.DataBind();
}
if(System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
This code produced error. It says:
External file format not supported
I noticed this error is similar when you use the wrong OLEDB provider. But in my case there is supposed to be no problem as I am using Microsoft.Jet.OLEDB.4.0 for .xls (2003) format. But somehow it happened still.
This code somehow works for any other .xls file that I tested. It doesn’t work only to the files that produced from conversion using the #Snippet1. However supposed I produced a file using #Snippet1 and then I copy all the cells into a new .xls file. The #Snippet2 will work against this new file. In short, only file directly produced from #Snippet1 that won’t work.
I figured I did something wrong. So I hope that somebody might be able to help me here.
Your Excel output snippet OS not generating an Excel file. It is generating an html table and setting the content type to tell the computer to open it with Excel.
You will need to use a library to generate a real excel file or use the excel data interop (there are plenty of articles in both) in order to consume it directly.
Alternatively you could generate a .csv file instead and still keep the Excel content-type so excel opens it but you lose formatting. Then you import can check the extension to read it either as excel or flat text.