public HttpResponseMessage GetExcelFile(string filename, string id, string lang) { using (var ctx = new RT2020Entities()) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest); // 如果 RT2020 call 會有 VWGContext,RT2020.Api call 就冇 VWGContext,咁就要用 AppSettings var directory = VWGContext.Current != null? VWGContext.Current.Config.GetDirectory("Reports") : ConfigHelper.ReportsBox; using (new Impersonate(directory, ConfigHelper.Impersonate_UserName, ConfigHelper.Impersonate_UserPassword)) { // 喺 ReportsBox directory path 內搵出隻 excel template file var excelFile = Directory.GetFiles(ConfigHelper.ReportsBox, "*.xlsx", SearchOption.AllDirectories) .Where(x => Path.GetFileNameWithoutExtension(x) == filename) .FirstOrDefault(); if (excelFile != null) { if (File.Exists(excelFile)) { // 讀入 dbo.Pipeline 記低嘅 user 要嘅 options var item = PipelineEx.Get(string.Format("PipelineId = '{0}'", id)); if (item != null) { #region 問 RT2020.Reports.dll 攞隻 Excel data source 送返俾 caller var raw = Helper.SA1340Helper.GenExcelDataSource(filename, item.Content, lang); if (raw != null) { var contentLength = raw.Length; //var mimeXls = "application/vnd.ms-excel"; var mimeXlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; var statuscode = HttpStatusCode.OK; // 200 successful response = Request.CreateResponse(statuscode); response.Content = new StreamContent(new MemoryStream(raw)); response.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeXlsx); response.Content.Headers.ContentLength = contentLength; ContentDispositionHeaderValue contentDisposition = null; if (ContentDispositionHeaderValue.TryParse("inline; filename=" + filename + ".xlsx", out contentDisposition)) { response.Content.Headers.ContentDisposition = contentDisposition; } } else { var message = String.Format("Unable to find resource. Resource \"{0}\" may not exist.", filename); HttpError err = new HttpError(message); response = Request.CreateErrorResponse(HttpStatusCode.NotFound, err); } #endregion } } } } return(response); } }
public HttpResponseMessage GetCsvFile(string filename, string id, string lang) { using (var ctx = new RT2020Entities()) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest); // 讀入 dbo.Pipeline 記低嘅 user 要嘅 options var item = PipelineEx.Get(string.Format("PipelineId = '{0}'", id)); if (item != null) { #region 問 RT2020.Reports.dll 攞隻 Csv data source 送返俾 caller var raw = Helper.SA1340Helper.GenCsvDataSource(filename, item.Content, lang); if (raw != "") { var content = Encoding.UTF8.GetBytes(raw); //! 將原本嘅 string 轉為 UTF8 byte[] var contentLength = content.Length; var mimeCsv = "text/csv"; var statuscode = HttpStatusCode.OK; // 200 successful response = Request.CreateResponse(statuscode); response.Content = new ByteArrayContent(Encoding.UTF8.GetPreamble().Concat(content).ToArray()); //! 加上 UTF8 Preamble response.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeCsv); response.Content.Headers.ContentLength = contentLength; ContentDispositionHeaderValue contentDisposition = null; if (ContentDispositionHeaderValue.TryParse("inline; filename=" + filename + ".csv", out contentDisposition)) { response.Content.Headers.ContentDisposition = contentDisposition; } } else { var message = String.Format("Unable to find resource. Resource \"{0}\" may not exist.", filename); HttpError err = new HttpError(message); response = Request.CreateErrorResponse(HttpStatusCode.NotFound, err); } #endregion } return(response); } }
private void OnCommandClick(object sender, EventArgs e) { if (!IsSelValid()) { return; } var cmd = (Button)sender; switch (cmd.Name) { case "cmdPreview": #region gen HTML Preview var id = Guid.NewGuid(); var subject = "SA1340-Summary"; var json = GetSelectionsJson(); var result = PipelineEx.Save(id, subject, json, (int)EnumHelper.Status.Active, DateTime.Now, ConfigHelper.CurrentUserId); if (result) { htmlBox1.Html = RT2020.Reports.Inventory.Journal.Summary.HTML(id.ToString()); } break; #endregion /** suspended * case "cmdPDF": #region export PDF * var pdf = RT2020.Reports.Inventory.Journal.Monthly.PDF(txtFrom.Text.Trim(), txtTo.Text.Trim(), FromDate.ToString("yyyy-MM-dd"), ToDate.ToString("yyyy-MM-dd")); * * if (pdf != null) * { * var dl = new FileDownloadGateway(); * dl.FileName = string.Format("{0}.{1}.pdf", WestwindHelper.GetWord("report.SA1330", "Setting"), DateTime.Now.ToString("yyyyMMddHHmmss")); * dl.SetContentType(DownloadContentType.OctetStream); * dl.StartBytesDownload(this, pdf); * } * break; #endregion * case "cmdExcel": #region export Excel * var xls = RT2020.Reports.Inventory.Journal.Monthly.Excel(txtFrom.Text.Trim(), txtTo.Text.Trim(), FromDate.ToString("yyyy-MM-dd"), ToDate.ToString("yyyy-MM-dd")); * * if (xls != null) * { * var dl = new FileDownloadGateway(); * dl.FileName = string.Format("{0}.{1}.xlsx", WestwindHelper.GetWord("report.SA1330", "Setting"), DateTime.Now.ToString("yyyyMMddHHmmss")); * dl.SetContentType(DownloadContentType.MicrosoftExcel); * dl.StartBytesDownload(this, xls); * } * break; #endregion */ case "cmdPivot": #region export PivotTable var pvt = RT2020.Reports.Inventory.Journal.Summary.Pivot(GetSelections()); if (pvt != null) { var dl = new FileDownloadGateway(); dl.FileName = string.Format("{0}.{1}.xlsx", WestwindHelper.GetWord("report.SA1340", "Setting"), DateTime.Now.ToString("yyyyMMddHHmmss")); dl.SetContentType(DownloadContentType.MicrosoftExcel); dl.StartBytesDownload(this, pvt); } break; #endregion } }