コード例 #1
0
        /// <summary>
        ///  创建Excel文件
        /// </summary>
        /// <param name="htmlCode"></param>
        /// <param name="sheetName"></param>
        /// <returns>生成文件的路径</returns>
        public string CreateExcel(string htmlCode, string sheetName, string fileName)
        {
            PageJson result       = new PageJson();
            string   fullFileName = string.Empty;

            try
            {
                if (string.IsNullOrEmpty(sheetName))
                {
                    sheetName = "sheet1";
                }
                htmlCode  = Server.UrlDecode(htmlCode);
                sheetName = Server.UrlDecode(sheetName);
                fileName  = Server.UrlDecode(fileName);
                ExcelWriter myExcel = new ExcelWriter(sheetName);
                myExcel.WriteData(htmlCode);
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tempFiles");
                fullFileName = myExcel.SaveAsFile(path, fileName);
            }
            catch
            {
                result.Success = false;
                result.Message = "未知原因导致生成表格失败!";
                return(result.ToJsonString());
            }

            result.Success = true;
            result.Message = fullFileName;
            string str = result.ToJsonString();

            str = Regex.Replace(str, @"\\", "/");

            //删除临时生成的文件
            ThreadStart deleTempFile = () =>
            {
                Thread.Sleep(1000 * 30);
                System.IO.File.Delete(fullFileName);
            };
            Thread newThread = new Thread(deleTempFile);

            newThread.Start();

            return(str);
        }
コード例 #2
0
        public string AddSaleInfo()
        {
            Response.ContentType = "text/html; charset=utf-8;";
            var    json           = new PageJson(false);
            var    excelId        = PageReq.GetForm("excelId");
            var    title          = PageReq.GetForm("title");
            var    type           = PageReq.GetForm("type");
            var    date           = PageReq.GetForm("date");
            string clientFilePath = PageReq.GetForm("filePath");

            var result = new InvokeResult()
            {
                Status = Status.Failed
            };
            var    allExelList    = dataOp.FindAll("LuxuriousHouse_ImportExcelFile").ToList();
            var    curMapObj      = allExelList.Where(c => c.String("excelId") == excelId).FirstOrDefault();
            string serverFilePath = string.Empty;


            var  hasExist = allExelList.Where(c => c.String("excelId") != excelId && c.String("title") == title).Count() > 0;
            bool isSave   = !hasExist;

            if (!isSave)
            {
                json.Message = "名字重复已存在!";
                return(json.ToJsonString());
            }
            else
            {
                if (Request.Files.Count != 0)
                {
                    try
                    {
                        #region 保存文件到服务器
                        HttpPostedFileBase file      = Request.Files[0];
                        string             fileName  = "Luxurious_" + title + Path.GetExtension(file.FileName);
                        string             directory = @"\Content\ExcelFile\";
                        string             filePath  = Server.MapPath(directory);
                        if (!Directory.Exists(filePath))
                        {
                            Directory.CreateDirectory(filePath);
                        }
                        serverFilePath = Path.Combine(directory, fileName);
                        string fullFilePath = Path.Combine(filePath, fileName);
                        if (!CheckFileExt(fullFilePath, "doc"))
                        {
                            json.Message = "文件格式不正确!";
                            return(json.ToJsonString());
                        }
                        file.SaveAs(fullFilePath);

                        curMapObj = new BsonDocument();
                        curMapObj.Add("title", title);
                        curMapObj.Add("type", type);
                        curMapObj.Add("date", date);
                        if (clientFilePath != "")
                        {
                            curMapObj.Add("clientPath", clientFilePath);
                        }
                        if (serverFilePath != "")
                        {
                            curMapObj.Add("serverPath", serverFilePath);
                        }
                        result = dataOp.Insert("LuxuriousHouse_ImportExcelFile", curMapObj);

                        #endregion

                        //写入数据
                        DataTable dt = GetDataTable(fullFilePath);
                        WriteLuxuryData(dt, result.BsonInfo.String("excelId"), "increment");
                    }
                    catch (Exception ex)
                    {
                        json.Message = ex.Message;
                        return(json.ToJsonString());
                    }
                }
                else
                {
                    var updateBson = new BsonDocument();
                    updateBson.Add("title", title);
                    updateBson.Add("type", type);
                    updateBson.Add("date", date);
                    result = dataOp.Update(curMapObj, updateBson);
                }
                json.Success = true;
                json.Message = "保存成功";
            }
            return(json.ToJsonString());
        }