- 从超大html表单(标题和input成对)中提取“标题和input中的ref值”
static void Main(string[] args)
{
ReadTxt();
InsertText();
}
/// <summary>
/// html有一个很大表单(标题和input成对),本方法实现:提取标题和input的ref属性放到List集合中,并转成可贴到Excel格式
/// </summary>
/// <param name="Path">文件地址</param>
public static void ReadTxt()
{
string Path = @"D:\DiagnosisInfo.html";
StreamReader sr = new StreamReader(Path, Encoding.UTF8);
string content;
List<string> list = new List<string>();
string[] pair = new string[2];
while ((content = sr.ReadLine()) != null)
{
string line = content.ToString()
.Replace("\"", "")
.Replace("'", "")
.Replace(">", "")
.Replace(" style=width: 80px", "")
.Replace(" style=", "\t")
.Replace(" :disabled", "\t")
.Replace(" width", "\t")
.Replace(" size=small", "");
if (line.IndexOf("<el-form-item") >= 0)
{
string[] ar = line.Replace("label=", "@").Split('@');
string title = ar.Length >= 2 ? ar[1] : "";
pair[0] = title;
}
else if (line.IndexOf("ref=") >= 0)
{
string model = line.Replace("ref=", "@").Split('@')[1];
pair[1] = model;
}
if (!string.IsNullOrWhiteSpace(pair[0]) && !string.IsNullOrWhiteSpace(pair[1]))
{
list.Add(pair[0] + "\t" + pair[1]);
pair[0] = "";
pair[1] = "";
}
}
string result = string.Join("\r\n", list);
}
/// <summary>
/// html有一个很大表单(标题和input成对),本方法实现:在input控件的“ref='控件id'”属性前面加上“ @keyup.native="JumpFocus('控件id')"”(其中“控件id”取自ref值)
/// </summary>
private static void InsertText()
{
string Path = @"D:\word_code\Client\cis\MRS_CASE_REPORT\src\components\cis\mrs_case_report\DiagnosisInfo\DiagnosisInfo.html";
StreamReader sr = new StreamReader(Path, Encoding.UTF8);
string content;
List<string> list = new List<string>();
while ((content = sr.ReadLine()) != null)
{
string line = content.ToString();
if (line.IndexOf("ref=") >= 0)
{
string start = "ref='";
string end = "'";
Regex rgx = new Regex("(?<=(" + start + "))[.\\s\\S]*?(?=(" + end + "))", RegexOptions.Multiline | RegexOptions.Singleline);//截取“ref='”和“'”之间的值
string tmp = rgx.Match(line).Value;//控件id
line = line.Replace("ref=", " @keyup.native=\"JumpFocus('"+tmp+"')\" ref=");
}
list.Add(line);
}
string result = string.Join("",list);
}