1. private void exportToExcelReportFile(DataTable ds, string fileName, string rptName)  
  2.     {  
  3.         string filesPath = "";  
  4.         System.IO.StreamWriter excelDoc;  
  5.         excelDoc = new System.IO.StreamWriter(fileName);  
  6.         const string startExcelXML = "<xml version>\r\n<Workbook " +  
  7.               "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" +  
  8.               " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n " +  
  9.               "xmlns:x=\"urn:schemas-    microsoft-com:office:" +  
  10.               "excel\"\r\n xmlns:ss=\"urn:schemas-microsoft-com:" +  
  11.               "office:spreadsheet\">\r\n <Styles>\r\n " +  
  12.               "<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " +  
  13.               "<Alignment ss:Vertical=\"Center\"/>\r\n <Borders/>" +  
  14.               "\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" +  
  15.               "\r\n <Protection/>\r\n </Style>\r\n " +  
  16.               "<Style ss:ID=\"BoldColumn\">\r\n <Font " +  
  17.               "x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " +  
  18.               "<Style ss:ID=\"StringLiteral\">\r\n <NumberFormat" +  
  19.               " ss:Format=\"@\"/>\r\n </Style>\r\n <Style " +  
  20.               "ss:ID=\"Decimal\">\r\n <NumberFormat " +  
  21.               "ss:Format=\"0.00\"/>\r\n </Style>\r\n " +  
  22.               "<Style ss:ID=\"Integer\">\r\n <NumberFormat " +  
  23.               "ss:Format=\"0\"/>\r\n </Style>\r\n <Style " +  
  24.               "ss:ID=\"DateTimeLiteral\">\r\n <NumberFormat " +  
  25.               "ss:Format=\"mm/dd/yyyy hh:mm AM/PM\"/>\r\n </Style>\r\n " +  
  26.               "<Style ss:ID=\"DateLiteral\">\r\n <NumberFormat " +  
  27.               "ss:Format=\"mm/dd/yyyy;@\"/>\r\n </Style>\r\n " +  
  28.               "</Styles>\r\n ";  
  29.         const string endExcelXML = "</Workbook>";  
  30.         string rptNames = "";  
  31.         int rowCount = 0;  
  32.         int sheetCount = 1;  
  33.         excelDoc.Write(startExcelXML);  
  34.         excelDoc.Write("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">");  
  35.         excelDoc.Write("<Table>");  
  36.  
  37.         //Add Sql Where Infomation  
  38.         excelDoc.Write("<Row>");  
  39.         excelDoc.Write("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");  
  40.         excelDoc.Write(oneAndEightTitleInfo);  
  41.         excelDoc.Write("</Data></Cell>");  
  42.         excelDoc.Write("</Row>");  
  43.  
  44.         excelDoc.Write("<Row>");  
  45.         for (int x = 0; x < ds.Columns.Count; x++)  
  46.         {  
  47.             excelDoc.Write("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");  
  48.             excelDoc.Write(ds.Columns[x].ColumnName);  
  49.             excelDoc.Write("</Data></Cell>");  
  50.         }  
  51.         excelDoc.Write("</Row>");  
  52.         rptNames = rptNames + ".xml";  
  53.         filesPath = Request.PhysicalApplicationPath + directoryPath + rptName;  
  54.         foreach (DataRow x in ds.Rows)  
  55.         {  
  56.             rowCount++;  
  57.             //if the number of rows is > 64000 create a new page to continue output  
  58.             if (rowCount == 64000)  
  59.             {  
  60.                 rowCount = 0;  
  61.                 sheetCount++;  
  62.                 excelDoc.Write("</Table>");  
  63.                 excelDoc.Write(" </Worksheet>");  
  64.                 excelDoc.Write("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">");  
  65.                 excelDoc.Write("<Table>");  
  66.             }  
  67.             excelDoc.Write("<Row>"); //ID=" + rowCount + "  
  68.             for (int y = 0; y < ds.Columns.Count; y++)  
  69.             {  
  70.                 System.Type rowType;  
  71.                 rowType = x[y].GetType();  
  72.                 switch (rowType.ToString())  
  73.                 {  
  74.                     case "System.String":  
  75.                         string XMLstring = x[y].ToString();  
  76.                         XMLstring = XMLstring.Trim();  
  77.                         XMLstring = XMLstring.Replace("&""&");  
  78.                         XMLstring = XMLstring.Replace(">"">");  
  79.                         XMLstring = XMLstring.Replace("<""<");  
  80.                         excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" +  
  81.                                        "<Data ss:Type=\"String\">");  
  82.                         excelDoc.Write(XMLstring);  
  83.                         excelDoc.Write("</Data></Cell>");  
  84.                         break;  
  85.                     case "System.DateTime":  
  86.                         DateTime XMLDate = (DateTime)x[y];  
  87.                         string XMLDatetoString = "";  
  88.                         XMLDatetoString = XMLDate.Year.ToString() + "-" +  
  89.                              (XMLDate.Month < 10 ? "0" + XMLDate.Month.ToString() : XMLDate.Month.ToString()) + "-" + (XMLDate.Day < 10 ? "0" +  
  90.                              XMLDate.Day.ToString() : XMLDate.Day.ToString()) + "T" +  
  91.                              (XMLDate.Hour < 10 ? "0" + XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) + ":" +  
  92.                              (XMLDate.Minute < 10 ? "0" + XMLDate.Minute.ToString() : XMLDate.Minute.ToString());  
  93.                         if (XMLDate.Hour > 0 | XMLDate.Minute > 0)  
  94.                         {  
  95.                             excelDoc.Write("<Cell ss:StyleID=\"DateTimeLiteral\">" +  
  96.                                          "<Data ss:Type=\"DateTime\">");  
  97.                         }  
  98.                         else 
  99.                         {  
  100.                             excelDoc.Write("<Cell ss:StyleID=\"DateLiteral\">" +  
  101.                                          "<Data ss:Type=\"DateTime\">");  
  102.                         }  
  103.                         excelDoc.Write(XMLDatetoString);  
  104.                         excelDoc.Write("</Data></Cell>");  
  105.                         break;  
  106.                     case "System.Boolean":  
  107.                         excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" +  
  108.                                     "<Data ss:Type=\"String\">");  
  109.                         excelDoc.Write(x[y].ToString());  
  110.                         excelDoc.Write("</Data></Cell>");  
  111.                         break;  
  112.                     case "System.Int16":  
  113.                     case "System.Int32":  
  114.                     case "System.Int64":  
  115.                     case "System.Byte":  
  116.                         excelDoc.Write("<Cell ss:StyleID=\"Integer\">" +  
  117.                                 "<Data ss:Type=\"Number\">");  
  118.                         excelDoc.Write(x[y].ToString());  
  119.                         excelDoc.Write("</Data></Cell>");  
  120.                         break;  
  121.                     case "System.Decimal":  
  122.                     case "System.Double":  
  123.                         excelDoc.Write("<Cell ss:StyleID=\"Decimal\">" +  
  124.                               "<Data ss:Type=\"Number\">");  
  125.                         excelDoc.Write(x[y].ToString());  
  126.                         excelDoc.Write("</Data></Cell>");  
  127.                         break;  
  128.                     case "System.DBNull":  
  129.                         excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" +  
  130.                               "<Data ss:Type=\"String\">");  
  131.                         excelDoc.Write("");  
  132.                         excelDoc.Write("</Data></Cell>");  
  133.                         break;  
  134.                     default:  
  135.                         throw (new Exception(rowType.ToString() + " not handled."));  
  136.                 }  
  137.             }  
  138.             excelDoc.Write("</Row>");  
  139.         }  
  140.         excelDoc.Write("</Table>");  
  141.         excelDoc.Write(" </Worksheet>");  
  142.         excelDoc.Write(endExcelXML);  
  143.         excelDoc.Close();  
  144.     }