zoukankan      html  css  js  c++  java
  • asp.net 导出excel的一种方法

    项目用到的一种导出excel 的方法予以记录:(具体的业务类可更具情况替换使用)

    protected void Export(string filename, List<ComponentConmentDetailHelp> list)
    {
    string[][] cols = new string[][] {
    new string[] { "Id", "主键" },
    new string[] { "UserCode","员工卡号"},
    new string[] { "UserName","姓名"},
    new string[] { "Comment", "评论内容"},
    new string[] { "AuthdorName", "作者"},
    new string[] { "FileName", "文件名"},
    new string[] { "Ts","时间"}
    };

    StringBuilder builder = new StringBuilder();
    builder.Append("<html xmlns:o="urn:schemas-microsoft-com:office:office"");
    builder.Append("xmlns:x="urn:schemas-microsoft-com:office:excel"");
    builder.Append("xmlns="http://www.w3.org/TR/REC-html40"");
    builder.Append("<head>");
    builder.Append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
    builder.Append("<style>");
    builder.Append(".xl26");
    builder.Append("{mso-style-parent:style0;");
    builder.Append("font-family:"Times New Roman", serif;");
    builder.Append("mso-font-charset:0;");
    builder.Append("mso-number-format:"@";}");
    builder.Append("</style>");
    builder.Append("<xml>");
    builder.Append("<x:ExcelWorkbook>");
    builder.Append("<x:ExcelWorksheets>");
    builder.Append("<x:ExcelWorksheet>");
    builder.Append("<x:Name>Sheet1</x:Name>");
    builder.Append("<x:WorksheetOptions>");
    builder.Append("<x:DefaultRowHeight>285</x:DefaultRowHeight>");
    builder.Append("<x:Selected/>");
    builder.Append("<x:Panes>");
    builder.Append("<x:Pane>");
    builder.Append("<x:Number>3</x:Number>");
    builder.Append("<x:ActiveCol>1</x:ActiveCol>");
    builder.Append("</x:Pane>");
    builder.Append("</x:Panes>");
    builder.Append("<x:ProtectContents>False</x:ProtectContents>");
    builder.Append("<x:ProtectObjects>False</x:ProtectObjects>");
    builder.Append("<x:ProtectScenarios>False</x:ProtectScenarios>");
    builder.Append("</x:WorksheetOptions>");
    builder.Append("</x:ExcelWorksheet>");
    builder.Append("<x:WindowHeight>6750</x:WindowHeight>");
    builder.Append("<x:WindowWidth>10620</x:WindowWidth>");
    builder.Append("<x:WindowTopX>480</x:WindowTopX>");
    builder.Append("<x:WindowTopY>75</x:WindowTopY>");
    builder.Append("<x:ProtectStructure>False</x:ProtectStructure>");
    builder.Append("<x:ProtectWindows>False</x:ProtectWindows>");
    builder.Append("</x:ExcelWorkbook>");
    builder.Append("</xml>");
    builder.Append("</head>");
    builder.Append("<body>");
    builder.Append("<table align="center"style='border-collapse:collapse;table-layout:fixed'>");
    builder.Append("<tr>");

    System.IO.StringWriter writer = new System.IO.StringWriter();
    foreach (string[] col in cols)
    {
    builder.Append("<td><b>" + col[1] + "</b></td>");
    }
    builder.Append("</tr>");

    foreach (ComponentConmentDetailHelp item in list)
    {
    Type type = item.GetType();//获取类型
    builder.Append("<tr>");
    foreach (string[] col in cols)
    {
    System.Reflection.PropertyInfo propertyInfo = type.GetProperty(col[0]);
    string val = Convert.ToString(propertyInfo.GetValue(item, null));

    builder.Append("<td class='xl26'>" + val + "</td>");
    }
    builder.Append("</tr>");
    }
    builder.Append("</body>");
    builder.Append("</html>");
    WebUtility.ExportToFile(filename + ".xls", builder.ToString(), "application/ms-excel");

    }

  • 相关阅读:
    JAVA实现加入收藏和设为首页---网摘
    oracle序列的创建和使用
    针对Eclipse闪退的两种解决方案
    Mavean多工程依赖项目
    Z_Tree的使用案例(出差地点的演示)
    JAVA将数字钱数转换为大写
    提交表单时,post方式无法提交(一种情况)
    HTML中字体的垂直排列
    按照拼音排序的SQL语句条件
    在jsp里调用out.flush()和response.flushBuffer()有什么区别
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/3666227.html
Copyright © 2011-2022 走看看