獲取數據後導出Excel

List<PortResourceInfo> list = getList()//獲取數據源

//導出exclehtml

Response.Clear();
Response.ContentType = "application/excel";
Response.AddHeader("Content-Disposition", "inline;filename=" + HttpUtility.UrlEncode("端口資源.xls", Encoding.UTF8));
StringBuilder expStr = new StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(expStr);
expStr.Append("<html><body>");
expStr.Append("<table><tr><th>序號</th><th>設備名稱</th><th>設備型號</th><th>設備IP</th><th>端口名稱</th><th>端口描述</th><th>端口帶寬</th><th>Up/Down</th><th>端口狀態</th></tr>");
int i = 1;
foreach (var item in list)
{
expStr.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td></tr>",
i, item.DeviceName, item.DiveceType,
item.IpAddress, item.IfName, item.Description,
item.IfSpeed, item.UpOrDown, item.IsFree
);
i++;
}app

expStr.Append("</table></body></html>");
Page.EnableViewState = false;
Response.Write(sw.ToString());
Response.End();ui

相關文章
相關標籤/搜索