Пример #1
0
        private static void SelectDynamicReport(ReportArgument rArgument, SearchArgument sArgument, string orderBy, string companyName, string branchName, string foreignKeyName, int id, string UserName)
        {
            // IEnumerable<T> data;
            List <RollBackDistributionCompanyInfo> data;
            int count;

            if (!String.IsNullOrEmpty(foreignKeyName))
            {
                if (sArgument != null)
                {
                    data  = SelectDynamicSearchByFK(foreignKeyName, id, sArgument, orderBy).ToList <RollBackDistributionCompanyInfo>();
                    count = SelectSearchByFKCountCached(foreignKeyName, id, sArgument);
                }
                else
                {
                    data  = SelectDynamicByFK(foreignKeyName, id, orderBy).ToList <RollBackDistributionCompanyInfo>();
                    count = SelectByFKCountCached(foreignKeyName, id);
                }
            }
            else
            {
                if (sArgument != null)
                {
                    data  = SelectSearch(sArgument, orderBy).ToList <RollBackDistributionCompanyInfo>();
                    count = data.Count;
                    //count = SelectSearchCountCached(sArgument);
                }
                else
                {
                    data  = Select(orderBy).ToList <RollBackDistributionCompanyInfo>();
                    count = data.Count;
                    //count = SelectCountCached();
                }
            }
            int n = count;
            int m = rArgument.ReportArguments.Count;

            TypeCode[]     codes = new TypeCode[m];
            PropertyInfo[] infos = new PropertyInfo[m];
            bool           first = true;

            if (rArgument.Format == "PDF")
            {
                PDFCreators creator = new PDFCreators(rArgument.Orientation, rArgument.MarginLeft, rArgument.MarginRight, rArgument.MarginTop, rArgument.MarginBottom);

                creator.SetDocumentHeaderFooter(companyName);
                creator.OpenPDF();
                creator.SetTitle(rArgument.ReportName);
                string[] headers = new string[m];
                for (int i = 0; i < m; i++)
                {
                    headers[i] = rArgument.ReportArguments[i].HeaderText;
                }
                creator.CreateTable(m, true, headers, branchName);

                for (int i = 0; i < n; i++)
                {
                    object[] values = new object[m];
                    for (int j = 0; j < m; j++)
                    {
                        if (first)
                        {
                            infos[j] = typeof(RollBackDistributionCompanyInfo).GetProperty(rArgument.ReportArguments[j].PropertyField);
                            if (IsNullableType(infos[j].PropertyType))
                            {
                                NullableConverter nc = new NullableConverter(infos[j].PropertyType);
                                codes[j] = Type.GetTypeCode(nc.UnderlyingType);
                            }
                            else
                            {
                                codes[j] = Type.GetTypeCode(infos[j].PropertyType);
                            }
                        }
                        values[j] = infos[j].GetValue(data[i], null);
                        if (codes[j] == TypeCode.DateTime)
                        {
                            DateTime date = (DateTime)values[j];
                            values[j] = date.ToShortDateString();
                        }

                        if (codes[j] == TypeCode.Decimal)
                        {
                            decimal dec = (decimal)values[j];
                            values[j] = String.Format("{0:#,0.00}", dec);
                        }

                        if (codes[j] == TypeCode.Boolean)
                        {
                            bool temp = (bool)values[j];
                            if (temp == true)
                            {
                                values[j] = "Да";
                            }
                            else
                            {
                                values[j] = "Не";
                            }
                        }
                    }
                    first = false;
                    if (creator.AddDataRow(values, m, codes))
                    {
                        i--;
                    }
                }
                creator.AddTable();
                creator.FinishPDF();
            }

            //Creating Excel document
            else if (rArgument.Format == "XLS")
            {
                ExcelFileWriter <RollBackDistributionCompanyInfo> myExcel = new ExcelFileWriter <RollBackDistributionCompanyInfo>();
                string[] headers = new string[m];
                for (int i = 0; i < m; i++)
                {
                    headers[i] = rArgument.ReportArguments[i].HeaderText;
                }
                myExcel.Headers = headers;

                int    temp_num    = ('A') + m - 1;
                char   lastCol     = Convert.ToChar(temp_num);
                string finalColumn = lastCol + "1";

                myExcel.ColumnCount = m - 1;
                myExcel.RowCount    = n;

                myExcel.ActivateExcel();
                myExcel.FillHeaderColumn(headers, "A1", finalColumn);

                for (int i = 0; i < n; i++)
                {
                    object[] values = new object[m];

                    for (int j = 0; j < m; j++)
                    {
                        if (first)
                        {
                            infos[j] = typeof(RollBackDistributionCompanyInfo).GetProperty(rArgument.ReportArguments[j].PropertyField);
                            if (IsNullableType(infos[j].PropertyType))
                            {
                                NullableConverter nc = new NullableConverter(infos[j].PropertyType);
                                codes[j] = Type.GetTypeCode(nc.UnderlyingType);
                            }
                            else
                            {
                                codes[j] = Type.GetTypeCode(infos[j].PropertyType);
                            }
                        }
                        values[j] = infos[j].GetValue(data[i], null);

                        if (codes[j] == TypeCode.DateTime)
                        {
                            DateTime date = (DateTime)values[j];
                            values[j] = date.ToShortDateString();
                        }

                        if (codes[j] == TypeCode.Boolean)
                        {
                            bool temp = (bool)values[j];
                            if (temp == true)
                            {
                                values[j] = "Да";
                            }
                            else
                            {
                                values[j] = "Не";
                            }
                        }
                    }
                    first = false;
                    string fColumn = "A" + (i + 2).ToString();
                    string lColumn = lastCol + (i + 2).ToString();
                    myExcel.FillRowData_Mine(values, fColumn, lColumn);
                }
                string fileName = UserName + "_" + rArgument.ReportName + ".xls";
                myExcel.SaveExcel(fileName);
                myExcel.FinishExcel(fileName);
            }

            //Create Word document
            else if (rArgument.Format == "DOC")
            {
                WordFileWriter <RollBackDistributionCompanyInfo> myWord = new WordFileWriter <RollBackDistributionCompanyInfo>();
                myWord.Orientation = rArgument.Orientation;
                myWord.ActivateWord();

                //myWord.InsertingText(rArgument.ReportName);
                //myWord.InsertingText("Датум на извештај: " + DateTime.Now.ToShortDateString());

                string[] headers = new string[m];
                for (int i = 0; i < m; i++)
                {
                    headers[i] = rArgument.ReportArguments[i].HeaderText;
                }
                myWord.Headers           = headers;
                object[,] tempFillValues = new object[n, m];

                CultureInfo oldCI   = System.Threading.Thread.CurrentThread.CurrentCulture;
                CultureInfo oldUICI = System.Threading.Thread.CurrentThread.CurrentUICulture;

                System.Threading.Thread.CurrentThread.CurrentCulture   = new CultureInfo("mk-MK");
                System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("mk-MK");


                for (int i = 0; i < n; i++)
                {
                    object[] values = new object[m];
                    for (int j = 0; j < m; j++)
                    {
                        if (first)
                        {
                            infos[j] = typeof(RollBackDistributionCompanyInfo).GetProperty(rArgument.ReportArguments[j].PropertyField);
                            if (IsNullableType(infos[j].PropertyType))
                            {
                                NullableConverter nc = new NullableConverter(infos[j].PropertyType);
                                codes[j] = Type.GetTypeCode(nc.UnderlyingType);
                            }
                            else
                            {
                                codes[j] = Type.GetTypeCode(infos[j].PropertyType);
                            }
                        }
                        values[j] = infos[j].GetValue(data[i], null);

                        if (codes[j] == TypeCode.DateTime)
                        {
                            DateTime date = (DateTime)values[j];
                            values[j] = date.ToShortDateString();
                        }

                        if (codes[j] == TypeCode.Boolean)
                        {
                            bool temp = (bool)values[j];
                            if (temp == true)
                            {
                                values[j] = "Да";
                            }
                            else
                            {
                                values[j] = "Не";
                            }
                        }
                        tempFillValues[i, j] = values[j];
                    }
                    first = false;
                }

                System.Threading.Thread.CurrentThread.CurrentCulture   = oldCI;
                System.Threading.Thread.CurrentThread.CurrentUICulture = oldUICI;

                myWord.FillValues = tempFillValues;
                myWord.CreateTable(n, m);

                myWord.InsertFooter();
                myWord.InsertHeader(rArgument.BranchName);

                string fileName = UserName + "_" + rArgument.ReportName + ".doc";
                myWord.SaveDOC(fileName);
                myWord.FinishDOC(fileName);
            }
        }