winFrom程序里面的DataGrid导出到Excel 各们大哥,这怎么做啊~!~winFrom程序里面的DataGrid导出到Excel 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一下代码仅能支持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=\"&A\"/> " + " <Footer x:Data=\"Page &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 ; } } 忘记说了,其中的DataTable 可以用 this.DataGrid1.DataSource获得. GetDateFormat()不能直接用吧 C#如何连接Oracle RAC集群数据库 客户端浏览Default.aspx窗体时执行服务器的Page_Load过程,那么服务器端如何得知客户端关闭了窗体Default.aspx 如何将数据集导入到Excel,让列成下拉框固定? 分组水晶報表的问题。。。 关于群发邮件 为什么2005里边好象没有arraylist了呢? 菜菜鸟求asp.net课设 关于IIS Express的疑问 如何禁止HTML的自动格式 在线等待:最简单的Response.Write语句怎么都会报错?急! 求一个正则表达式 关于visio的一些小问题
{
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=\"&A\"/> " +
" <Footer x:Data=\"Page &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 ;
}
}
不能直接用吧