C# 实现Excel读取接口写入数据
[Route("adm/getInfo")]
[HttpGet]
public string GetInfo()
{
var types = typeof(GCP.Server.WebAPI.Controllers.HomeController).Assembly.GetTypes().Where(m => m.Name.EndsWith("Controller"));
System.Text.StringBuilder sb = new System.Text.StringBuilder(100);
sb.Append("接口").Append(",").Append("类型").Append(",").Append("注释").AppendLine();
using (System.IO.StreamReader sr = new System.IO.StreamReader(@"F:\GCP\SourceCode\GCP\GCP.WebAPI\App_Data\GCP.WebAPI.XML"))
{
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.Load(@"F:\GCP\SourceCode\GCP\GCP.WebAPI\App_Data\GCP.WebAPI.XML");
var members = xml.LastChild.LastChild.ChildNodes;
//var members = xml.LastChild.ChildNodes;
foreach (var t in types)
{
var routePrefix = t.GetCustomAttributes(false).FirstOrDefault(m => m.ToString().Contains("RoutePrefix")) as RoutePrefixAttribute;
if (routePrefix == null)
{
continue;
}
string prefix = routePrefix.Prefix;
var ms = t.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
.Where(m => m.GetCustomAttributes(false).Any(n => n.GetType() == typeof(RequestControlAttribute)));
foreach (var m in ms)
{
var requestCtrl = m.GetCustomAttributes(typeof(RequestControlAttribute), false)[0] as RequestControlAttribute;
var routeAttr = m.GetCustomAttributes(typeof(RouteAttribute), false)[0] as RouteAttribute;
Row r = new Row
{
Url = prefix + "/" + routeAttr.Template,
Type = requestCtrl.requestorType.ToString(),
Content = GetContent(members, m)
};
sb.Append(r.Url).Append(",").Append(r.Type).Append(",").Append(r.Content).AppendLine();
}
}
}
System.IO.File.WriteAllText("e:/info.csv", sb.ToString(), System.Text.Encoding.UTF8);
return "OK";
}
private string GetContent(XmlNodeList members, MethodInfo m)
{
for (int i = 0; i < members.Count; i++)
{
var mem = members[i];
if (mem.Attributes["name"].Value.Contains(m.DeclaringType.ToString() + "." + m.Name))
{
return mem.FirstChild.InnerText.Replace("\r\n", "").Replace(",", "_").Trim();
}
}
return "";
}
class Row
{
public string Url { get; set; }
public string Type { get; set; }
public string Content { get; set; }
}
人说,如果你很想要一样东西,就放它走 。如果它回来找你,那么它永远都是你的。要是它没有回来,那么不用再等了,因为它根本就不是你的。
——伊恩·麦克尤恩