各们大哥,这怎么做啊~!~winFrom程序里面的DataGrid导出到Excel

解决方案 »

  1.   

    一下代码仅能支持OFFICE 2003,其他版本的Office 打开此Excel的话,需要先用Office 2003存为xls格式或者其他版本能看的格式才行,一下是代码:public static void Export2XmlConvert2Excel( DataTable table , string filename )
    {
    System.Text.StringBuilder sb = new System.Text.StringBuilder() ;
    System.IO.StreamWriter sw = new System.IO.StreamWriter( filename , false , System.Text.Encoding.UTF8 ) ; try
    {
    sb.Append( "<?xml version=\"1.0\"?>" +
    " <?mso-application progid=\"Excel.Sheet\"?> " +
    " <Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" " +
    " xmlns:o=\"urn:schemas-microsoft-com:office:office\" " + 
    " xmlns:x=\"urn:schemas-microsoft-com:office:excel\" " +
    " xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" " +
    " xmlns:html=\"http://www.w3.org/TR/REC-html40\"> " +
    " <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\"> " +
    " <LastAuthor>Zero Xiong</LastAuthor> " +
    " <Created>" + GetDateFormat( DateTime.Now.ToShortDateString() ) + "T08:45:22Z</Created> " +
    " <Version>11.5606</Version> " +
    " </DocumentProperties> " +
    " <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\"> " +
    " <WindowHeight>10830</WindowHeight> " +
    " <WindowWidth>15480</WindowWidth> " +
    " <WindowTopX>360</WindowTopX> " +
    " <WindowTopY>75</WindowTopY> " +
    " <AcceptLabelsInFormulas/> " +
    " <ProtectStructure>False</ProtectStructure> " +
    " <ProtectWindows>False</ProtectWindows> " +
    " </ExcelWorkbook> " +
    " <Styles> " +
    " <Style ss:ID=\"Default\" ss:Name=\"Normal\"> " +
    " <Alignment ss:Vertical=\"Bottom\"/> " +
    " <Borders/> " +
    " <Font ss:FontName=\"MS Sans Serif\" x:Family=\"Swiss\"/> " +
    " <Interior/> " +
    " <NumberFormat/> " +
    " <Protection/> " +
    " </Style> " +
    " <Style ss:ID=\"s23\"> " +
    " <NumberFormat/> " +
    " </Style> " +
    " <Style ss:ID=\"s24\"> " +
    " <NumberFormat ss:Format=\"Short Date\"/> " +
    " </Style> " +
    " </Styles> " +
    " <Names> " +
    " <NamedRange ss:Name=\"Results\" ss:RefersTo=\"=Results!R1C1:R101C23\"/> " +
    " </Names> " +
    " <Worksheet ss:Name=\"Results\"> " +
    " <Table ss:ExpandedColumnCount=\"" + table.Columns.Count.ToString() + "\" ss:ExpandedRowCount=\"" + ( table.Rows.Count + 1 ).ToString() + "\" x:FullColumns=\"1\" x:FullRows=\"1\"> " )  ; sb.Append(" <Row> ") ;
    foreach(DataColumn dc in table.Columns ) 
    {
    sb.Append(" <Cell ss:StyleID=\"s23\"><Data ss:Type=\"String\" x:Ticked=\"1\">" + dc.ColumnName.Trim() + "</Data> " +
    " <NamedCell ss:Name=\"Results\"/></Cell> ") ;
    }
    sb.Append(" </Row> ") ; foreach(DataRow dr in table.Rows )
    {
    sb.Append(" <Row> ") ;
    foreach(object obj in dr.ItemArray ) 
    {
    if ( obj.GetType().ToString() == "System.Decimal" || obj.GetType().ToString() == "System.Int32" )
    sb.Append(" <Cell ss:StyleID=\"s23\"><Data ss:Type=\"Number\">" + obj.ToString() + "</Data> " +
    " <NamedCell ss:Name=\"Results\"/></Cell> ") ;
    else if ( obj.GetType().ToString() == "System.DateTime" )
    sb.Append(" <Cell ss:StyleID=\"s24\"><Data ss:Type=\"DateTime\">" + GetDateFormat(DateTime.Parse(obj.ToString()).ToShortDateString()) + "T00:00:00.000</Data> " +
    " <NamedCell ss:Name=\"Results\"/></Cell> ") ;
    else
    sb.Append(" <Cell ss:StyleID=\"s23\"><Data ss:Type=\"String\" x:Ticked=\"1\">" + obj.ToString() + "</Data> " +
    " <NamedCell ss:Name=\"Results\"/></Cell> ") ;

    }
    sb.Append(" </Row> ") ;
    } sb.Append(      " </Table> " +
    " <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\"> " +
    " <PageSetup> " +
    "  <Header x:Data=\"&amp;A\"/> " +
    "  <Footer x:Data=\"Page &amp;P\"/> " +
    " </PageSetup> " +
    " <Print> " +
    "  <ValidPrinterInfo/> " +
    " <PaperSizeIndex>9</PaperSizeIndex> " +
    " <HorizontalResolution>200</HorizontalResolution> " +
    " <VerticalResolution>200</VerticalResolution> " +
    " </Print> " +
    " <Selected/> " +
    " <ProtectObjects>False</ProtectObjects> " +
    " <ProtectScenarios>False</ProtectScenarios> " +
    " </WorksheetOptions> " +
    " </Worksheet> " +
    " </Workbook>  " )  ; sw.Write( sb.ToString() ) ;
    sw.Close() ;
    }
    catch(Exception ex )
    {
    throw ex ;
    }

    }
      

  2.   

    忘记说了,其中的DataTable  可以用 this.DataGrid1.DataSource获得.
      

  3.   

    GetDateFormat()
    不能直接用吧