您好,欢迎访问一九零五行业门户网

ASP.NET(C#)读取Excel的文件内容

.xls格式       office2003及以下版本
.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的文件内容。
其它类似信息

推荐信息