Esempio n. 1
0
        /// <summary>
        /// Reads data from a file with a list of potential customers
        /// </summary>
        /// <param name="fileName">Contains paths and names of incoming files (a list of customers and a black-list)  </param>
        public void ReadTheFile(object fileName)
        {
            string fileNameString;

            fileNameString = ((DataClassString3)fileName).N1;
            Excel.Application ObjWorkExcel = new Excel.Application();
            Excel.Workbook    ObjWorkBook  = ObjWorkExcel.Workbooks.Open(fileNameString);
            Excel.Worksheet   ObjWorkSheet = (Excel.Worksheet)ObjWorkBook.Sheets[1];

            var lastCell = ObjWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell);

            lastColumn = (int)lastCell.Column;
            lastRow    = (int)lastCell.Row;

            for (int i = 1; i <= lastColumn; i++)
            {
                ObjWorkSheet.Columns[i].AutoFit();
            }

            list = new DataClassString3[lastRow - 1];
            for (int i = 0; i < lastRow - 1; i++)
            {
                list[i]    = new DataClassString3();
                list[i].N1 = ObjWorkSheet.Cells[i + 2, 1].Text.ToString();
                list[i].N1 = list[i].N1.Trim();
                list[i].N2 = ObjWorkSheet.Cells[i + 2, 2].Text.ToString();
                list[i].N2 = list[i].N2.Trim();
                list[i].N3 = ObjWorkSheet.Cells[i + 2, 3].Text.ToString();
                list[i].N3 = list[i].N3.Trim();
            }

            ObjWorkBook.Close(false, Type.Missing, Type.Missing);
            ObjWorkExcel.Quit();
            GC.Collect();
        }
        protected void ButStart_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                nameOfFile = new DataClassString3();
                guidID     = Guid.NewGuid().ToString(); //   generating of unique key

                worker1 = new Worker();

                worker1.ListBoxNotUniqEvent     += worker_ListBoxNotUniqEvent;
                worker1.ListBoxNotValidEvent    += worker_ListBoxNotValidEvent;
                worker1.ListBoxInBlackListEvent += worker_ListBoxInBlackListEvent;

                string typeOfFile1 = System.IO.Path.GetExtension(FUpFile.FileName);
                string typeOfFile2 = System.IO.Path.GetExtension(FUpBlack.FileName);
                if ((typeOfFile1 == ".xls" || typeOfFile1 == ".xlsx") && (typeOfFile2 == ".xls" || typeOfFile2 == ".xlsx"))
                {
                    DelegateForWorker startWorkerMethods = worker1.ReadTheFile;
                    startWorkerMethods += worker1.ReadTheBlackList;
                    startWorkerMethods += worker1.ValidationTaxId;
                    startWorkerMethods += worker1.SearchNotUniqTaxId;
                    startWorkerMethods += worker1.SearchTaxIdInBlackList;
                    if (RadioButtonList1.SelectedValue == "2")
                    {
                        startWorkerMethods += worker1.WriteResultsIntoFile;
                    }

                    string path = Server.MapPath("App_Data//");       //  getting absolute path to the folder vith data
                    FUpFile.SaveAs(path + guidID + FUpFile.FileName); //   upload the file to the server
                    FUpBlack.SaveAs(path + guidID + FUpBlack.FileName);

                    nameOfFile.N1 = Server.MapPath(@"App_Data\" + guidID + FUpFile.FileName);         //   saving the absolute path for reading the file in methods of "worker"
                    nameOfFile.N2 = Server.MapPath(@"App_Data\" + guidID + FUpBlack.FileName);
                    nameOfFile.N3 = Server.MapPath(@"App_Data\Result_" + guidID + ".xlsx");

                    startWorkerMethods(nameOfFile);

                    File.Delete(nameOfFile.N1);          //   deleting used files
                    File.Delete(nameOfFile.N2);

                    if (RadioButtonList1.SelectedValue == "2")
                    {
                        Response.ContentType = "application/xlsx";
                        Response.AppendHeader("Content-Disposition", "attachment; filename=results.xlsx");
                        Response.TransmitFile(nameOfFile.N3);
                        Response.End();
                        File.Delete(nameOfFile.N3);
                    }
                }
                else
                {
                    Response.Write("Select the input file and the blacklist");
                }
            }
        }