标签:style blog http io ar color os sp for
文件上传—Excel(也可其他文件)
最近在公司里面学会了一种Excel上传的操作,个人感觉这种还行也挺容易理解。
下面贴出实例代码,写得不好请大家不要见怪。
先给一张EXCEL的表数据结构吧:
然后是读取EXCEL并保存的操作:
protected void btnread_Click(object sender, EventArgs e) { if (fupload.PostedFile.InputStream.Length < 1) //判断存不存在上传文件 { Response.Write("<script>alert(‘请选择文件!‘)</script>"); return; } string FileName = fupload.FileName;//上传文件文件名 string FilePath = fupload.PostedFile.FileName;//上传文件完整路径+文件名 if (FileName.ToLower().IndexOf(".xls") == -1 || FileName.ToLower().IndexOf(".xlsx") == -1)//上传的文件类型 { Response.Write("<script>alert(‘只可以选择Excel文件‘)</script>"); return; } string temppath = Request.PhysicalApplicationPath + "UPz//up//data//";//上传路径 if (!Directory.Exists(temppath)) { Directory.CreateDirectory(temppath); } string filepath = temppath + FileName; try { //保存文件 fupload.SaveAs(filepath); } catch (Exception) { esponse.Write("<script>alert(‘文件保存失败!‘)</script>"); return; } }
接下来就是写入数据库的操作:
protected void btnBc_Click(object sender, EventArgs e) { string truePath = Request.PhysicalApplicationPath + "UpZ//" + System.DateTime.Now.ToString("yyyyMMdd") + "//up//"; if (!Directory.Exists(truePath)) { Directory.CreateDirectory(truePath); } try { File.Copy(Request.PhysicalApplicationPath + "UpZ//up//data//" + lblfileName.Text.Trim(), truePath + lblfileName.Text.Trim(), true); FileAndInst fi = new FileAndInst(); fi.filename = lblfileName.Text.Trim(); Response.Write("<script>alert(‘保存成功!‘)</script>"); Thread thread = new Thread(FileJX); thread.IsBackground = true;//这样能随主程序一起结束 thread.Start((object)fi);//开启一个线程 } catch (Exception) { Response.Write("<script>alert(‘保存失败!‘)</script>"); } File.Delete(lblfilePath.Text.Trim()); } struct FileAndInst { public string filename;} //定义一个结构体 public void FileJX(object aa) { string pathZML = AppDomain.CurrentDomain.BaseDirectory; string temppath = pathZML + "UPz//up//data//"; FileAndInst fi = (FileAndInst)aa; string FileName = fi.filename; string filePath = pathZML + "UpZ//" + System.DateTime.Now.ToString("yyyyMMdd") + "//up//" + FileName; try { DataSet ds = ExcelSqlConnection(filePath, FileName);//连接Excel 读取Excel数据 并返回DataSet数据集合 DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组 int rowsnum = ds.Tables[0].Rows.Count; if (rowsnum == 0) { Response.Write("<script>alert(‘Excel表为空表,无数据!‘)</script>"); //当Excel表为空时,对用户进行提示 } else { //因为我数据在EXCEL表中第一行属于文件头不不读取,即从第二行开始的;而我需要读取并写入数据库的数据在第四行,所以这里我直接从2开始循环 for (int i = 2; i < dr.Length; i++) { //xuhao = dr[i][0].ToString().Trim(); //第一列的序号我不需要写入数据库 bankNo = dr[i][1].ToString().Trim(); //银行行号 accno = dr[i][2].ToString().Trim();//签约账号 accName = dr[i][3].ToString().Trim(); //账号户名 papertype = dr[i][4].ToString().Trim();//证件类型下面就是 //等等数据,下面我就不列举了 //获取完数据后就进行数据插入就行了 } } } catch (Exception ex) { PrintLog.WriteLog("文件信息校验异常!文件路径:" + filePath + "异常信息:" + ex.Message.ToString(), "DPP.txt"); } } /// <summary> /// 连接Excel 读取Excel数据 并返回DataSet数据集合 /// </summary> /// <param name="filepath">Excel服务器路径</param> /// <param name="filename">Excel表名称</param> /// <returns></returns> public static System.Data.DataSet ExcelSqlConnection(string filepath, string filename) { string fileType = filename.Split(‘.‘)[1];//文件后缀名 string strCon = string.Empty; if (fileType == "xls") { strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=‘Excel 8.0;HDR=YES;IMEX=1‘"; } else if (fileType == "xlsx") { strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=‘Excel 12.0;IMEX=1‘"; } OleDbConnection ExcelConn = new OleDbConnection(strCon); try { string strCom = string.Format("SELECT * FROM [Sheet1$]"); ExcelConn.Open(); OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn); DataSet ds = new DataSet(); myCommand.Fill(ds, "[" + filename + "$]"); ExcelConn.Close(); return ds; } catch { ExcelConn.Close(); return null; } }
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/witeem/p/4164745.html