.xlsx花样 Office2007 及以上版本
.csv花样 以逗号分开的字符串文本(能够将上述两种文件范例另存为此花样)
读取前两种花样和读取后一种花样会用两种差别的要领。
下面看顺序:
页面前台:
<div> <%-- 文件上传控件 用于将要读取的文件上传 并经由过程此控件猎取文件的信息--%> <asp:FileUpload ID="fileSelect" runat="server" /> <%-- 点击此按钮实行读取要领--%> <asp:Button ID="btnRead" runat="server" Text="ReadStart" /> </div>
背景代码:
//声明变量(属性) string currFilePath = string.Empty; //待读取文件的全途径 string currFileExtension = string.Empty; //文件的扩展名 //Page_Load事宜 注册按钮单击事宜 protected void Page_Load(object sender,EventArgs e) { this.btnRead.Click += new EventHandler(btnRead_Click); } //按钮单击事宜 //内里的3个要领将在下面给出 protected void btnRead_Click(object sender,EventArgs e) { Upload(); //上传文件要领 if(this.currFileExtension ==".xlsx" || this.currFileExtension ==".xls") { DataTable dt = ReadExcelToTable(currFilePath); //读取Excel文件(.xls和.xlsx花样) } else if(this.currFileExtension == ".csv") { DataTable dt = ReadExcelWidthStream(currFilePath); //读取.csv花样文件 } }
下面列出按钮单击事宜中的3个要领
///<summary> ///上传文件到暂时目次中 ///</ummary> private void Upload() { HttpPostedFile file = this.fileSelect.PostedFile; string fileName = file.FileName; string tempPath = System.IO.Path.GetTempPath(); //猎取体系暂时文件途径 fileName = System.IO.Path.GetFileName(fileName); //猎取文件名(不带途径) this.currFileExtension = System.IO.Path.GetExtension(fileName); //猎取文件的扩展名 this.currFilePath = tempPath + fileName; //猎取上传后的文件途径 记录到前面声明的全局变量 file.SaveAs(this.currFilePath); //上传 } ///<summary> ///读取xls\xlsx花样的Excel文件的要领 ///</ummary> ///<param name="path">待读取Excel的全途径</param> ///<returns></returns> private DataTable ReadExcelToTable(string path) { //衔接字符串 string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能涌现过剩的空格 而且分号注重 //string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Office 07以下版本 由于本人用Office2010 所以没有用到这个衔接字符串 可根据本身的状况挑选 或许顺序推断要用哪个衔接字符串 using(OleDbConnection conn = new OleDbConnection(connstring)) { conn.Open(); DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"Table"}); //获得一切sheet的名字 string firstSheetName = sheetsName.Rows[0][2].ToString(); //获得第一个sheet的名字 string sql = string.Format("SELECT * FROM [{0}],firstSheetName); //查询字符串 OleDbDataAdapter ada =new OleDbDataAdapter(sql,connstring); DataSet set = new DataSet(); ada.Fill(set); return set.Tables[0]; } } ///<summary> ///读取csv花样的Excel文件的要领 ///</ummary> ///<param name="path">待读取Excel的全途径</param> ///<returns></returns> private DataTable ReadExcelWithStream(string path) { DataTable dt = new DataTable(); bool isDtHasColumn = false; //标记DataTable 是不是已生成了列 StreamReader reader = new StreamReader(path,System.Text.Encoding.Default); //数据流 while(!reader.EndOfStream) { string meaage = reader.ReadLine(); string[] splitResult = message.Split(new char[]{','},StringSplitOption.None); //读取一行 以逗号分开 存入数组 DataRow row = dt.NewRow(); for(int i = 0;i<splitResult.Length;i++) { if(!isDtHasColumn) //假如还没有生成列 { dt.Columns.Add("column" + i,typeof(string)); } row[i] = splitResult[i]; } dt.Rows.Add(row); //添加行 isDtHasColumn = true; //读取第一行后 就标记已存在列 再读取今后的行时,就不再生成列 } return dt; }
以上就是asp.net(C#)怎样读取Excel的文件的实例详解的细致内容,更多请关注ki4网别的相干文章!