Esempio n. 1
0
        /// <summary>
        /// Update HK-GEMSS.xls file and create HK-GEMSS.xml
        /// </summary>
        /// <param name="gemTradingInfo">the GEM trading News Info.</param>
        public void UpdateGemssFileAndGenerateXMLFile(TradingInfo gemTradingInfo)
        {
            string gemssFilePath = MiscUtil.BackUpFileWithDateFolder(configObj.HKGEMSS_Workbook, false);

            using (ExcelApp app = new ExcelApp(false, false))
            {
                var workbook  = ExcelUtil.CreateOrOpenExcelFile(app, gemssFilePath);
                var worksheet = ExcelUtil.GetWorksheet(configObj.HKGEMSS_Worksheet, workbook);
                if (worksheet == null)
                {
                    Logger.LogErrorAndRaiseException(string.Format("Cannot get worksheet {0} from workbook {1}", configObj.HKGEMSS_Worksheet, workbook.Name));
                }

                using (ExcelLineWriter writer = new ExcelLineWriter(worksheet, 1, 1, ExcelLineWriter.Direction.Down))
                {
                    // Allocate spaces
                    ExcelUtil.InsertBlankRows(ExcelUtil.GetRange("A1", worksheet), 24);
                    writer.PlaceNext(1, 1);

                    // Form 1st line and write
                    string line = string.Empty;
                    writer.WriteLine(gemTradingInfo.DateStr);

                    // Copy 2nd and 3rd line from line 26 and line 27
                    writer.WriteLine(worksheet.Cells[writer.Row + 24, writer.Col]);
                    writer.WriteLine(worksheet.Cells[writer.Row + 24, writer.Col]);

                    if (gemTradingInfo.StockList == null)
                    {
                        writer.WriteLine("NIL");
                        while (writer.Row < 19)
                        {
                            writer.WriteLine(" ");
                        }
                    }
                    else
                    {
                        // Fill stock info
                        foreach (StockInfo stockInfo in gemTradingInfo.StockList)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append(stockInfo.Ric.PadRight(12));
                            sb.Append(stockInfo.StockName.PadRight(27));
                            sb.Append(stockInfo.Shares.PadLeft(15));
                            sb.Append(stockInfo.Turnover.PadLeft(20));
                            line = sb.ToString();
                            writer.WriteLine(line);
                        }
                        while (writer.Row < 19)
                        {
                            writer.WriteLine(" ");
                        }

                        //Fill non-designated securities summary if non-designated securities exist
                        writer.PlaceNext(19, 1);
                        if (!string.IsNullOrEmpty(gemTradingInfo.NonDesignatedSecuritiesRecordingSum) && gemTradingInfo.NonDesignatedSecuritiesRecordingSum != "0")
                        {
                            writer.WriteLine(gemTradingInfo.NonDesignatedSecuritiesRecordingSum.PadLeft(recordAlignrightLength));
                            writer.WriteLine(gemTradingInfo.NonDesignatedSharesShortSoldSum.PadLeft(recordAlignrightLength));
                            writer.WriteLine(gemTradingInfo.NonDesignatedShortSellTransactionSum.PadLeft(recordAlignrightLength));
                        }
                        // Fill designated securities summary
                        writer.WriteLine(gemTradingInfo.DesignatedSecuritiesRecordingSum.PadLeft(recordAlignrightLength));
                        writer.WriteLine(gemTradingInfo.DesignatedSharesShortSoldSum.PadLeft(recordAlignrightLength));
                        writer.WriteLine(gemTradingInfo.DesignatedShortSellTurnoverShares.PadLeft(recordAlignrightLength));
                        writer.WriteLine(gemTradingInfo.DesignatedShortSellTurnoverValue.PadLeft(recordAlignrightLength));
                        if (!string.IsNullOrEmpty(gemTradingInfo.HKDTurnoverValue))
                        {
                            writer.WriteLine(gemTradingInfo.HKDTurnoverValue.PadLeft(recordAlignrightLength));
                        }
                    }

                    for (int page = 1; page <= 25; page++)
                    {
                        string id      = "HK/GEMSS" + page.ToString("D2");
                        int    upRow   = 24 * (page - 1) + 1;
                        int    downRow = upRow + 23;
                        writer.PlaceNextAndWriteLine(upRow, 3, id);
                        writer.PlaceNextAndWriteLine(downRow, 3, id);
                    }

                    //Fill color for C columns
                    Range range = ExcelUtil.GetRange(1, 3, 24, 3, worksheet);
                    range.Interior.Color = ExcelUtil.GetRange(49, 3, worksheet).Interior.Color;

                    ProductionXmlFileTemplate xmlFileTem = GetGemssXmlFileContent(worksheet);
                    ConfigUtil.WriteXml(configObj.HKGEMSS_XmlPath, xmlFileTem);
                    TaskResultList.Add(new TaskResultEntry("XML file for HK-GEMSS", "", configObj.HKGEMSS_XmlPath));


                    if (File.Exists(configObj.HKGEMSS_Workbook))
                    {
                        File.Delete(configObj.HKGEMSS_Workbook);
                    }
                    //Save files as a copy
                    workbook.SaveCopyAs(configObj.HKGEMSS_Workbook);
                    TaskResultList.Add(new TaskResultEntry("HK-GEMSS", "", configObj.HKGEMSS_Workbook));
                    //Close current opend excel file
                    workbook.Close(false, gemssFilePath, false);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Get GEM trading News Info.
        /// </summary>
        /// <param name="uri">the uri of the source data which is a html page.</param>
        /// <returns>a instance of the Class TradingInfo which is about the GEM trading News Info.</returns>
        public TradingInfo GetGemTradingInfo(string uri)
        {
            HtmlAgilityPack.HtmlDocument htmlDoc = null;
            htmlDoc = WebClientUtil.GetHtmlDocument(uri, 180000);

            TradingInfo gemTradingInfo = new TradingInfo();

            gemTradingInfo.StockList = new List <StockInfo>();
            List <string> valueList = new List <string>();
            string        dateStr   = DateTime.Now.ToString("dddd dd/MM/yyyy");

            gemTradingInfo.DateStr = string.Format("Recorded as of {0} 04:00 pm :-", dateStr);

            //Get the trading news information
            string gemTradingNewsInfo = htmlDoc.DocumentNode.SelectSingleNode("//body/pre/font").InnerText;

            //Parse and get the required information
            if (!IsNewsExist(gemTradingNewsInfo))
            {
                gemTradingInfo.StockList = null;
            }

            using (StringReader sr = new StringReader(gemTradingNewsInfo))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] frags = line.Split(new string[] { "  " }, StringSplitOptions.RemoveEmptyEntries);
                    int      ric   = 0;
                    if (frags.Length == 4 && int.TryParse(frags[0].Trim(), out ric))
                    {
                        StockInfo stockInfo = new StockInfo();
                        stockInfo.Ric       = string.Format("<{0}.HK>", ric.ToString("D4"));
                        stockInfo.StockName = frags[1].Trim();
                        stockInfo.Shares    = frags[2].Trim();
                        stockInfo.Turnover  = frags[3].Trim();
                        gemTradingInfo.StockList.Add(stockInfo);
                        continue;
                    }

                    frags = line.Split(':');
                    if (frags.Length == 2 && frags[1].Trim() != "")
                    {
                        valueList.Add(line.Trim());
                    }
                }
            }

            //Parse to get the summary info of Main Board
            if (valueList != null && valueList.Count > 0)
            {
                gemTradingInfo.DesignatedSecuritiesRecordingSum  = valueList[1];
                gemTradingInfo.DesignatedSharesShortSoldSum      = valueList[2];
                gemTradingInfo.DesignatedShortSellTurnoverShares = valueList[3];
                gemTradingInfo.DesignatedShortSellTurnoverValue  = valueList[4];
                if (valueList[5].Contains("Short Selling Turnover Total Value ($)          : HKD  "))
                {
                    gemTradingInfo.HKDTurnoverValue = valueList[5];
                }
            }
            return(gemTradingInfo);
        }