By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

Export and Import from Excel into a Web page

P: n/a
Siu
Hi,
I use the following code to export and import a file Excel from resp. into a
Web page with the following code:

//EXPORT
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
HtmlTextWriter(oStringWriter);

DataGrid1.AllowPaging = false;

DataGrid1.DataBind();
this.ClearControls(DataGrid1);
DataGrid1.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

Response.End();
DataGrid1.AllowPaging = true;
DataGrid1.DataBind();

//IMPORT
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
strFileNamePath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
string sql = "SELECT * FROM [Sheet1$]";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection) ;
DataGrid1.DataSource = rd;
DataGrid1.DataBind();
rd.Close();

The export code works fine, whether the import code has a problem,namely:
when I import a Excel file which I have created from scratch with two simple
column, the Import code works.... When I import an Excel file which I've
exported with my Export code, I obtain the following error at the line
conn.Open():

External table is not in the expected format.

I've already check in the exported Excel file,
the name of the sheet which is "Sheet1" and I've also deleted the formatting
namely rows and columns colors... but it doesn't work!!

How can I solve the problem?

Thanks :)
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Siu
I've discovered that the export code creates a file which has HTML format and
it is visible in Excel: in order to make the import code working I should
manually convert the HTML file into a .xls file.... My question is now:

how can I code this function of conversion from HTML to .xls (... like a
Save As... function) in the codebehind??

Please help me ;)

"Siu" wrote:
Hi,
I use the following code to export and import a file Excel from resp. into a
Web page with the following code:

//EXPORT
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
HtmlTextWriter(oStringWriter);

DataGrid1.AllowPaging = false;

DataGrid1.DataBind();
this.ClearControls(DataGrid1);
DataGrid1.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

Response.End();
DataGrid1.AllowPaging = true;
DataGrid1.DataBind();

//IMPORT
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
strFileNamePath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
string sql = "SELECT * FROM [Sheet1$]";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection) ;
DataGrid1.DataSource = rd;
DataGrid1.DataBind();
rd.Close();

The export code works fine, whether the import code has a problem,namely:
when I import a Excel file which I have created from scratch with two simple
column, the Import code works.... When I import an Excel file which I've
exported with my Export code, I obtain the following error at the line
conn.Open():

External table is not in the expected format.

I've already check in the exported Excel file,
the name of the sheet which is "Sheet1" and I've also deleted the formatting
namely rows and columns colors... but it doesn't work!!

How can I solve the problem?

Thanks :)

Nov 19 '05 #2

P: n/a
This isn't .NET specific, but a turnkey product to allow Excel-web
interactivity (multiple users updating the same workbooks using only
web browsers) is BadBlue Enterprise. See
http://badblue.com/helpxls.htm - while charts and real-time editing
(with cell-by-cell security for each user) is supported, I"m not sure
formatting of cells comes across.

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.