/// <summary> /// 附件添加 /// </summary> /// <param name="context"></param> public void FilseAdd(HttpContext context) { string OperationID = context.Request.QueryString["OperationID"]; //业务ID string SessionID = context.Request.QueryString["SessionID"]; //SessionID string FileType = context.Request.QueryString["FileType"]; //附件的类型 SessionUserModel currentUser = context.Session["UserInfo"] as SessionUserModel; string pt = context.Request["Filename"].ToString(); HttpPostedFile postedFile = context.Request.Files["Filedata"]; //获取上传信息对象 string filename = postedFile.FileName; //获取上传的文件 名字 string tempPath = FileType == "系统必备工具" ? "/Download/" : UploadFileCommon.CreateDir("File"); //获取保存文件夹路径。 string savepath = context.Server.MapPath(tempPath); //获取保存路径 string sExtension = filename.Substring(filename.LastIndexOf('.')); //获取拓展名 int fileLength = postedFile.ContentLength; //获取文件大小 string fileContentLength = fileSizs(postedFile.ContentLength); //转换成MB,GB,TB if (!Directory.Exists(savepath)) //查看当前文件夹是否存在 { Directory.CreateDirectory(savepath); } //string sNewFileName = DESEncrypt.Encrypt(DateTime.Now.ToString("yyyyMMddhhmmsfff"));//上传后的文件名字 string sNewFileName = Guid.NewGuid().ToString(); if (OperationID != "" && SessionID != "" && OperationID != null) { List <Sys_ModelFile> ModelFilelist = null; if (context.Session[SessionID] == null) { ModelFilelist = new List <Sys_ModelFile>(); } else { ModelFilelist = context.Session[SessionID] as List <Sys_ModelFile>; } Sys_ModelFile model = new Sys_ModelFile(); model.FileID = Guid.NewGuid().ToString(); model.File_Name = filename; model.File_Extension = sExtension; model.File_Size = fileLength; model.File_Path = tempPath + sNewFileName + sExtension; model.File_AddTime = DateTime.Now; model.File_OperationID = OperationID; model.File_UserID = currentUser.UserID; model.File_Type = FileType; try { //postedFile.SaveAs(savepath + "@/" + sNewFileName + sExtension);//保存 postedFile.SaveAs(savepath + sNewFileName + sExtension);//保存 postedFile = null; ModelFilelist.Add(model); context.Session[SessionID] = ModelFilelist; string fileUrl = "/Files/Download.aspx?path=" + model.File_Path + "&filename=" + model.File_Name; context.Response.Write("{filadd:'true',id:'" + model.FileID + "',filename:'" + model.File_Name + "',filesize:'" + fileContentLength + "',uploaddate:'" + Convert.ToDateTime(model.File_AddTime).ToString("yyyy年MM月dd日") + "',fileUrl:'" + fileUrl + "',filePicUrl:'" + model.File_Path + "'}"); } catch { context.Response.Write("{filadd:'false',filename:'" + model.File_Name + "'}"); } } else { context.Response.Write("{filadd:'false',filename:'" + filename + "'}");//在没有业务ID时候,SessionID不能为空 } //context.Response.End(); }
/// <summary> /// /// </summary> /// <param name="dt"></param> protected String ExportExcel(DataTable dt) { if (dt == null || dt.Rows.Count == 0) { return("0"); } Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { return("0"); } System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; Microsoft.Office.Interop.Excel.Range range; long totalCount = dt.Rows.Count; long rowRead = 0; float percent = 0; for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]; range.Interior.ColorIndex = 15; range.Font.Bold = true; } for (int r = 0; r < dt.Rows.Count; r++) { for (int i = 0; i < dt.Columns.Count; i++) { if (i == 3) { string a = dt.Rows[r][i].ToString(); if (a != "0") { Sys_Department dep = depbll.Find(p => p.Department_Code == a); if (dep != null) { worksheet.Cells[r + 2, i + 1] = dep.Department_Name; } } } else if (i == 2) { worksheet.Cells[r + 2, i + 1] = "'" + dt.Rows[r][i].ToString(); } else if (i == 0) { worksheet.Cells[r + 2, i + 1] = "'" + dt.Rows[r][i].ToString(); } else { worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i].ToString(); } } rowRead++; percent = ((float)(100 * rowRead)) / totalCount; } //xlApp.Visible = true; workbook.Saved = true; string tempPath = UploadFileCommon.CreateDir("EXL"); if (!Directory.Exists(tempPath))//查看当前文件夹是否存在 { Directory.CreateDirectory(tempPath); } try { string sNewFileName = DateTime.Now.ToString("yyyyMMddhhmmsfff");//上传后的文件名字 string ph = tempPath + @"/" + sNewFileName + ".xlsx"; workbook.SaveAs(System.Web.HttpContext.Current.Request.MapPath(ph)); return(ph); } catch { return("0"); } finally { workbook.Close(true, Type.Missing, Type.Missing); workbook = null; xlApp.Quit(); xlApp = null; } }
public void DataFilseAdd(HttpContext context) { SessionUserModel currentUser = context.Session["UserInfo"] as SessionUserModel; string pt = context.Request["Filename"].ToString(); HttpPostedFile postedFile = context.Request.Files["Filedata"]; //获取上传信息对象 string filename = postedFile.FileName; //获取上传的文件 名字 string tempPath = UploadFileCommon.CreateDir("File"); //获取保存文件夹路径。 string savepath = context.Server.MapPath(tempPath); //获取保存路径 string sExtension = filename.Substring(filename.LastIndexOf('.')); //获取拓展名 int fileLength = postedFile.ContentLength; //获取文件大小 string fileContentLength = fileSizs(postedFile.ContentLength); //转换成MB,GB,TB if (!Directory.Exists(savepath)) //查看当前文件夹是否存在 { Directory.CreateDirectory(savepath); } //string sNewFileName =DESEncrypt.Encrypt(DateTime.Now.ToString("yyyyMMddhhmmsfff"));//上传后的文件名字 string sNewFileName = Guid.NewGuid().ToString(); string OperationID = context.Request.QueryString["OperationID"]; //业务ID string SessionID = context.Request.QueryString["SessionID"]; //SessionID string FileType = context.Request.QueryString["FileType"]; //附件的类型 if (!string.IsNullOrEmpty(FileType)) { FileType = "未归类"; } //if (!filename.Contains("-")) //{ // FileType = context.Request.QueryString["FileType"];//附件的类型 // if (!string.IsNullOrEmpty(FileType)) // { // FileType = HttpUtility.UrlDecode(FileType); // } //} //else //{ // string[] fname = filename.Split('-'); // if (fname.Count() > 0) // { // FileType = fname[0]; // } // else // { // FileType = context.Request.QueryString["FileType"];//附件的类型 // if (!string.IsNullOrEmpty(FileType)) // { // FileType = HttpUtility.UrlDecode(FileType); // } // } //} //string types = context.Request.QueryString["FileType2"];//当前选中的类别节点 //if (!string.IsNullOrEmpty(types)) //{ // types = HttpUtility.UrlDecode(types); //} if (OperationID != "" && SessionID != "" && OperationID != null) { List <Sys_ModelFile> ModelFilelist = null; if (context.Session[SessionID] == null) { ModelFilelist = new List <Sys_ModelFile>(); } else { ModelFilelist = context.Session[SessionID] as List <Sys_ModelFile>; } Sys_ModelFile model = new Sys_ModelFile(); model.FileID = Guid.NewGuid().ToString(); model.File_Name = filename; model.File_Extension = sExtension; model.File_Size = fileLength; model.File_Path = tempPath + sNewFileName + sExtension; model.File_AddTime = DateTime.Now; model.File_OperationID = OperationID; model.File_UserID = currentUser.UserID; model.File_Type = FileType; string hz = ""; Regex r = new Regex(@"[\u4e00-\u9fa5]+"); Match mc1 = r.Match(FileType); if (mc1.Length != 0) //类别中含有汉字 { hz = "..(未归类)"; } try { postedFile.SaveAs(savepath + sNewFileName + sExtension);//保存 postedFile = null; ModelFilelist.Add(model); context.Session[SessionID] = ModelFilelist; string filename2 = model.File_Name; if (filename2.Length > 9) { Match mc = r.Match(filename2); if (mc.Length != 0) //名称中含有汉字 { filename2 = filename2.Substring(0, 7) + hz; } else { filename2 = filename2.Substring(0, 9) + hz; } } else { filename2 = filename2 + hz; } context.Response.Write("{filadd:'true',id:'" + model.FileID + "',filename:'" + model.File_Name + "',filesize:'" + fileContentLength + "',uploaddate:'" + Convert.ToDateTime(model.File_AddTime).ToString("yyyy年MM月dd日") + "',fileUrl:'" + model.File_Path + "',filetitle:'" + filename2 + "'}"); } catch { context.Response.Write("{filadd:'false',filename:'" + model.File_Name + "'}"); } } else { context.Response.Write("{filadd:'false',filename:'" + filename + "'}");//在没有业务ID时候,SessionID不能为空 } //context.Response.End(); }
protected void Page_Load(object sender, EventArgs e) { HttpPostedFile postedFile = Request.Files["File1"]; string depCode = Request["depCode"]; if (string.IsNullOrEmpty(depCode)) { depCode = "001"; } try { if (postedFile.FileName != "") { string tempPath = UploadFileCommon.CreateDir("Uplod"); //获取保存文件夹路径。 string savepath = Server.MapPath(tempPath); //获取保存路径 string fileExtension = System.IO.Path.GetExtension(postedFile.FileName).ToLower(); if (!Directory.Exists(savepath)) //查看当前文件夹是否存在 { Directory.CreateDirectory(savepath); } string sNewFileName = DateTime.Now.ToString("yyyyMMddhhmmsfff");//上传后的文件名字 string fname = tempPath + sNewFileName + fileExtension; string allowedExtensions = ".xlsx|.xls"; if (!allowedExtensions.Contains(fileExtension)) { //this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('请选择Excel文件!');col()", true); MessageBox.Show(this, "请选择Excel文件"); return; } postedFile.SaveAs(Server.MapPath(fname)); string filename = Server.MapPath(fname); ImportExcl excl = new ImportExcl(); DataTable dt = excl.ExcelToDataSet(filename); if (dt != null) { int a = dt.Rows.Count; List <Sys_UserInfo> listUsermodel = new List <Sys_UserInfo>(); List <Sys_Post> listpostmodel = new List <Sys_Post>(); for (int i = 0; i < a; i++) { Sys_UserInfo sysmodel = new Sys_UserInfo(); sysmodel.UserInfoID = Guid.NewGuid().ToString(); sysmodel.UserInfo_FullName = dt.Rows[i][1].ToString(); string Post_Name = dt.Rows[i][2].ToString(); Sys_Post postmodel = postbll.Find(p => p.Post_Name == Post_Name && p.Post_DepCode == depCode); Sys_Post post = new Sys_Post(); if (postmodel == null) { post.PostID = Guid.NewGuid().ToString(); post.Post_Name = dt.Rows[i][2].ToString(); post.Post_DepCode = depCode; post.Post_AddUserID = CurrUserInfo().UserID; post.Post_AddTime = DateTime.Now; sysmodel.UserInfo_Post = post.PostID; listpostmodel.Add(post); } else { sysmodel.UserInfo_Post = postmodel.PostID; } sysmodel.UserInfo_Position = dt.Rows[i][3].ToString(); sysmodel.UserInfo_PhoneNumber = dt.Rows[i][4].ToString(); sysmodel.UserInfo_PositionLevel = dt.Rows[i][5].ToString(); string DataDict_Name = dt.Rows[i][6].ToString(); Sys_DataDict DataDictmodel = DataDictbll.Find(p => p.DataDict_Name == DataDict_Name); if (DataDictmodel != null) { sysmodel.UserInfo_Type = DataDictmodel.DataDict_Code; } else { sysmodel.UserInfo_Type = "0403"; } sysmodel.UserInfo_LoginUserName = dt.Rows[i][1].ToString(); sysmodel.UserInfo_LoginUserPwd = DESEncrypt.Encrypt("111111"); sysmodel.UserInfo_DepCode = depCode; sysmodel.UserInfo_RoleID = "02f13817-9f74-4169-b279-4b00cc741a91"; sysmodel.UserInfo_Status = "0301"; sysmodel.UserInfo_Sequence = int.Parse(dt.Rows[i][0].ToString()); listUsermodel.Add(sysmodel); } bool u = userbll.AddLists(listpostmodel, listUsermodel); if (u) { this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('导入成功!');col()", true); } else { MessageBox.Show(this, "导入失败!"); } } } else { MessageBox.Show(this, "导入失败!"); } } catch { } }