private void ExportExcel(System.Data.DataTable table,string path,string[] captions)
{
Excel.Application excel = new Excel.ApplicationClass();
Excel._Workbook xBk = excel.Workbooks.Add(true);
Excel._Worksheet xSt = (Excel._Worksheet)xBk.ActiveSheet;
//Excel.WorksheetClass worksheet= Excel.WorksheetClass)excel.ActiveSheet;for(int i = 0;i < captions.Length;i ++)  excel.Cells[1,i+1] = captions[i];for(int i = 0;i < table.Rows.Count;i ++)
{
     for(int j = 0;j < table.Columns.Count;j ++)
     {
          excel.Cells[i+2,j+1] = table.Rows[i][j];
     }
}xSt.get_Range(excel.Cells[1,1],excel.Cells[table.Rows.Count + 1,captions.Length]).Columns.AutoFit();excel.Visible = false; 
excel.DisplayAlerts = false;

excel.Save(path);
excel.Application.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
}

解决方案 »

  1.   

    用这个就可以了。我用的office对象库11.0如果你用的版本和我不一样,可能就是最后少两个或一个Missing.Value就行了。
    xBook.SaveAsexcelSavePath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);