예제 #1
0
        public JsonResult Import(int id, string date, HttpPostedFileBase file)
        {
            JsonResultData result = new JsonResultData();

            try
            {
                string   fileName      = Path.GetFileName(file.FileName);
                string   fileExtension = Path.GetExtension(fileName).ToLower();
                DateTime baseDate      = DateTime.Today;
                if (string.IsNullOrWhiteSpace(date) == false)
                {
                    DateTime.TryParse(date, out baseDate);
                }

                if (fileExtension != ".xlsx")
                {
                    result.Message = "上传的文件格式错误,只能上传后缀为.xlsx的Excel文件";
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }
                BF_IMPORT.Entity entity = BF_IMPORT.Instance.GetEntityByKey <BF_IMPORT.Entity>(id);
                if (entity == null)
                {
                    result.Message = "未找到配置" + id;
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }

                string path = System.Web.HttpContext.Current.Server.MapPath("~/tmp/up/");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                string saveName = DateTime.Now.ToString("yyyyMMdd_HHmmss_") + SystemSession.UserID + "_" + fileName;

                file.SaveAs(path + saveName);
                string message = string.Empty;
                Dictionary <int, string> errorList = new Dictionary <int, string>();
                int n = BF_IMPORT.ImportDataFile(entity, path + saveName, baseDate, out message, out errorList);
                if (n < 1)
                {
                    BLog.Write(BLog.LogLevel.WARN, "外导数据【" + id + "】失败:" + message);
                }
                result.IsSuccess = n > 0;
                result.Message   = message;
                result.Result    = "";
                if (errorList != null && errorList.Count > 0)
                {
                    result.Result = string.Join("\r\n", errorList);
                }
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message   = ex.Message;
                BLog.Write(BLog.LogLevel.WARN, "外导数据【" + id + "】出错:" + ex.ToString());
            }

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
예제 #2
0
        /// <summary>
        /// 样表
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        private ActionResult ImportDownload(BF_IMPORT.Entity entity)
        {
            try
            {
                List <string>            commentList = new List <string>();
                DataTable                dt          = BF_IMPORT.GetSampleTable(entity, out commentList);
                string                   filename    = HttpUtility.UrlEncode(string.Format("{0}_{1}.xlsx", entity.NAME, DateTime.Now.ToString("yyyyMMddHHmmss")), Encoding.UTF8);
                string                   path        = System.Web.HttpContext.Current.Server.MapPath("~/tmp/");
                Library.Export.ExcelFile export      = new Library.Export.ExcelFile(path);
                string                   fullName    = export.ToExcel(dt);
                if (string.IsNullOrWhiteSpace(fullName) == true || System.IO.File.Exists(fullName) == false)
                {
                    return(ShowAlert("未生成Excel文件"));
                }

                IWorkbook workBook;
                using (FileStream fs = System.IO.File.OpenRead(fullName))
                {
                    workBook = new XSSFWorkbook(fs);
                    ISheet sheet = workBook.GetSheetAt(0);
                    IRow   row   = sheet.GetRow(0);

                    XSSFDrawing patriarch = (XSSFDrawing)sheet.CreateDrawingPatriarch();
                    for (int c = 0; c < row.Cells.Count; c++)
                    {
                        if (string.IsNullOrWhiteSpace(commentList[c]) == false)
                        {
                            IComment comment = patriarch.CreateCellComment(new XSSFClientAnchor(0, 50, 0, 50, c, 0, c + 3, 5));
                            comment.Author           = "编辑提示";
                            comment.String           = new XSSFRichTextString("【编辑提示】\r\n" + commentList[c]);
                            row.Cells[c].CellComment = comment;
                        }
                    }
                }

                //重写文件
                using (FileStream fs = System.IO.File.Create(fullName))
                {
                    workBook.Write(fs);
                    fs.Close();
                }

                System.Web.HttpContext.Current.Response.Buffer = true;
                System.Web.HttpContext.Current.Response.Clear();//清除缓冲区所有内容
                System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
                System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
                System.Web.HttpContext.Current.Response.WriteFile(fullName);
                System.Web.HttpContext.Current.Response.Flush();
                System.Web.HttpContext.Current.Response.End();
                //删除文件
                export.Delete(fullName);
            }
            catch (Exception ex)
            {
                ShowAlert("生成样表出错:" + ex.Message);
            }
            return(View());
        }