469,275 Members | 1,794 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Export and Import from Excel into a Web page

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
2 3683
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
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.

Similar topics

1 post views Thread by frankvfox | last post: by
1 post views Thread by Matt | last post: by
13 posts views Thread by Hemant Sipahimalani | last post: by
7 posts views Thread by Vanessa | last post: by
5 posts views Thread by Simon | last post: by
1 post views Thread by Arch Stanton | last post: by
3 posts views Thread by =?Utf-8?B?YzY3NjIyOA==?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.