/// <summary> /// 读取证券代码表 /// </summary> /// <returns></returns> public DataTable GetStockCode() { //代码表文件的完整路径 string filePath = DataPath + Market + "\\STKINFO70.DAT"; //判断文件是否存在 this.CheckFile(filePath); if (message == filePath + "文件不存在") { return(null); } //建立存放代码表的Datatable DataTable dtCode = new DataTable(); dtCode.Columns.Add("证券代码", typeof(string)); dtCode.Columns.Add("证券名称", typeof(string)); dtCode.Columns.Add("速记码", typeof(string)); //打开数据文件,建立文件流 FileStream fs = null; BinaryReader br = null; try { fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); br = new BinaryReader(fs); } catch { message = "读取文件" + filePath + "失败"; return(null); } finally { message = "读取文件" + filePath + "成功"; } //存放代码的开始位置 int startAddress = 0x44A6; //每个代码块的大小 int recordSize = 320; //从起始位置开始读,12开始的4个字节为代码表总数 fs.Position = 0xC;// 12; //文件中证券总数 int stockCount = br.ReadInt32(); for (int i = 0; i < stockCount; i++) { //实例化一个证券代码 StockCode sc = new StockCode(); //文件流的位置,开始位置+第几块*每块大小 fs.Position = startAddress + i * recordSize; //证券代码,10个字节,后面是\0,去掉 sc.code = this.Market + System.Text.Encoding.Default.GetString(br.ReadBytes(10)).Replace("\0", ""); if (string.IsNullOrEmpty(sc.code)) { break; } //以下代码有点冗余,有空再改 //判断该代码是否是需要的编码开头的代码 foreach (string s in this.SegularCode.Split(new string[] { "," }, StringSplitOptions.None)) { if (sc.code.Substring(0, s.Length) == s) { //证券名称,32个字节 sc.name = System.Text.Encoding.Default.GetString(br.ReadBytes(32)).Replace("\0", ""); //速记码 sc.sjm = ChinesePinYin.GetChineseSpell(sc.name); //将代码信息保存到表中 DataRow dRow = dtCode.NewRow(); dRow["证券代码"] = sc.code; dRow["证券名称"] = sc.name; dRow["速记码"] = sc.sjm; dtCode.Rows.Add(dRow); } } //判断该代码是否是指定的代码,如指数 foreach (string s in this.SegularCode.Split(new string[] { "," }, StringSplitOptions.None)) { if (sc.code == s) { //证券名称,32个字节 sc.name = System.Text.Encoding.Default.GetString(br.ReadBytes(32)).Replace("\0", ""); //将代码信息保存到表中 DataRow dRow = dtCode.NewRow(); dRow["证券代码"] = sc.code; dRow["证券名称"] = sc.name; dRow["速记码"] = ChinesePinYin.GetChineseSpell(sc.name); dtCode.Rows.Add(dRow); } } } br.Close(); fs.Close(); fs.Dispose(); return(dtCode); }
/// <summary> /// 解析网页源代码,提取证券代码名称(东方财富网证券列表) /// </summary> /// <param name="content"></param> /// <returns></returns> public DataTable RegexStockCode(string content) { if (content.Length == 0) { return(null); } //上证指数、深成指数 string specificCode = Main.staticAppSetup["SpecificCode"].ToString(); //沪深A股、深圳创业板 string segularCode = Main.staticAppSetup["SegularCode"].ToString(); DataTable dt = new DataTable(); //构建显示证券代码的表格 dt.Columns.Add("证券代码", typeof(string)); dt.Columns.Add("证券名称", typeof(string)); dt.Columns.Add("速记码", typeof(string)); string stock; //每行证券信息 string gpsc; //证券市场 string gpdm; string gpmc; //正则获取<li><a target="_blank" href="http://quote.eastmoney.com/sz300223.html">北京君正(300223)</a></li>这种段落 Match m; Regex r = new Regex(@"<li><a target=.*?</a></li>", RegexOptions.IgnoreCase | RegexOptions.Compiled); for (m = r.Match(content); m.Success; m = m.NextMatch()) { //每行证券链接 stock = m.Groups[0].Value.ToString(); //市场 if (stock.IndexOf(@"/sh") > 0) { gpsc = "SH"; } else { gpsc = "SZ"; } //完整行:<li><a target="_blank" href="http://quote.eastmoney.com/sh500001.html">基金金泰(500001)</a></li> //证券名称 //截取html开始到证券代码最后一位之间的字符串 stock = stock.Substring(stock.IndexOf("html")); //从第七位到倒数第七位之间为证券名称 gpmc = stock.Substring(6, stock.Length - 23); //最后六位为证券代码 gpdm = gpsc + stock.Substring(gpmc.Length + 7, 6); //判断该代码是否是需要的编码开头的代码 foreach (string s in segularCode.Split(new string[] { "," }, StringSplitOptions.None)) { if (gpdm.Substring(0, s.Length) == s) { DataRow row = dt.NewRow(); row["证券代码"] = gpdm; row["证券名称"] = gpmc; row["速记码"] = ChinesePinYin.GetChineseSpell(gpmc); dt.Rows.Add(row); } } //判断该代码是否是指定的代码,如指数 foreach (string s in segularCode.Split(new string[] { "," }, StringSplitOptions.None)) { if (gpdm == s) { DataRow row = dt.NewRow(); row["证券代码"] = gpdm; row["证券名称"] = gpmc; row["速记码"] = ChinesePinYin.GetChineseSpell(gpmc); dt.Rows.Add(row); } } } return(dt); }