private void CrawlCurrentPage(WebBrowser wb, bool isOffline, bool IsUnConfirmChecked, ref bool hasValidData) { mshtml.IHTMLDocument2 doc2 = isOffline ? null : (mshtml.IHTMLDocument2)wb.Document; string html = isOffline ? s_htmlFake : doc2.body.innerHTML; Debug.WriteLine(html); List <ImportInvoiceDTO> list = new List <ImportInvoiceDTO>(); List <hParser.Tags.TableRow> validRowList = new List <hParser.Tags.TableRow>(); //this.parseResult = ""; #region 使用IHTMLDocument2提取HTML mshtml.HTMLTableClass table = IsUnConfirmChecked ? (mshtml.HTMLTableClass)doc2.all.item("example1", 0) : (mshtml.HTMLTableClass)doc2.all.item("example", 0); if (table == null) { hasValidData = false; //throw new InvalidOperationException("无效table"); return; } mshtml.HTMLTableSectionClass tbody = (mshtml.HTMLTableSectionClass)table.lastChild; if (tbody == null) { hasValidData = false; //throw new InvalidOperationException("无效tbody"); return; } var tbodyHtml = tbody.innerHTML; if (0 == string.Compare(tbody.innerText, "没找到记录", StringComparison.InvariantCultureIgnoreCase)) { hasValidData = false; //throw new InvalidOperationException("无效tbody"); return; } #region WPF WebBroswer交互源代码DOM元素总结 #if RESEARCH //HTMLDocument doc01 = wb.Document as HTMLDocument; ////IHTMLDocument2 doc02 = wb.Document as IHTMLDocument2; //Debug.WriteLine(doc01.body.innerHTML); ///读/写元素 /// mshtml.IHTMLElement login_pass = (mshtml.IHTMLElement)doc2.all.item("login_pass", 0); mshtml.IHTMLElement password = (mshtml.IHTMLElement)doc2.all.item("password", 0); password.setAttribute("value", "12345678"); login_pass.setAttribute("style", ""); mshtml.IHTMLElement login_pass1 = (mshtml.IHTMLElement)doc2.all.item("login_pass1", 0); mshtml.IHTMLElement password1 = (mshtml.IHTMLElement)doc2.all.item("password1", 0); login_pass1.setAttribute("style", "display:none;"); //password1.setAttribute("style", "width:1px"); //IHTMLElement item = doc01.getElementById("ptmm"); //item.innerHTML = "<INPUT id=\"pwd\" class=\"login_input password\" type=\"text\" value=\"\" />"; //// doc01.body.insertAdjacentHTML(,); //MessageBox.Show(item.innerText); //wb.NavigateToString(doc01.body.innerHTML); /// Trigger event //点击确定按钮 loginBT.click(); /// script injection /// //Basic ds = new Basic(); //wb.ObjectForScripting = ds;//该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问 ///Levarage JS /// mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow; win.execScript("Login('12345678', '', 1)", "javascript"); return; #endif #endregion #endregion #region 使用HtmlParser提取tbodyHtml Lexer lexer = new Lexer(tbodyHtml); hParser.Parser parser = new hParser.Parser(lexer); hParser.NodeFilter filter = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.TableRow)); NodeList nodeList = parser.Parse(filter); if (nodeList.Count == 0) { hasValidData = false; MessageBox.Show("没有符合要求的节点"); } else { for (int i = 0; i < nodeList.Count; i++) { //抓取一行 var tagTR = parserTR(nodeList[i]); #region 充填有效行 if (tagTR != null) { validRowList.Add(tagTR); } #endregion } parserValidTR(validRowList, IsUnConfirmChecked, ref list); } #endregion #region 使用HtmlParser提取HTML /* Lexer lexer = new Lexer(html); * hParser.Parser parser = new hParser.Parser(lexer); * hParser.NodeFilter filter = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.TableRow)); * NodeList nodeList = parser.Parse(filter); * if (nodeList.Count == 0) * MessageBox.Show("没有符合要求的节点"); * else * { * for (int i = 0; i < nodeList.Count; i++) * { * //抓取一行 * var tagTR = parserTR(nodeList[i]); * #region 充填有效行 * if (tagTR != null) * validRowList.Add(tagTR); #endregion * * } * * parserValidTR(validRowList, ref list); * * } */ #endregion #region 日志 & 导出 & 持久化 if (list == null || list.Count == 0) { MessageBox.Show("该页面上没有检测到预期数据"); hasValidData = false; } ImportInvoiceListDTO soap = new ImportInvoiceListDTO { List = list, Result = new ImportInvoiceResultDTO { Message = "CALLBACK", Status = 9 } }; Debug.Write(soap); #region Log if (this.IfLog == "1") { soap.List.ForEach(impinfo => { if (IsUnConfirmChecked) { LogHelper.WriteLog(typeof(WebBoxView), string.Format("发票代码{0} 发票号码{1} 开票日期{2} 销方税号{3} 金额{4} 税额{5} 来源{6} 发票状态{7} 勾选标志{8} 操作时间{9}", impinfo.InvoiceCode, impinfo.InvoiceNumber, impinfo.CreateDate, impinfo.SalesTaxNumber, impinfo.Amount, impinfo.Tax, impinfo.From, impinfo.Status, impinfo.SelectTag, impinfo.ChosenTime)); } else { LogHelper.WriteLog(typeof(WebBoxView), string.Format("发票代码{0} 发票号码{1} 开票日期{2} 销方税号{3} 金额{4} 税额{5} 来源{6} 发票状态{7} 确认月份{8}", impinfo.InvoiceCode, impinfo.InvoiceNumber, impinfo.CreateDate, impinfo.SalesTaxNumber, impinfo.Amount, impinfo.Tax, impinfo.From, impinfo.Status, impinfo.SelectTag)); } }); } #endregion if (this.IfCallWS == "1") { CallWS(soap); } Debug.Write("本页已同步完成,请点击下一页继续同步"); //FakeBusy(); #endregion }
public ImportInvoiceListDTO InjectList(ImportInvoiceListDTO soap) { int probe = 0; string exceptionMsg = ""; using (TaxEntities db = new TaxEntities()) { foreach (var model in soap.List) { VIMS_BIZ_INVOICE entity = null; if (db.VIMS_BIZ_INVOICE.Any(o => o.FPDM == model.InvoiceCode && o.FPHM == model.InvoiceNumber)) {//修改 entity = db.VIMS_BIZ_INVOICE.First(o => o.FPDM == model.InvoiceCode && o.FPHM == model.InvoiceNumber); //db.VIMS_BIZ_INVOICE.Remove(old); entity.DKZT = model.DeductionStatus; // entity.QRBZ = model.IsChosen == "是" ? "2" : "1"; entity.XFMC = model.SalesName; entity.SHRQ = DateTime.Now; } else {//新增 entity = new VIMS_BIZ_INVOICE(); entity.ID = Guid.NewGuid().ToString(); entity.DKZT = model.DeductionStatus; // entity.QRBZ = model.IsChosen == "是" ? "2" : "1"; entity.XFMC = model.SalesName; entity.SHRQ = DateTime.Now; #region 非空冗余 entity.FPLB = "0"; entity.GFSH = model.TaxCode; entity.FPMW = "N/A"; // entity.FPSL = 0.0M;//发票税率(只有机动车销售发票显示税率) entity.QDBZ = "N";//清单标识(当清单标识为Y时发票为汉字防伪发票) #endregion #region Mapping entity.RZJG = "0"; //认证结果(0:认证通过,1:认证未通过,255:待认证) entity.RZZT = "0"; //!string.IsNullOrEmpty(model.CertificateStatus) && string.Compare(model.CertificateStatus, "已确认", StringComparison.InvariantCultureIgnoreCase) == 0 ? "0" : "1"; //认证结果(0:认证通过,1:认证未通过,255:待认证)//认证状态 0:可以认证 1:不能认证 entity.FPDM = model.InvoiceCode; entity.FPHM = model.InvoiceNumber; DateTime d_createDate = DateTime.Now; entity.KPRQ = DateTime.TryParse(model.CreateDate, out d_createDate) ? d_createDate : d_createDate; entity.XFSH = "N/A";//model.SalesTaxNumber;//销方税号(货运为承运人,机动车为纳税人识别号) entity.FPJE = model.Amount; entity.FPSE = model.Tax; //?????? entity.FPLY = "0";//??????发票来源 FPLY(0:手工录入;1:扫描采集;2:接口传入;3:导入;4:录入) / 来源 RZLY(0:系统认证;1:其他认证) //entity.??? = model.SelectTag; //entity.RZSJ = model.OperationTime; #endregion entity = db.VIMS_BIZ_INVOICE.Add(entity); } } this.Errors = GetErrors(db); if (!this.HasError) { try { //db.SaveChanges(); probe = db.SaveChanges(); } catch (Exception ex) { exceptionMsg = ex.Message; } } else { exceptionMsg = this.Errors != null && this.Errors.Count > 0 ? this.Errors.Values.FirstOrDefault() : "DTO校验异常"; } } soap.Result = new ImportInvoiceResultDTO { Message = !string.IsNullOrEmpty(exceptionMsg) ? exceptionMsg : "SUCCESS", Status = probe }; return(soap); }