Esempio n. 1
0
        protected void lbDownload_Click(object sender, EventArgs e)
        {
            DataTable dt = Extenders.ObjectToDataTable(this.coverpages[0]);

            ReportHeader reportHeader = ReportHeader.getReportHeder(this.jobSample);


            ReportParameterCollection reportParameters = new ReportParameterCollection();

            reportParameters.Add(new ReportParameter("CustomerPoNo", reportHeader.cusRefNo));
            reportParameters.Add(new ReportParameter("AlsThailandRefNo", reportHeader.alsRefNo));
            reportParameters.Add(new ReportParameter("Date", reportHeader.cur_date.ToString("dd MMMM yyyy") + ""));
            reportParameters.Add(new ReportParameter("Company", reportHeader.addr1));
            reportParameters.Add(new ReportParameter("Company_addr", reportHeader.addr2));
            reportParameters.Add(new ReportParameter("DateSampleReceived", reportHeader.dateOfDampleRecieve.ToString("dd MMMM yyyy") + ""));
            reportParameters.Add(new ReportParameter("DateAnalyzed", reportHeader.dateOfAnalyze.ToString("dd MMMM yyyy") + ""));
            reportParameters.Add(new ReportParameter("DateTestCompleted", reportHeader.dateOfTestComplete.ToString("dd MMMM yyyy") + ""));
            reportParameters.Add(new ReportParameter("SampleDescription", reportHeader.description));
            reportParameters.Add(new ReportParameter("Test", "-"));
            reportParameters.Add(new ReportParameter("ResultDesc", lbResultDesc.Text));
            reportParameters.Add(new ReportParameter("AlsSingaporeRefNo", (String.IsNullOrEmpty(this.jobSample.singapore_ref_no) ? String.Empty : this.jobSample.singapore_ref_no)));

            // Variables
            Warning[] warnings;
            string[]  streamIds;
            string    mimeType  = string.Empty;
            string    encoding  = string.Empty;
            string    extension = string.Empty;



            List <template_seagate_copperwire_img> dat    = new List <template_seagate_copperwire_img>();
            template_seagate_copperwire_img        tmp    = new template_seagate_copperwire_img();
            template_seagate_copperwire_img        corImg = this.refImg.Where(x => x.img_type.Value == 1).FirstOrDefault();

            if (corImg != null)
            {
                tmp.img1 = CustomUtils.GetBytesFromImage(corImg.path_img1);
            }
            corImg = this.refImg.Where(x => x.img_type.Value == 2).FirstOrDefault();
            if (corImg != null)
            {
                tmp.img2 = CustomUtils.GetBytesFromImage(corImg.path_img1);
            }
            dat.Add(tmp);
            // Setup the report viewer object and get the array of bytes
            ReportViewer viewer = new ReportViewer();

            viewer.ProcessingMode         = ProcessingMode.Local;
            viewer.LocalReport.ReportPath = Server.MapPath("~/ReportObject/copperwire_seagate.rdlc");
            viewer.LocalReport.SetParameters(reportParameters);
            viewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));                // Add datasource here
            viewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", dt));                // Add datasource here
            viewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet3", dat.ToDataTable())); // Add datasource here



            string download = String.Empty;

            StatusEnum status = (StatusEnum)Enum.Parse(typeof(StatusEnum), this.jobSample.job_status.ToString(), true);

            switch (status)
            {
            case StatusEnum.ADMIN_CONVERT_WORD:
                if (!String.IsNullOrEmpty(this.jobSample.path_word))
                {
                    Response.Redirect(String.Format("{0}{1}", Configurations.HOST, this.jobSample.path_word));
                }
                else
                {
                    byte[] bytes = viewer.LocalReport.Render("Word", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                    if (!Directory.Exists(Server.MapPath("~/Report/")))
                    {
                        Directory.CreateDirectory(Server.MapPath("~/Report/"));
                    }
                    using (FileStream fs = File.Create(Server.MapPath("~/Report/") + this.jobSample.job_number + "_orginal." + extension))
                    {
                        fs.Write(bytes, 0, bytes.Length);
                    }



                    #region "Insert Footer & Header from template"
                    Document doc1 = new Document();
                    doc1.LoadFromFile(Server.MapPath("~/template/") + "Blank Letter Head - EL.doc");
                    HeaderFooter header = doc1.Sections[0].HeadersFooters.Header;
                    HeaderFooter footer = doc1.Sections[0].HeadersFooters.Footer;
                    Document     doc2   = new Document(Server.MapPath("~/Report/") + this.jobSample.job_number + "_orginal." + extension);
                    foreach (Section section in doc2.Sections)
                    {
                        foreach (DocumentObject obj in header.ChildObjects)
                        {
                            section.HeadersFooters.Header.ChildObjects.Add(obj.Clone());
                        }
                        foreach (DocumentObject obj in footer.ChildObjects)
                        {
                            section.HeadersFooters.Footer.ChildObjects.Add(obj.Clone());
                        }
                    }



                    doc2.SaveToFile(Server.MapPath("~/Report/") + this.jobSample.job_number + "." + extension);
                    #endregion
                    Response.ContentType = mimeType;
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + this.jobSample.job_number + "." + extension);
                    Response.WriteFile(Server.MapPath("~/Report/" + this.jobSample.job_number + "." + extension));
                    Response.Flush();

                    #region "Delete After Download"
                    String deleteFile1 = Server.MapPath("~/Report/") + this.jobSample.job_number + "." + extension;
                    String deleteFile2 = Server.MapPath("~/Report/") + this.jobSample.job_number + "_orginal." + extension;

                    if (File.Exists(deleteFile1))
                    {
                        File.Delete(deleteFile1);
                    }
                    if (File.Exists(deleteFile2))
                    {
                        File.Delete(deleteFile2);
                    }
                    #endregion
                }
                break;

            case StatusEnum.LABMANAGER_CHECKING:
            case StatusEnum.LABMANAGER_APPROVE:
            case StatusEnum.LABMANAGER_DISAPPROVE:
                if (!String.IsNullOrEmpty(this.jobSample.path_word))
                {
                    Response.Redirect(String.Format("{0}{1}", Configurations.HOST, this.jobSample.path_word));
                }
                break;

            case StatusEnum.ADMIN_CONVERT_PDF:
                if (!String.IsNullOrEmpty(this.jobSample.path_word))
                {
                    Response.Redirect(String.Format("{0}{1}", Configurations.HOST, this.jobSample.path_word));
                }

                //if (!String.IsNullOrEmpty(this.jobSample.path_word))
                //{
                //    Word2Pdf objWorPdf = new Word2Pdf();
                //    objWorPdf.InputLocation = String.Format("{0}{1}", Configurations.PATH_DRIVE, this.jobSample.path_word);
                //    objWorPdf.OutputLocation = String.Format("{0}{1}", Configurations.PATH_DRIVE, this.jobSample.path_word).Replace("doc", "pdf");
                //    try
                //    {
                //        objWorPdf.Word2PdfCOnversion();
                //        Response.Redirect(String.Format("{0}{1}", Configurations.HOST, this.jobSample.path_word).Replace("doc", "pdf"));

                //    }
                //    catch (Exception ex)
                //    {
                //        Console.WriteLine();
                //        Response.Redirect(String.Format("{0}{1}", Configurations.HOST, this.jobSample.path_word));

                //    }
                //}
                break;
            }
        }