public bool Export ( DataGrid dataGrid, string excelTitle)
{
System. Data. DataTable dt = new System. Data. DataTable ( ) ;
for ( int i = 0 ; i < dataGrid. Columns. Count; i++ )
{
if ( dataGrid. Columns[ i] . Visibility == System. Windows. Visibility. Visible)
{
dt. Columns. Add ( dataGrid. Columns[ i] . Header. ToString ( ) ) ;
}
}
for ( int i = 0 ; i < dataGrid. Items. Count; i++ )
{
int columnsIndex = 0 ;
System. Data. DataRow row = dt. NewRow ( ) ;
for ( int j = 0 ; j < dataGrid. Columns. Count; j++ )
{
if ( dataGrid. Columns[ j] . Visibility == System. Windows. Visibility. Visible)
{
if ( dataGrid. Items[ i] != null && ( dataGrid. Columns[ j] . GetCellContent ( dataGrid. Items[ i] ) as TextBlock) != null )
{
row[ columnsIndex] = ( dataGrid. Columns[ j] . GetCellContent ( dataGrid. Items[ i] ) as TextBlock) . Text. ToString ( ) ;
}
else row[ columnsIndex] = "" ;
columnsIndex++ ;
}
}
dt. Rows. Add ( row) ;
}
if ( ExcelExport ( dt, excelTitle) != null )
{
return true ;
}
else
{
return false ;
}
}
public string ExcelExport ( System. Data. DataTable DT, string title)
{
try
{
Microsoft. Office. Interop. Excel. Application ExcelApp = new Microsoft. Office. Interop. Excel. Application ( ) ;
Microsoft. Office. Interop. Excel. Workbook ExcelBook = ExcelApp. Workbooks. Add ( System. Type. Missing) ;
Microsoft. Office. Interop. Excel. Worksheet ExcelSheet = ( Microsoft. Office. Interop. Excel. Worksheet) ExcelBook. Worksheets[ 1 ] ;
ExcelSheet. Cells. NumberFormat = "@" ;
ExcelSheet. Name = title;
/ / 设置Sheet标题
string start = "A1" ;
string end = ChangeASC ( DT. Columns. Count) + "1" ;
Microsoft. Office. Interop. Excel. Range _Range = ( Microsoft. Office. Interop. Excel. Range) ExcelSheet. get_Range ( start, end) ;
_Range. Merge ( 0 ) ;
_Range = ( Microsoft. Office. Interop. Excel. Range) ExcelSheet. get_Range ( start, end) ;
_Range. HorizontalAlignment = Microsoft. Office. Interop. Excel. XlHAlign. xlHAlignCenter;
_Range. Font. Size = 22 ;
_Range. Font. Name = "宋体" ;
ExcelSheet. Cells[ 1 , 1 ] = title;
_Range. EntireColumn. AutoFit ( ) ;
for ( int m = 1 ; m <= DT. Columns. Count; m++ )
{
ExcelSheet. Cells[ 2 , m] = DT. Columns[ m - 1 ] . ColumnName. ToString ( ) ;
start = "A2" ;
end = ChangeASC ( DT. Columns. Count) + "2" ;
_Range = ( Microsoft. Office. Interop. Excel. Range) ExcelSheet. get_Range ( start, end) ;
_Range. Font. Size = 15 ;
_Range. Font. Bold = true ;
_Range. Font. Name = "宋体" ;
_Range. EntireColumn. AutoFit ( ) ;
_Range. HorizontalAlignment = Microsoft. Office. Interop. Excel. XlHAlign. xlHAlignCenter;
}
for ( int i = 0 ; i < DT. Rows. Count; i++ )
{
for ( int j = 1 ; j <= DT. Columns. Count; j++ )
{
ExcelSheet. Cells[ i + 3 , j] = DT. Rows[ i] [ j - 1 ] . ToString ( ) ;
}
}
for ( int n = 0 ; n < DT. Rows. Count + 1 ; n++ )
{
start = "A" + ( n + 3 ) . ToString ( ) ;
end = ChangeASC ( DT. Columns. Count) + ( n + 3 ) . ToString ( ) ;
_Range = ( Microsoft. Office. Interop. Excel. Range) ExcelSheet. get_Range ( start, end) ;
_Range. Font. Size = 12 ;
_Range. Font. Name = "宋体" ;
_Range. EntireColumn. AutoFit ( ) ;
_Range. HorizontalAlignment = Microsoft. Office. Interop. Excel. XlHAlign. xlHAlignCenter;
}
ExcelApp. DisplayAlerts = false ;
Microsoft. Win32. SaveFileDialog sfd = new Microsoft. Win32. SaveFileDialog ( ) ;
sfd. DefaultExt = ".xlsx" ;
sfd. Filter = "Office 2007 File|*.xlsx|Office 2000-2003 File|*.xls|所有文件|*.*" ;
if ( sfd. ShowDialog ( ) == true )
{
if ( sfd. FileName != "" )
{
ExcelBook. SaveAs ( sfd. FileName) ;
}
}
ExcelBook. Close ( ) ;
ExcelApp. Quit ( ) ;
return sfd. FileName;
}
catch ( Exception e)
{
return null ;
}
}
private string ChangeASC ( int count)
{
string ascstr = "" ;
switch ( count)
{
case 1 :
ascstr = "A" ;
break ;
case 2 :
ascstr = "B" ;
break ;
case 3 :
ascstr = "C" ;
break ;
case 4 :
ascstr = "D" ;
break ;
case 5 :
ascstr = "E" ;
break ;
case 6 :
ascstr = "F" ;
break ;
case 7 :
ascstr = "G" ;
break ;
case 8 :
ascstr = "H" ;
break ;
case 9 :
ascstr = "I" ;
break ;
case 10 :
ascstr = "J" ;
break ;
case 11 :
ascstr = "K" ;
break ;
case 12 :
ascstr = "L" ;
break ;
case 13 :
ascstr = "M" ;
break ;
case 14 :
ascstr = "N" ;
break ;
case 15 :
ascstr = "O" ;
break ;
case 16 :
ascstr = "P" ;
break ;
case 17 :
ascstr = "Q" ;
break ;
case 18 :
ascstr = "R" ;
break ;
case 19 :
ascstr = "S" ;
break ;
case 20 :
ascstr = "Y" ;
break ;
default :
ascstr = "U" ;
break ;
}
return ascstr;
}