コード例 #1
0
        private void ExportDataTableToExcel(DataTable dtImportDoc, string strFileName)
        {//将清单数据表的错误信息写入Excel。
            AposeExcel file = new AposeExcel();
            Hashtable  ht   = new Hashtable();

            ht.Add("datatable", dtImportDoc);
            ht.Add("FilePath", strFileName);
            ht.Add("SheetName", "sheet1");
            ht.Add("dtColName1", "是否成功"); // 数据集列名
            ht.Add("dtColName2", "错误信息"); // 数据集列名
            ht.Add("exColName1", "是否成功"); // excel列名
            ht.Add("exColName2", "错误信息"); // excel列名
            file.UpdateDataExcel(ht);
        }
コード例 #2
0
        public ActionResult ImportBizLogic()
        {
            DataTable dtImportDoc = null;
            var       result      = new Result(false);

            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "TempDirectory\\";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                if (Request.Files.Count > 0)
                {
                    foreach (string upload in Request.Files)
                    {
                        if (upload != null && upload.Trim() != "")
                        {
                            System.Web.HttpPostedFileBase postedFile = Request.Files[upload];
                            string filename = Path.GetFileName(postedFile.FileName);
                            if (filename.Length > 4)
                            {
                                string strExName = Path.GetExtension(filename);
                                switch (strExName.ToLower())
                                {
                                case ".xls":
                                case ".xlsx":
                                    string fileNamePath = path + Path.GetFileNameWithoutExtension(postedFile.FileName) + "_temp_" + DateTime.Now.Ticks.ToString() + strExName;
                                    postedFile.SaveAs(fileNamePath);
                                    if (postedFile.ContentLength / 1024 <= 5120)
                                    {
                                        var excel = new AposeExcel();
                                        dtImportDoc = excel.Import(fileNamePath, "");
                                        string msg = string.Empty;
                                        if (dtImportDoc.Rows.Count > 3000)
                                        {
                                            result = new Result(false, "导入数据不能大于1000条!");
                                        }
                                        else
                                        {
                                            result = ValidateDataImportDoc(dtImportDoc, fileNamePath);
                                            if (result.Success)
                                            {
                                                //入库
                                                RF.Concrete <IHM_ClientInfoRepository>().ImportClientDbo(dtImportDoc);
                                                result = new Result(true);
                                                try
                                                {
                                                    System.IO.File.Delete(fileNamePath);
                                                }
                                                catch (Exception)
                                                {
                                                }
                                            }
                                            result.Message = result.Message + "</br>" + fileNamePath;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result = new Result(false, ex.Message);
            }

            return(View("ImportClient", result));
        }