Example #1
0
        public ActionResult ExceptionDataExportToExcel()
        {
            OperationResult result         = new OperationResult(OperationResultType.Error);
            var             scanValidKey   = "ScanValid";
            var             scanInvalidKey = "ScanInvalid";

            List <Product_Model> validModels   = SessionAccess.Get(scanValidKey) as List <Product_Model>;
            List <Product_Model> inValidModels = SessionAccess.Get(scanInvalidKey) as List <Product_Model>;

            List <List <string> > validDa   = new List <List <string> >();
            List <List <string> > inValidDa = new List <List <string> >();

            if (validModels != null && validModels.Any())
            {
                foreach (var valda in validModels)
                {
                    validDa.Add(new List <string>()
                    {
                        valda.Id.ToString(), valda.ProductBarcode
                    });
                }
            }
            if (inValidModels != null && inValidModels.Any())
            {
                foreach (var valda in inValidModels)
                {
                    inValidDa.Add(new List <string>()
                    {
                        valda.Id.ToString(), valda.ProductBarcode, valda.Notes
                    });
                }
            }
            List <List <List <string> > > li = new List <List <List <string> > >();

            li.Add(validDa);
            li.Add(inValidDa);

            YxkSabri.ExcelUtility excel = new ExcelUtility();
            var    name    = DateTime.Now.ToString("yyyyMMddHHff") + ".xls";
            string basedir = Server.MapPath(@"\Content\UploadFiles\Excels\ExportDa\");

            if (Directory.Exists(basedir))
            {
                Directory.Delete(basedir, true);
            }
            Directory.CreateDirectory(basedir);


            string path = basedir + name;
            var    rsul = excel.ExportMulitExcelSheet(li, path, new string[] { "有效", "无效" });

            if (rsul)
            {
                result = new OperationResult(OperationResultType.Success);
            }

            return(File(path, "application/ms-excel", "导出数据.xls"));


            //// string path = "D:/test/销售月报表.xlsx";
            // FileStream fs = new FileStream(path, FileMode.Open);
            // byte[] buffer = new byte[fs.Length];
            // fs.Read(buffer, 0, buffer.Length);

            // fs.Close();
            //System.IO.File.Delete(path);

            // Response.ContentType = "application/ms-excel";
            // Response.Charset = "GB2312";
            // Response.ContentEncoding = System.Text.Encoding.UTF8;

            // Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("销售月报表.xlsx"));
            // Response.OutputStream.Write(buffer, 0, buffer.Length);
            // Response.Flush();
            // Response.End();


            //  return Json(result);
        }