1 usingSystem;2 usingSystem.Collections;3 usingSystem.Collections.Generic;4 usingSystem.Configuration;5 usingSystem.Data;6 usingSystem.Data.OleDb;7 usingSystem.Web;8 usingSystem.IO;9 usingSystem.Linq;10 usingNPOI.HSSF.UserModel;11 usingSystem.Text;12 usingSystem.Web.UI.WebControls;13 usingSystem.Xml.Schema;14
15
16 namespaceMd.Api.Web17 {18 ///
19 ///ExcelFileUploadHQL 的摘要说明20 ///
21 public classExcelFileUploadHQL : IHttpHandler22 {23 private static int CompID { get; set; }24 private static int CreateUser { get; set; }25 private static int ProjectID { get; set; }26 private static string ProjectCode { get; set; }27
28 private stringPath29 {30 get
31 {32 string path = ConfigurationManager.AppSettings["filepath"];33
34 try
35 {36 path =HttpContext.Current.Server.MapPath(path);37 }38 catch(Exception)39 {40 }41 returnpath;42 }43 }44
45 ///
46 ///接收请求47 ///
48 ///
49 public voidProcessRequest(HttpContext context)50 {51 string result = "";52 context.Response.ContentType = "text/plain";53 HttpPostedFile postFile = context.Request.Files["Filedata"];54 CompID = Convert.ToInt32(context.Request["CompID"] + "");55 CreateUser = Convert.ToInt32(context.Request["CreateUser"] + "");56 ProjectID = Convert.ToInt32(context.Request["ProjectID"] + "");57 ProjectCode = context.Request["ProjectCode"] + "";58
59 string excelName =postFile.FileName;60 string savePath = "";61
62 try
63 {64
65 /*文件是否有内容*/
66 if (postFile != null && postFile.ContentLength > 0)67 {68 if (string.IsNullOrEmpty(excelName))69 {70 excelName = Guid.NewGuid().ToString("N") +System.IO.Path.GetExtension(postFile.FileName);71 }72
73 if (!File.Exists(Path))74 {75 System.IO.Directory.CreateDirectory(Path);76 }77
78 string Mess = "";79 savePath = Path +excelName;80 postFile.SaveAs(savePath);81
82 if (Mess != "")83 {84 //错误输出
85 context.Response.Write("false@" +Mess);86 }87
88 Mess =DealExcelForHangQiLun(LoadDataFromExcel(savePath));89 if (string.IsNullOrEmpty(Mess))90 {91 Mess = "导入成功!" + "@true";92 }93 result = "true@" +Mess;94 }95 }96 catch(Exception ex)97 {98 result = "false@上传失败!" +ex.ToString();99 }100 context.Response.Write(result);101 }102
103 public static stringDealExcelForHangQiLun(DataSet ds)104 {105 try
106 {107 string errorRow = "";108 string repeatstr = "";109 int clevel = 0;110 Dictionary goodsdictionary = new Dictionary();111
112 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)113 {114 //获取Excel 最大层级并分级
115 Dictionary&