示例#1
0
        private void load_rpt_FILE(String reportName, String storeName, Dictionary<string, string> paramerter,
            Dictionary<string, string> parameterReport = null
            , String hasValue = "false", String title = "Tiêu đề", String isPrintTemp = "False",
            String isExportExcel = "False")
        {
            //ReportDB.exportDataToExcel("ABC", null, "OIUYTR");
            //return;

            AssetMangement.GenerateData.ConnectData sqlData = new AssetMangement.GenerateData.ConnectData();
            if (hasValue == "True")
            {
                foreach (KeyValuePair<string, string> pair in paramerter)
                {
                    sqlData.Paramerters.Add("@" + pair.Key);
                    sqlData.ParametersType.Add(SqlDbType.VarChar);
                    sqlData.ParamertersValue.Add(pair.Value);
                }
            }

            // add paramerters cho store proc
            rpds_demo = new ReportDataSet();
            bool isLoad = sqlData.Read_Store(storeName, true);
            if (isExportExcel == "True" || isExportExcel == "true")
            {
                // HyperLink1.NavigateUrl = HttpContext.Current.Request.PhysicalApplicationPath + "ReportPages/";
                // return;

                string tieude = Request["TitleName"];

                DataTable dt = new DataTable();
                string reportNameWithoutExtension = reportName.Substring(0, reportName.IndexOf('.'));
                dt = sqlData.DataSource;
                Dictionary<string, string> listNewColumnName = ReportDB.ReadXML(reportNameWithoutExtension);
                if (listNewColumnName == null)
                {
                    Label1.Text = "Lỗi đọc XML";
                    return;
                }
                foreach (KeyValuePair<string, string> pair in listNewColumnName)
                {
                    //   values += "&" + pair.Key + "=" + pair.Value;
                    // check name load from xml
                    if (dt.Columns.Contains(pair.Key) && !dt.Columns.Contains(pair.Value))
                        dt.Columns[pair.Key].ColumnName = pair.Value;
                }
                //         string path =  ReportDB.SaveAs(reportNameWithoutExtension);
                string path = HttpContext.Current.Request.PhysicalApplicationPath + "ReportPages/" + reportNameWithoutExtension + ".xls";
                bool isComplete = ReportDB.exportDataToExcel(tieude, dt, path);
                Label1.Text = "Xuất file excel cho report " + tieude;
                if (isComplete)
                {
                    Label1.Text += " thành công ! ";// + "<a id=\"linkdownload\" href=\"" + path + "\">Tải file</a>";
                    HyperLink1.NavigateUrl = "../" + reportNameWithoutExtension + ".xls";
                    HyperLink1.Text = "Tải file";

                    // System.Diagnostics.Process.Start(path);                       

                }
                else
                    Label1.Text += " thất bại !";

                // Label1.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + "reportName" + ".xls";                

                return;
            }
            try
            {
                if (isLoad)
                {
                    try
                    {
                        DataTable dt = sqlData.DataSource;
                        if (isPrintTemp.Equals("True"))
                            dt = getDataPrintTemp(dt, parameterReport.First().Value);
                        dt.TableName = "Newrpt";
                        rpds_demo.Tables.Add(dt);
                    }
                    catch (Exception ex)
                    {
                        Label1.Text = ex.Message;
                    }
                }
                else
                {
                    Label1.Text = "Không kết nối được";
                }
            }
            catch (Exception ex)
            {
                Label1.Text = "Lỗi data: " + ex.Message;
                return;
            }
            finally
            {
                GC.Collect();
            }
            try
            {
                string reportPath = Server.MapPath("/Reports/Management/" + reportName);
                cryRpt = new ReportDocument();
                cryRpt.Load(reportPath);
                cryRpt.SummaryInfo.ReportTitle = title;
                cryRpt.SetDataSource(rpds_demo.Tables["Newrpt"]);
                if (parameterReport.Count > 0)
                {
                    foreach (KeyValuePair<string, string> pair in parameterReport)
                    {
                        ParameterDiscreteValue pa = new ParameterDiscreteValue();
                        pa.Value = pair.Value;
                        ParameterFieldDefinition crParameterFieldDefinition = cryRpt.DataDefinition.ParameterFields[pair.Key];
                        crParameterFieldDefinition.CurrentValues.Clear();
                        crParameterFieldDefinition.CurrentValues.Add(pa);
                        crParameterFieldDefinition.ApplyCurrentValues(crParameterFieldDefinition.CurrentValues);
                    }

                }

                //cryRpt.PrintOptions.PrinterName = "Fax";
                //cryRpt.PrintToPrinter(1, false, 0, 0);
                //CrystalReportViewer.ReportSource = cryRpt;

                //System.Drawing.Printing.PrintDocument pDoc = new System.Drawing.Printing.PrintDocument();
                // CrystalDecisions.Shared.PrintLayoutSettings PrintLayout = new CrystalDecisions.Shared.PrintLayoutSettings();
                //System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
                // printerSettings.PrinterName = "Default printer";
                // don't use this, use the new button
                //PrintLayout.Scaling = PrintLayoutSettings.PrintScaling.DoNotScale;

                //System.Drawing.Printing.PageSettings pSettings = new System.Drawing.Printing.PageSettings(printerSettings);
                //rpt.PrintOptions.DissociatePageSizeAndPrinterPaperSize = false;
                //cryRpt.PrintOptions.PrinterDuplex = PrinterDuplex.Simplex;
                //System.Drawing.Printing.PageSettings pageSettings = new System.Drawing.Printing.PageSettings(printerSettings);
                //if (cryRpt.DefaultPageSettings.PaperSize.Height > cryRpt.DefaultPageSettings.PaperSize.Width)
                //{
                //  cryRpt.PrintOptions.DissociatePageSizeAndPrinterPaperSize = true;
                // rptClientDoc.PrintOutputController.ModifyPaperOrientation(CrPaperOrientationEnum.crPaperOrientationPortrait);
                //}
                //else
                //{
                //    cryRpt.PrintOptions.DissociatePageSizeAndPrinterPaperSize = true;
                //    //rptClientDoc.PrintOutputController.ModifyPaperOrientation(CrPaperOrientationEnum.crPaperOrientationLandscape);
                //}
                //cryRpt.PrintToPrinter(printerSettings, pageSettings, false, PrintLayout);

                CrystalReportViewer.ReportSource = cryRpt;

            }
            catch (Exception exr)
            {
                Label1.Text = "Lỗi parameter Report: " + exr.Message;
                return;
            }
            finally
            {
                GC.Collect();
            }

            //  ReportDB.exportDataToExcel("demo", sqlData.DataSource);

        }
示例#2
0
        private void ReportMultiesTable()
        {
            try
            {
                Dictionary<string, Dictionary<string, string>> LstValues = new Dictionary<string, Dictionary<string, string>>();
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                Dictionary<string, string> ParametersReport = new Dictionary<string, string>();
                string key_compare = "pfrp_";
                string storeKey = "_stna_";
                int _index = 0;
                string storeName ="";
                string _keyParam = "";
                foreach (String key in Request.QueryString.AllKeys)
                {
                    if (key != "ReportName" && key != "StoreName" && key != "IsMuiltiesStore"
                        && key != "HasValue" && key != "TitleName" && key != "IsPrintTemp"
                        && key != "IsExportExcel" && key != "PathExport" && key != "IsExportExcelOld" && key != "IsExportNew" && key != "TypeExport"
                        )
                    {
                        //Lenght Key > 5 char                        
                        if (key.Substring(0, key_compare.Length).Equals(key_compare))
                            ParametersReport.Add(key.Substring(
                                key_compare.Length
                                , key.Length - key_compare.Length), Request[key]);
                        else
                        {
                            _index = key.IndexOf(storeKey);
                            storeName = key.Substring(0,_index);
                            _keyParam = key.Substring(_index + 6, key.Length - _index - 6);
                            if (!LstValues.ContainsKey(storeName))
                                LstValues.Add(storeName, new Dictionary<string, string>());
                            LstValues[storeName].Add(_keyParam, Request[key]);                            
                        }                        
                    }
                }

                //Goi load report
                AssetMangement.GenerateData.ConnectData sqlData = new AssetMangement.GenerateData.ConnectData();
                rpds_demo = new ReportDataSet();
                try
                {
                    foreach (var store_name in LstValues.Keys)
                    {
                        sqlData.Paramerters = new List<string>();
                        sqlData.ParametersType = new List<SqlDbType>();
                        sqlData.ParamertersValue = new List<string>();

                        if (Request["HasValue"] == "True")
                        {
                            foreach (KeyValuePair<string, string> pair in LstValues[store_name])
                            {
                                sqlData.Paramerters.Add("@" + pair.Key);
                                sqlData.ParametersType.Add(SqlDbType.VarChar);
                                sqlData.ParamertersValue.Add(pair.Value);
                            }
                        }

                        //add paramerters cho store proc
                        //rpds_demo = new ReportDataSet();
                        bool isLoad = sqlData.Read_Store(store_name, true);
                        if (isLoad)
                        {
                            try
                            {
                                DataTable dt = sqlData.DataSource;
                                dt.TableName = storeKey + store_name;
                                rpds_demo.Tables.Add(dt);
                            }
                            catch (Exception ex)
                            {
                                Label1.Text = ex.Message;
                            }
                        }
                        else
                        {
                            Label1.Text = "Không kết nối được";
                        }
                    }

                }
                catch (Exception ex)
                {
                    Label1.Text = "Lỗi data: " + ex.Message;
                    return;
                }
                finally
                {
                    GC.Collect();
                }

                try
                {
                    string reportPath = Server.MapPath("/Reports/Management/" + Request["ReportName"]);
                    cryRpt = new ReportDocument();
                    cryRpt.Load(reportPath);
                    cryRpt.SummaryInfo.ReportTitle = Request["TitleName"];
                    foreach (var keyStoreName in LstValues.Keys)
                    {
                        //reportDocument.OpenSubreport("rptSubReport1.rpt").SetDataSource(dt1);
                        // reportDocument.OpenSubreport("rptSubReport2JNR.rpt").SetDataSource(dt2);
                        cryRpt.Database.Tables[keyStoreName].SetDataSource(rpds_demo.Tables[storeKey + keyStoreName]);
                    }

                    if (ParametersReport.Count > 0)
                    {
                        foreach (KeyValuePair<string, string> pair in ParametersReport)
                        {
                            ParameterDiscreteValue pa = new ParameterDiscreteValue();
                            pa.Value = pair.Value;
                            ParameterFieldDefinition crParameterFieldDefinition = cryRpt.DataDefinition.ParameterFields[pair.Key];
                            crParameterFieldDefinition.CurrentValues.Clear();
                            crParameterFieldDefinition.CurrentValues.Add(pa);
                            crParameterFieldDefinition.ApplyCurrentValues(crParameterFieldDefinition.CurrentValues);
                        }

                    }

                    CrystalReportViewer.ReportSource = cryRpt;

                }
                catch (Exception exr)
                {
                    Label1.Text = "Lỗi parameter Report: " + exr.Message;
                    return;
                }
                finally
                {
                    GC.Collect();
                }
            }
            catch (Exception ex)
            {
                Label1.Text = "Lỗi:" + ex.Message;
                return;
            }
        }