Excelメソッドへのデータのエクスポート


#region  Excel 
    /// <summary>
    ///  Excel 
    /// </summary>
    /// <param name="ds"> </param>
    /// <param name="fileName"> </param>
    protected void CreateExcel(DataTable dt, string fileName)
    {
        StringBuilder strb = new StringBuilder();
        strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
        strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
        strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\">");
        strb.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");
        strb.Append(" <style>");
        strb.Append(".xl26");
        strb.Append(" {mso-style-parent:style0;");
        strb.Append(" font-family:\"Times New Roman\", serif;");
        strb.Append(" mso-font-charset:0;");
        strb.Append(" mso-number-format:\"@\";}");
        strb.Append(" </style>");
        strb.Append(" <xml>");
        strb.Append(" <x:ExcelWorkbook>");
        strb.Append(" <x:ExcelWorksheets>");
        strb.Append(" <x:ExcelWorksheet>");
        strb.Append(" <x:Name>SheetAssets</x:Name>");
        strb.Append(" <x:WorksheetOptions>");
        strb.Append(" <x:DefaultRowHeight>285</x:DefaultRowHeight>");
        strb.Append(" <x:Selected/>");
        strb.Append(" <x:Panes>");
        strb.Append(" <x:Pane>");
        strb.Append(" <x:Number>3</x:Number>");
        strb.Append(" <x:ActiveCol>1</x:ActiveCol>");
        strb.Append(" </x:Pane>");
        strb.Append(" </x:Panes>");
        strb.Append(" <x:ProtectContents>False</x:ProtectContents>");
        strb.Append(" <x:ProtectObjects>False</x:ProtectObjects>");
        strb.Append(" <x:ProtectScenarios>False</x:ProtectScenarios>");
        strb.Append(" </x:WorksheetOptions>");
        strb.Append(" </x:ExcelWorksheet>");
        strb.Append(" <x:WindowHeight>6750</x:WindowHeight>");
        strb.Append(" <x:WindowWidth>10620</x:WindowWidth>");
        strb.Append(" <x:WindowTopX>480</x:WindowTopX>");
        strb.Append(" <x:WindowTopY>75</x:WindowTopY>");
        strb.Append(" <x:ProtectStructure>False</x:ProtectStructure>");
        strb.Append(" <x:ProtectWindows>False</x:ProtectWindows>");
        strb.Append(" </x:ExcelWorkbook>");
        strb.Append(" </xml>");
        strb.Append("");
        strb.Append(" </head> <body> <table align=\"center\" style='border-collapse:collapse;table-layout:fixed'> <tr>");
        if (dt.Rows.Count > 0)
        {
            //   
            int columncount = dt.Columns.Count;
            for (int columi = 0; columi < columncount; columi++)
            {
                strb.Append(" <td style='text-align:center;'><b>" + dt.Columns[columi] + "</b></td>");
            }
            strb.Append(" </tr>");
            //   
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strb.Append(" <tr>");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (dt.Columns[j].DataType == Type.GetType("System.Decimal"))
                    {
                        strb.Append(" <td class='xl26' style='text-align:right;'>" + dt.Rows[i][j].ToString() + "</td>");
                    }
                    else
                    {
                        strb.Append(" <td class='xl26'>" + dt.Rows[i][j].ToString() + "</td>");
                    }
                }
                strb.Append(" </tr>");
            }
        }
        strb.Append("</table></body></html>");
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//   
        Response.ContentType = "application/ms-excel";// excel 。  
        this.EnableViewState = false;
        Response.Write(strb);
        Response.End();

    }
    #endregion