WinForm中DataGrid扩展类 - 快速导出Excel文件,带保存对话框,并杀死进程。相对完美的解决方案[转http://yuunagi.blogbus.com/logs/38361340.html]

using System;
using System.Windows.Forms;
using System.Diagnostics;


namespace Xc_db_rygl
{
 
/// <summary>
 
/// ExportDataGrid 的摘要说明。
 
/// </summary>

 public class ExportDataGrid
 
{
  
public DataGrid ExportGrid;
  
public Form ParentWindow=null;
  
/// <summary>
  
/// made by 江边孤鸟 who's email is jbgh608@163.com
  
/// </summary>
  
/// <param name="parentWindow">父亲窗口</param>
  
/// <param name="grid">要导出的DataGrid</param>

  public ExportDataGrid(Form parentWindow,DataGrid   grid)
  
{
   ExportGrid
=grid;
   ParentWindow
= parentWindow;
   SaveFileDialog();
  }

  
保存对话框

  
#region 导出Excel
  
public bool ExportExcel(string FilePath,string p_ReportName)
  
{
   
if ( this.ExportGrid.TableStyles.Count == 0 ) return false;
   DataGridTableStyle ts 
= this.ExportGrid.TableStyles[0];

   
// 创建Excel对象                    
   Excel.Application xlApp = new Excel.ApplicationClass();
   
if ( xlApp == null )
   
{
    MessageBox.Show(
"Excel无法启动");
    
return false;
   }

   
// 创建Excel工作薄
   Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
   Excel.Worksheet xlSheet 
= (Excel.Worksheet)xlBook.Worksheets[1];
            
   
// 设置标题
   Excel.Range range = xlSheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,ts.GridColumnStyles.Count]);
   range.MergeCells 
= true;
   xlApp.ActiveCell.FormulaR1C1 
= p_ReportName;
   xlApp.ActiveCell.Font.Size 
= 20;
   xlApp.ActiveCell.Font.Bold 
= true;
   xlApp.ActiveCell.HorizontalAlignment 
= Excel.Constants.xlCenter;

   
// 列索引,行索引,总列数,总行数
   int colIndex = 0;
   
int RowIndex = 0;
   
int colCount = ts.GridColumnStyles.Count;
   
int RowCount = this.ParentWindow.BindingContext[this.ExportGrid.DataSource,this.ExportGrid.DataMember].Count;

   
// 创建缓存数据
   object[,] objData = new object[RowCount + 1, colCount ];
   
// 获取列标题
   foreach(DataGridColumnStyle cs in ts.GridColumnStyles)
   
{
    objData[RowIndex,colIndex
++= cs.HeaderText;    
   }
            
   
// 获取数据
   for(RowIndex =1;RowIndex<=RowCount;RowIndex++)
   
{
    
for(colIndex=0;colIndex < colCount;colIndex++)
    
{
     objData[RowIndex,colIndex] 
= this.ExportGrid[RowIndex-1,colIndex].ToString();
    }

    Application.DoEvents();
   }

   
// 写入Excel
   xlApp.get_Range(xlApp.Cells[2,   1],   xlApp.Cells[2,   colIndex]).Font.Bold   =   true;  
   range 
= xlSheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount+2,colCount]);            
   range.Value2 
= objData;

   
// 保存
   try
   
{
    xlApp.Cells.EntireColumn.AutoFit();   
    xlApp.Cells.VerticalAlignment   
=   Excel.Constants.xlCenter;   
    xlApp.Cells.HorizontalAlignment   
=   Excel.Constants.xlCenter;    
    
//xlApp.Visible   =   true;   
    xlBook.Saved  = true;
    xlBook.SaveCopyAs(FilePath 
+ ".xls");  
    MessageBox.Show(
"导出成功1!");
    
this.ParentWindow.Cursor=Cursors.Default;
   }

   
catch
   
{
    MessageBox.Show(
"保存出错,请检查!");
    
return false;
   }

   
finally
   
{
    xlApp.Quit();
    GC.Collect();
    KillProcess(
"excel") ;   
   }

   
return true;
  }

  
#endregion


  
#region 杀死进程
  
private void KillProcess(string processName) 
  

   System.Diagnostics.Process myproc
= new System.Diagnostics.Process(); 
   
//得到所有打开的进程 
   try 
   

    
foreach (Process thisproc in Process.GetProcessesByName(processName)) 
    

     thisproc.Kill();     
    }
 
   }
 
   
catch(Exception Exc) 
   

    
throw new Exception("",Exc); 
   }
 
  }
  
  
#endregion
 
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值