zoukankan      html  css  js  c++  java
  • 导出Excel(利用xml标记)

    using System;
    using System.Data;
    using System.Web;
    using System.Text;
    using System.IO;

    /// <summary>
            
    /// 生成Excel数据并返回文件名称
            
    /// strData为要显示的内容--Html编码格式
            
    /// </summary>
            
    /// <param name="strData"></param>
            
    /// <returns></returns>

            public static string strCreateExcelAndReturnFileName(string strData)
            
    {
                
    string strFileName = "myData.xls";
                
    //====================================================
                StringBuilder sb = new StringBuilder();
                sb.Append(
    "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
                sb.Append(
    "xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
                sb.Append(
    "xmlns=\"http://www.w3.org/TR/REC-html40\">");
                sb.Append("<head>");
                sb.Append(
    "<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">");
                sb.Append(
    "<meta name=ProgId content=Excel.Sheet>");
                sb.Append(
    "<meta name=Generator content=\"Microsoft Excel 10\">");
                sb.Append(
    "<!--[if gte mso 9]><xml>");
                sb.Append(
    "<x:ExcelWorkbook>");
                sb.Append(
    "<x:ExcelWorksheets>");
                sb.Append(
    "<x:ExcelWorksheet>");
                sb.Append(
    "<x:Name>电信卡报表</x:Name>");
                sb.Append(
    "<x:WorksheetOptions>");
                sb.Append(
    "<x:Print>");
                sb.Append(
    "<x:ValidPrinterInfo/>");
                sb.Append(
    "</x:Print>");
                sb.Append(
    "</x:WorksheetOptions>");
                sb.Append(
    "</x:ExcelWorksheet>");
                sb.Append(
    "</x:ExcelWorksheets>");
                sb.Append(
    "</x:ExcelWorkbook>");
                sb.Append(
    "</xml>");
                sb.Append(
    "<![endif]--> ");
                sb.Append(
    "</head>");
                sb.Append(
    "<body>");
                sb.Append(strData);
                sb.Append(
    "</body>");
                sb.Append(
    "</html>");
                
    //====================================================
                WriteExcelFile(HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings["path_excel"].ToString().Trim()) + strFileName,sb.ToString());
                
    //====================================================
                return System.Configuration.ConfigurationSettings.AppSettings["path_excel"].ToString().Trim() + strFileName;
            }



    /// <summary>
            
    ///    写Excel文件
            
    /// </summary>
            
    /// <param name="strFileName"></param>
            
    /// <param name="strData"></param>

            private void WriteExcelFile(string strFileName,string strData)
            
    {
                
    try
                
    {
                    StreamWriter sw 
    = new StreamWriter(strFileName,true,System.Text.Encoding.Default);
                    sw.WriteLine(strData);
                    sw.Close();
                }

                
    catch(Exception e)
                
    {
                    
    string str = e.Message;
                }

            }
  • 相关阅读:
    YII 视图层的超链接不会正常显示了
    GIT 命令大全详解
    git将一个分支完全覆盖另外一个分支如:stable分支代码完全覆盖brush分支
    MySQL 查询日志
    Yii 数据库查看保存数据时发生的错误
    Hyperledger Fabric(3)通道与组织
    Hyperledger Fabric(2)共识与交易
    Hyperledger Fabric(1)基础架构
    Hyperledger Fabric 环境搭建(2)
    Hyperledger Fabric 环境搭建(1)
  • 原文地址:https://www.cnblogs.com/ding0910/p/556829.html
Copyright © 2011-2022 走看看