用以下代码将一个datagrid的内容导出为一个excelResponse.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#读取的数据源?怎么办?
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#读取的数据源?怎么办?
生成/读取(反向更新数据库) Excel文件
http://blog.csdn.net/ChengKing/archive/2005/11/29/539514.aspx
如下 验证过的东东//创建一个数据链接
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;
string strCom1 = " SELECT * FROM [Sheet1$] " ; //选择出数据
//string strCom1 = "update [Sheet1$] set name='ggg' " ;//更新数据 "name"--> (excel里第一行数据相当于字段名)
OleDbCommand myconmm=new OleDbCommand (strCom1,myConn);
myconmm.ExecuteNonQuery ();
//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
//创建一个 DataSet对象
DataSet myDataSet = new DataSet ( ) ;
//得到自己的DataSet对象
myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
//关闭此数据链接
myConn.Close ( ) ;
name age addr
wang 22 beijing
zhao 23 shanghaiSELECT age,addr FROM [Sheet1$] where name='wang'
就会选择出 22,beijing
没有的话应该
string strCom = "SELECT * FROM ["+tableName+"$]";