c#上传excel到数据库表

上传人:xiao****1972 文档编号:84136272 上传时间:2019-03-02 格式:DOC 页数:26 大小:136.50KB
返回 下载 相关 举报
c#上传excel到数据库表_第1页
第1页 / 共26页
c#上传excel到数据库表_第2页
第2页 / 共26页
c#上传excel到数据库表_第3页
第3页 / 共26页
c#上传excel到数据库表_第4页
第4页 / 共26页
c#上传excel到数据库表_第5页
第5页 / 共26页
点击查看更多>>
资源描述

《c#上传excel到数据库表》由会员分享,可在线阅读,更多相关《c#上传excel到数据库表(26页珍藏版)》请在金锄头文库上搜索。

1、C# 上传excel到数据库表 一usingSystem.Data; usingSystem.Data.OleDb; usingSystem.IO; usingSystem.Text;protectedvoidPage_Load(objectsender,EventArgse) publicvoidExcelToDS(stringpath) stringstrSheetName=sheet1; stringstrConn=Provider=Microsoft.Jet.OLEDB.4.0;DataSource=+path+;ExtendedProperties=Excel8.0;HDR=Yes;

2、IMEX=1; /Sql语句 stringstrExcel=select*from+strSheetName+$; DataSetds=newDataSet(); /连接数据源 OleDbConnectionconn=newOleDbConnection(strConn); conn.Open(); /适配到数据源 OleDbDataAdapteradapter=newOleDbDataAdapter(strExcel,conn); adapter.Fill(ds,data); conn.Close(); GridView1.DataSource=ds.Tablesdata; GridView

3、1.DataBind(); Label1.Text=ds.Tables0.Rows.Count.ToString(); SqlAccess.Sqldatabasesql=newSqlAccess.Sqldatabase(); StringBuildersb=newStringBuilder(); for(inti=0;ids.Tables0.Rows.Count;i+) sb.Append(insertintodatavalues(+ds.Tables0.Rowsicompanyname+,+ds.Tables0.Rowsitelname+,+ds.Tables0.Rowsiqytel+,+d

4、s.Tables0.Rowsifax+);); sql.ExecuteNonQuery(CommandType.Text,sb.ToString(); protectedvoidButton1_Click(objectsender,EventArgse) stringfileName=null; try BooleanfileOK=false; Stringpath=Server.MapPath(./doc/); if(FileUpload2.HasFile) StringfileExtension= System.IO.Path.GetExtension(FileUpload2.FileNa

5、me).ToLower(); StringallowedExtensions= .xls; /C#读取Excel中数据for(inti=0;iallowedExtensions.Length;i+) if(fileExtension=allowedExtensionsi) fileOK=true; if(fileOK) fileName=r_+DateTime.Now.ToString(yyyy_MM_dd_hh_mm_ss)+_+DateTime.Now.Millisecond+ System.IO.Path.GetExtension(FileUpload2.FileName).ToLowe

6、r(); if(File.Exists(path+fileName) Randomrnd=newRandom(10000); fileName=fileName+rnd.Next(); FileUpload2.PostedFile.SaveAs(path +fileName); else catch(Exceptionexp) ExcelToDS(Server.MapPath(.)+doc+fileName); C# 上传excel到数据库表二C# 上传excel到数据库表/ protected void Button1_ServerClick(object sender, EventArgs

7、 e) #region 变量 bool style = false; string Road = ; #endregion #region 文件上传 try string ExcelFile = this.FileUpload.PostedFile.FileName;/FileUpload为asp:FileUpload控件 string FileName = ExcelFile.Substring(ExcelFile.LastIndexOf(.) + 1); if (FileName != xls) Response.Write(alert(您上传的不是Excel文件!); return; #

8、region 设置上传路径奖文件保存到服务器 string datetime = DateTime.Now.Date.ToString(yyyyMMdd); string time = DateTime.Now.ToShortTimeString().Replace(:, ); string NewFileName = Excel + datetime + time + DateTime.Now.Millisecond.ToString() + .xls;/生成新文件名 Road = Server.MapPath(.Update) + + NewFileName; this.FileUploa

9、d.PostedFile.SaveAs(Road); #endregion catch Response.Write(alert(数据上传失败,请重新导入); return; #endregion #region 数据导入 try #region 变量 StormCIS.BLL.WorkCenter_StudentInfo bll = new StormCIS.BLL.WorkCenter_StudentInfo(); string titlename = 教师职工号, 教师姓名, 教师性别, 出生日期, 学历, 联系电话 ; DataSet ds = bll.ExcelTables(Road); #endregion #region 检验表格数据 for (int i = 0; i ds.Tables.Count; i+)

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号