用c#导出的excel文件,再把其作为一个数据源读取里面的记录出错,怎么办?
用以下代码将一个datagrid的内容导出为一个excel
Response.AppendHeader("Content-Disposition","attachment;filename="+filename);
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
//设置输出流为简体中文
Response.ContentType = "application/ms-excel";
//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
datagrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
没有问题,但是用下面的代码读取这个导出的excel文件时出错:
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+filePath+";"+"Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open();
DataTable schemaTable = myConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,null);
string tableName=schemaTable.Rows[0][2].ToString().Trim();
string strCom = "SELECT * FROM ["+tableName+"]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom,myConn) ;
DataSet xlsDataSet = new DataSet();
myCommand.Fill(xlsDataSet,"ExcelInfo");
好像以流的方式输出的EXCEL 文件并不能作为C#读取的数据源?怎么办?
问题点数:100、回复次数:2Top
1 楼singlepine(小山)回复于 2005-12-03 23:40:49 得分 0
strCom = "SELECT * FROM ["+tableName+"]";
改成
string strCom = "SELECT * FROM ["+tableName+"$]";
Top
2 楼singlepine(小山)回复于 2005-12-03 23:43:41 得分 0
或
string strCom="SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="+filePath+";User ID=Admin;Password=;Extended properties=Excel 8.0')...["+tableName+"$]";Top




