DataSet,DataTable 不使用EXCEL组件直接保存为EXCEL

 B/S模式下DataSet导出保存为EXCEL直接可以Respone出来,比较简单,而C/S模式下将DataSet导出到EXCEL相对比较麻烦,要使用EXCEL要引用Microsoft.Office.Interop.Excel,经常会出现一些EXCEL进程无法关闭的问题,很让人厌恶,查看了网上一些资料,其实EXCEL也是有他自己的固定的XML格式的
。具体格式如下,首先要先定义好它的头部分,
显示出来的EXCEL 才不会错误。

public    string  ExcelHeader()
        
{
            System.Text.StringBuilder sb 
= new System.Text.StringBuilder();
            sb.Append(
"<?xml version=\"1.0\"?>\n");
            sb.Append(
"<?mso-application progid=\"Excel.Sheet\"?>\n");
            sb.Append(
              
"<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" ");
            sb.Append(
"xmlns:o=\"urn:schemas-microsoft-com:office:office\" ");
            sb.Append(
"xmlns:x=\"urn:schemas-microsoft-com:office:excel\" ");
            sb.Append(
"xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" ");
            sb.Append(
"xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n");
            sb.Append(
              
"<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
            sb.Append(
"</DocumentProperties>");
            sb.Append(
              
"<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n");
            sb.Append(
"<ProtectStructure>False</ProtectStructure>\n");
            sb.Append(
"<ProtectWindows>False</ProtectWindows>\n");
            sb.Append(
"</ExcelWorkbook>\n");
            
return sb.ToString();
        }


接下来就是具体的内容了,包括DATASET,DATATABLE,等数据源

public   void  exportToExcel(DataSet source,  string  fileName)
        
{
            position 
= 0;
            System.IO.StreamWriter excelDoc;

            excelDoc 
= new System.IO.StreamWriter(fileName);
            
string startExcelXML = ExcelHeader();
            startExcelXML 
+= "<Styles>\r\n " +
                
"<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " +
                
"<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" +
                
"\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" +
                
"\r\n <Protection/>\r\n </Style>\r\n " +
                
"<Style ss:ID=\"BoldColumn\">\r\n <Font " +
                
"x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " +
                
"<Style ss:ID=\"StringLiteral\">\r\n <NumberFormat" +
                
" ss:Format=\"@\"/>\r\n </Style>\r\n <Style " +
                
"ss:ID=\"Decimal\">\r\n <NumberFormat " +
                
"ss:Format=\"0.0000\"/>\r\n </Style>\r\n " +
                
"<Style ss:ID=\"Integer\">\r\n <NumberFormat " +
                
"ss:Format=\"0\"/>\r\n </Style>\r\n <Style " +
                
"ss:ID=\"DateLiteral\">\r\n <NumberFormat " +
                
"ss:Format=\"mm/dd/yyyy;@\"/>\r\n </Style>\r\n " +
                
"</Styles>\r\n ";
            
const string endExcelXML = "</Workbook>";

            
int rowCount = 0;
            
int sheetCount = 1;
          
            excelDoc.Write(startExcelXML);
            excelDoc.Write(
"<Worksheet ss:Name=\"Sheet" + sheetCount + "\">");
            excelDoc.Write(
"<Table>");
            excelDoc.Write(
"<Row>");
            
for (int x = 0; x < source.Tables[0].Columns.Count; x++)
            
{
                excelDoc.Write(
"<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");
                excelDoc.Write(source.Tables[
0].Columns[x].ColumnName);
                excelDoc.Write(
"</Data></Cell>");
            }

            excelDoc.Write(
"</Row>");
            
foreach (DataRow x in source.Tables[0].Rows)
            
{
                rowCount
++;
//Excel 最多只能显示64000条数据,在同一个WorkSheet里
                if (rowCount == 64000)
                
{
                    rowCount 
= 0;
                    sheetCount
++;
                    excelDoc.Write(
"</Table>");
                    excelDoc.Write(
" </Worksheet>");
                    excelDoc.Write(
"<Worksheet ss:Name=\"Sheet" + sheetCount + "\">");
                    excelDoc.Write(
"<Table>");
                }

                excelDoc.Write(
"<Row>"); //ID=" + rowCount + "
                for (int y = 0; y < source.Tables[0].Columns.Count; y++)
                
{
                    System.Type rowType;
                    rowType 
= x[y].GetType();
                    
switch (rowType.ToString())
                    
{
                        
case "System.String":
                            
string XMLstring = x[y].ToString();
                            XMLstring 
= XMLstring.Trim();
                            XMLstring 
= XMLstring.Replace("&""&");
                            XMLstring 
= XMLstring.Replace(">"">");
                            XMLstring 
= XMLstring.Replace("<""<");
                            excelDoc.Write(
"<Cell ss:StyleID=\"StringLiteral\">" +
                                           
"<Data ss:Type=\"String\">");
                            excelDoc.Write(XMLstring);
                            excelDoc.Write(
"</Data></Cell>");
                            
break;
                        
case "System.DateTime": 

                            DateTime XMLDate = (DateTime)x[y];
                            
string XMLDatetoString = ""//Excel Converted Date
                            XMLDatetoString = XMLDate.Year.ToString() +
                                 
"-" +
                                 (XMLDate.Month 
< 10 ? "0" +
                                 XMLDate.Month.ToString() : XMLDate.Month.ToString()) 
+
                                 
"-" +
                                 (XMLDate.Day 
< 10 ? "0" +
                                 XMLDate.Day.ToString() : XMLDate.Day.ToString()) 
+
                                 
"T" +
                                 (XMLDate.Hour 
< 10 ? "0" +
                                 XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) 
+
                                 
":" +
                                 (XMLDate.Minute 
< 10 ? "0" +
                                 XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) 
+
                                 
":" +
                                 (XMLDate.Second 
< 10 ? "0" +
                                 XMLDate.Second.ToString() : XMLDate.Second.ToString()) 
+
                                 
".000";
                            excelDoc.Write(
"<Cell ss:StyleID=\"DateLiteral\">" +
                                         
"<Data ss:Type=\"DateTime\">");
                            excelDoc.Write(XMLDatetoString);
                            excelDoc.Write(
"</Data></Cell>");
                            
break;
                        
case "System.Boolean":
                            excelDoc.Write(
"<Cell ss:StyleID=\"StringLiteral\">" +
                                        
"<Data ss:Type=\"String\">");
                            excelDoc.Write(x[y].ToString());
                            excelDoc.Write(
"</Data></Cell>");
                            
break;
                        
case "System.Int16":
                        
case "System.Int32":
                        
case "System.Int64":
                        
case "System.Byte":
                            excelDoc.Write(
"<Cell ss:StyleID=\"Integer\">" +
                                    
"<Data ss:Type=\"Number\">");
                            excelDoc.Write(x[y].ToString());
                            excelDoc.Write(
"</Data></Cell>");
                            
break;
                        
case "System.Decimal":
                        
case "System.Double":
                            excelDoc.Write(
"<Cell ss:StyleID=\"Decimal\">" +
                                  
"<Data ss:Type=\"Number\">");
                            excelDoc.Write(x[y].ToString());
                            excelDoc.Write(
"</Data></Cell>");
                            
break;
                        
case "System.DBNull":
                            excelDoc.Write(
"<Cell ss:StyleID=\"StringLiteral\">" +
                                  
"<Data ss:Type=\"String\">");
                            excelDoc.Write(
"");
                            excelDoc.Write(
"</Data></Cell>");
                            
break;
                        
default:
                            
throw (new Exception(rowType.ToString() + " not handled."));
                    }

                    position 
= y;
                    ProgressEventArgs pe 
= new ProgressEventArgs(position);
                    OnProgressChange(pe);
                }

                excelDoc.Write(
"</Row>");
            }

            excelDoc.Write(
"</Table>");
            excelDoc.Write(
" </Worksheet>");
            excelDoc.Write(endExcelXML);
            excelDoc.Close();
        }
}

你可能感兴趣的:(Datatable)