uploadfy导入Excel内容到数据库中2

cs:(上传文件并将Excel中的内容导入到数据库中)
public JsonResult DoImport(string uploadtype, string q_t)
        {
            iPAL.Applications.Authen.LoginUser user = (iPAL.Applications.Authen.LoginUser)(Session["LoginUser"]);
            m_userno = user.WorkNo;

            AjaxCallResult result = new AjaxCallResult();

            HttpContext.Response.ContentType = "text/plain";
            HttpContext.Response.Charset = "utf-8";

            string msg = string.Empty;
            string strUploadPath = HttpContext.Request.PhysicalApplicationPath + "iPALUploads\\";
            try
            {
                if (!Directory.Exists(strUploadPath))
                {
                    Directory.CreateDirectory(strUploadPath);
                }
                if ( HttpContext.Request.Files.Count != 1 )
                {
                    result.result = -1;
                    result.data = "上传文件只能为1.";
                    return Json(result);
                }
                HttpPostedFileBase postedFile = HttpContext.Request.Files[0];
                string fileName = postedFile.FileName;
                string ext = fileName.Substring(fileName.LastIndexOf('.'));
                string tempFile = fileName.Substring(0,fileName.Length - ext.Length -1 ) +  Guid.NewGuid().ToString("N");
                tempFile = Path.Combine(strUploadPath, tempFile) + ext;
                postedFile.SaveAs(tempFile);

                result.result = ImportSheet(tempFile, q_t, m_userno, ref msg);
                result.data = msg;

            }
            catch (Exception ex)
            {

                result.result = -1;
                result.data = ex.Message;
            }

            return Json(result);
        }
        private int ImportSheet(string filePath,string bigT,string userno,ref string msg)
        {
            ExcelReader excel = new ExcelReader();
            DataTable dt = new DataTable();
            int nRet = 0;
            List<BIGT_SHEET_MODEL> lstSheet = btModuel.GetSheetModel();//sheet
            List<BIGT_SHEET_MODEL> lstColModel = btModuel.GetColModel();//列模型
            List<BigTColModel> lstCol = new List<BigTColModel>();

            BIGT_SHEET_MODEL col;
            BIGT_SHEET_MODEL sheet;

            int nFailSheet = 0;
            for( int i=0; i < lstSheet.Count;i++)
            {
                sheet = lstSheet[i];
                if (excel.ReadFromExcel(filePath, sheet.sheet_name, dt) < 0)
                {
                    nFailSheet++;
                    if (nFailSheet == lstSheet.Count)
                    {
                        msg = "没有发现任何可识别Sheet名";
                        return -1;
                    }
                    continue;
                }
                //获取列模型
                lstCol.Clear();
                for (int k = 0; k < lstColModel.Count; k++)
                {
                    col = lstColModel[k];
                    if (string.IsNullOrEmpty(col.col_index))//只取与数据库对应的列
                    {
                        continue;
                    }
                    if (string.IsNullOrEmpty(col.col_model) || sheet.col_model == col.col_model)
                    {

                        BigTColModel colModel = new BigTColModel();
                        colModel.sheet_name = col.sheet_name;
                        colModel.col_index = col.col_index;

                        lstCol.Add(colModel);
                    }
                    else
                    {
                        continue;
                    }
                }

                nRet = ImportSheetData(userno, sheet.sheet_name, bigT, dt, lstCol, ref msg);
                if (nRet < 0)
                {
                    return -1;
                }
            }
           
            return 1;
        }

你可能感兴趣的:(upload)