/// <summary>
/// 把DataTable导出到EXCEL
/// </summary>
/// <param name="reportName">报表名称</param>
/// <param name="dt">数据源表</param>
/// <param name="saveFileName">Excel全路径文件名</param>
/// <returns>导出是否成功</returns>
public bool SubExportExcel(string reportName,DataTable dt,string saveFileName)
{
if(dt==null) 
{
_ReturnStatus = -1;
_ReturnMessage = "数据集为空!";
return false;           
}
bool fileSaved=false; //保存文件
if(saveFileName!="")
{
Excel.Application xlApp=new Excel.ApplicationClass();    if(xlApp==null)
{
_ReturnStatus = -1;
_ReturnMessage = "无法创建Excel对象,可能您的计算机未安装Excel";
return false;
}  Excel.Workbooks workbooks=xlApp.Workbooks;
Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
Excel.Range range;

worksheet.Cells.Font.Size = 10; long totalCount=dt.Rows.Count;
long rowRead=0;
float percent=0; worksheet.Cells.NumberFormatLocal = "@" ;
worksheet.Cells[1,1] = reportName ;
((Excel.Range)worksheet.Cells[1,1]).Font.Size = 12 ;
((Excel.Range)worksheet.Cells[1,1]).Font.Bold = true ; //写入字段
for(int i=0;i<dt.Columns.Count;i++)
{
worksheet.Cells[2,i+1]=dt.Columns[i].ColumnName;
range=(Excel.Range)worksheet.Cells[2,i+1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true; }
//写入数值
for(int r=0;r<dt.Rows.Count;r++)
{
for(int i=0;i<dt.Columns.Count;i++)
{
worksheet.Cells[r+3,i+1]=dt.Rows[r][i].ToString() ;
}
rowRead++;
percent=((float)(100*rowRead))/totalCount ;
}
        
range = worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[dt.Rows.Count+2,dt.Columns.Count]) ;
range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null) ; if( dt.Rows.Count > 0)
{
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;
} if(dt.Columns.Count>1)
{
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;


try
{
workbook.Saved =true;
workbook.SaveCopyAs(saveFileName);
fileSaved=true;
}
catch(Exception ex)
{
fileSaved=false;
_ReturnStatus = -1;
_ReturnMessage = "导出文件时出错,文件可能正被打开!\n"+ex.Message;
}
finally
{
//释放Excel对应的对象
xlApp.Application.Workbooks.Close(); if(range != null)

System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
range = null;
}
if(worksheet != null)

System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
}
if(workbook != null)

System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
}
if(workbooks != null)

System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
workbooks = null;
}
xlApp.Quit();
if(xlApp != null)

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
GC.Collect() ;
}
}
else
{
fileSaved=false;
}           
    
return fileSaved;
} public bool ExportExcel(string reportName,DataTable dt,string saveFileName)
{
bool flag = false ;
if(SubExportExcel(reportName,dt,saveFileName))
{
flag = true ;
}
else
{
flag =  false ;
}
GC.Collect();
return flag ;
}我调用ExportExcel方法,始终无法清除Excel进程,请高手指点一下,究竟是哪里的原因

解决方案 »

  1.   

     你没有关点你创建的EXCEL
    我把我的函数贴给你看
     public void ExportDataGridViewToExcel1(DataGridView dataGridView1)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.CreatePrompt = false;
                saveFileDialog.Title = "导出Excel文件到";            if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }
                Stream myStream = saveFileDialog.OpenFile();            Excel.ApplicationClass MyExcel = new Excel.ApplicationClass();
                MyExcel.Visible = true;
                int rowcount = 0;
                int columncount = 0;            MyExcel.Application.Workbooks.Add(true);
                columncount = dataGridView1.ColumnCount;
                rowcount = dataGridView1.RowCount;            try
                {
                    for (int m = 0; m < columncount; m++)
                    {
                        MyExcel.Cells[1, m + 1] = dataGridView1.Columns[m].HeaderText;
                    }
                    for (int i = 0; i < rowcount - 1; i++)
                    {
                        for (int j = 0; j < columncount - 1; j++)
                        {
                            MyExcel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
                        }
                    }
                    MessageBox.Show("文件" + "\n\n导出完毕! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    MyExcel.Quit();
                    myStream.Close();
                }                
            }