Example #1
0
        public ActionResult DownSumRain(string sd, string ed)
        {
            DateTime dtStart;
            DateTime dtEnd;

            sd = sd.Replace(' ', '-');
            ed = ed.Replace(' ', '-');

            if (string.IsNullOrEmpty(sd) || string.IsNullOrEmpty(ed))
            {
                dtStart = DateTime.Now;
                dtEnd   = DateTime.Now;
            }
            else
            {
                string[] aSdt = sd.Split('-');
                string[] aEdt = ed.Split('-');

                dtStart = new DateTime(Convert.ToInt32(aSdt[0]), Convert.ToInt32(aSdt[1]), Convert.ToInt32(aSdt[2]), Convert.ToInt32(aSdt[3]), 0, 1);
                dtEnd   = new DateTime(Convert.ToInt32(aEdt[0]), Convert.ToInt32(aEdt[1]), Convert.ToInt32(aEdt[2]), Convert.ToInt32(aEdt[3]), 59, 59);
            }

            string sStartDate = dtStart.ToString("yyyy-MM-ddTHH:mm:ss");
            string sEndDate   = dtEnd.ToString("yyyy-MM-ddTHH:mm:ss");

            string ssql = @" select STID,STNAME,COUNTY,LAT,LON,sum(CONVERT(float, RAIN)) as RAIN from RainStation where 1=1
                       and RTime between @sd and @ed
                       and datepart(mi,RTime) = 0 and datepart(ss,RTime) = 0
                       group by STID,STNAME,COUNTY,LAT,LON   ";

            //List<RainStation> DataList = new List<RainStation>();
            var DataList = dbDapper.Query(ssql, new { sd = sStartDate, ed = sEndDate });

            //產生檔案路徑
            string sTempPath = Path.Combine(Server.MapPath("~/temp/"), DateTime.Now.ToString("yyyyMMdd"));

            //建立資料夾
            Directory.CreateDirectory(sTempPath);
            string sSaveFilePath = Path.Combine(sTempPath, "SumRainData_" + Guid.NewGuid().ToString() + ".csv");

            DataTable dt = new DataTable();

            dt.Columns.Add("STID");
            dt.Columns.Add("STNAME");
            dt.Columns.Add("COUNTY");
            dt.Columns.Add("LAT");
            dt.Columns.Add("LON");
            dt.Columns.Add("RAIN");

            foreach (var item in DataList)
            {
                DataRow NewRow = dt.NewRow();
                NewRow["STID"]   = item.STID;
                NewRow["STNAME"] = item.STNAME;
                NewRow["COUNTY"] = item.COUNTY;
                NewRow["LAT"]    = item.LAT;
                NewRow["LON"]    = item.LON;
                NewRow["RAIN"]   = item.RAIN;

                dt.Rows.Add(NewRow);
            }

            DataExport de       = new DataExport();
            Boolean    bSuccess = de.ExportBigDataToCsv(sSaveFilePath, dt);

            if (bSuccess)
            {
                string filename = string.Format("SumRainData_{0}_{1}.csv", sd, ed).Replace("-", "");

                //ASP.NET 回應大型檔案的注意事項
                //http://blog.miniasp.com/post/2008/03/11/Caution-about-ASPNET-Response-a-Large-File.aspx


                //***** 下載檔案過大,使用特殊方法 *****
                HttpContext context = System.Web.HttpContext.Current;
                context.Response.TransmitFile(sSaveFilePath);
                context.Response.ContentType = "text/csv";
                context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
                Response.End();
            }

            return(null);
        }
Example #2
0
        public ActionResult DownRainStation(string sd, string ed, string Station)
        {
            DateTime dtStart;
            DateTime dtEnd;

            sd = sd.Replace(' ', '-');
            ed = ed.Replace(' ', '-');

            if (string.IsNullOrEmpty(sd) || string.IsNullOrEmpty(ed))
            {
                dtStart = DateTime.Now;
                dtEnd   = DateTime.Now;
            }
            else
            {
                string[] aSdt = sd.Split('-');
                string[] aEdt = ed.Split('-');

                dtStart = new DateTime(Convert.ToInt32(aSdt[0]), Convert.ToInt32(aSdt[1]), Convert.ToInt32(aSdt[2]), Convert.ToInt32(aSdt[3]), 0, 1);
                dtEnd   = new DateTime(Convert.ToInt32(aEdt[0]), Convert.ToInt32(aEdt[1]), Convert.ToInt32(aEdt[2]), Convert.ToInt32(aEdt[3]), 59, 59);
            }

            string sStartDate = dtStart.ToString("yyyy-MM-ddTHH:mm:ss");
            string sEndDate   = dtEnd.ToString("yyyy-MM-ddTHH:mm:ss");

            string ssql = @" select * from RainStation where 1=1
                      and STID = @STID
                      and RTime between @sd and @ed
                      and datepart(mi,RTime) = 0 and datepart(ss,RTime) = 0
                       order by RTime   ";

            List <RainStation> DataList = new List <RainStation>();

            DataList = dbDapper.Query <RainStation>(ssql, new { STID = Station, sd = sStartDate, ed = sEndDate });

            //產生檔案路徑
            string sTempPath = Path.Combine(Server.MapPath("~/temp/"), DateTime.Now.ToString("yyyyMMdd"));

            //建立資料夾
            Directory.CreateDirectory(sTempPath);
            string sSaveFilePath = Path.Combine(sTempPath, "RainDataByStation_" + Guid.NewGuid().ToString() + ".csv");

            DataTable dt = Utils.ConvertToDataTable <RainStation>(DataList);


            DataExport de       = new DataExport();
            Boolean    bSuccess = de.ExportBigDataToCsv(sSaveFilePath, dt);

            if (bSuccess)
            {
                string filename = string.Format("RainDataByStation_{0}_{1}.csv", sd, ed).Replace("-", "");

                //ASP.NET 回應大型檔案的注意事項
                //http://blog.miniasp.com/post/2008/03/11/Caution-about-ASPNET-Response-a-Large-File.aspx


                //***** 下載檔案過大,使用特殊方法 *****
                HttpContext context = System.Web.HttpContext.Current;
                context.Response.TransmitFile(sSaveFilePath);
                context.Response.ContentType = "text/csv";
                context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
                Response.End();
            }

            return(null);
        }