public static bool MainProc(string search, string keyword, string input) { bool use = false; string title = ""; string titleText = ""; string titleLink = ""; string remark = ""; getReg(search, ref title, ref titleText, ref titleLink, ref remark); //标题 Regex r = new Regex(title, RegexOptions.IgnoreCase); MatchCollection matchCollection = r.Matches(input); //简介 string abstractReg = remark; MatchCollection abstractCollection = Regex.Matches(input, abstractReg, RegexOptions.IgnoreCase); for (int i = 0; i < abstractCollection.Count; i++) { Match m = abstractCollection[i]; string abstractStr = StringHelp.ReplaceHtmlTag(m.Value.Replace("'", "")); if (m.Success && ruleJudge(abstractStr, keyword)) { //标题 string textReg = titleText; MatchCollection textMatchCollection = Regex.Matches(matchCollection[i].Value, textReg, RegexOptions.IgnoreCase); Match match2 = textMatchCollection[0]; string titleStr = ""; if (match2.Success) { titleStr = StringHelp.ReplaceHtmlTag(match2.Result("$1")); } //标题链接 string LinkReg = titleLink; MatchCollection linkMatchCollection = Regex.Matches(matchCollection[i].Value, LinkReg, RegexOptions.IgnoreCase); Match match3 = linkMatchCollection[0]; string titleLinkStr = ""; if (match3.Success) { titleLinkStr = match3.Value.Replace("<a href=", "").Replace("\"", ""); } #region 创建采集对象 TelCollection tel = new TelCollection(); tel.Search = search; tel.Telphone = keyword; tel.Title = titleStr; tel.TitleLink = titleLinkStr; tel.Abstract = abstractStr; #endregion StringHelp.WriteExcel(tel); use = true; break; } } return(use); }
//public static IFont font = newhssfworkbook.CreateFont();//创建字体样式 //public static ICellStyle style = newhssfworkbook.CreateCellStyle();//创建单元格样式 public static void WriteExcel(TelCollection tel) { #region 设置字体 font.Color = NPOI.HSSF.Util.HSSFColor.Blue.Index; //设置字体颜色 style.SetFont(font); //设置单元格样式中的字体样式 #endregion //默认数据行 var newrow = newsheet.CreateRow(newsheet.LastRowNum + 1);//创建新行 //搜索引擎 var newcell0 = newrow.CreateCell(0); newcell0.SetCellValue(tel.Search); #region 号码 var newcell1 = newrow.CreateCell(1); newcell1.SetCellValue(tel.Telphone); //手机号 //设置号码超链接 HSSFHyperlink link = new HSSFHyperlink(HyperlinkType.Url); //建一个HSSFHyperlink实体,指明链接类型为URL(这里是枚举,可以根据需求自行更改) if (tel.Search == "360") { link.Address = Reg360.url + tel.Telphone;//给HSSFHyperlink的地址赋值 } else { link.Address = RegBaidu.url + tel.Telphone; } newcell1.Hyperlink = link; //将链接方式赋值给单元格的Hyperlink即可将链接附加到单元格上 //超链接字体 newcell1.CellStyle = style; //为单元格设置显示样式 #endregion #region 标题 var newcell2 = newrow.CreateCell(2); newcell2.SetCellValue(tel.Title); //标题 //设置标题超链接 HSSFHyperlink titleLink = new HSSFHyperlink(HyperlinkType.Url); //建一个HSSFHyperlink实体,指明链接类型为URL(这里是枚举,可以根据需求自行更改) titleLink.Address = tel.TitleLink; //给HSSFHyperlink的地址赋值 newcell2.Hyperlink = titleLink; //将链接方式赋值给单元格的Hyperlink即可将链接附加到单元格上 //设置字体 newcell2.CellStyle = style; //为单元格设置显示样式 #endregion //简介 var newcell3 = newrow.CreateCell(3); newcell3.SetCellValue(tel.Abstract); Object locker = new Object(); lock (locker) { try { FileStream fs = File.OpenWrite(filePathOut); newhssfworkbook.Write(fs); //向打开的这个xls文件中写入表并保存。 fs.Close(); } catch (Exception ex) { StringHelp.Write(StringHelp.pathError, ex.Message + tel.Telphone + "\r\n"); System.Threading.Thread.Sleep(1 * 1000); WriteExcel(tel); StringHelp.Write(StringHelp.pathError, tel.Telphone + "重新执行\r\n"); } } }