//有保存对话框

public void dgvToExcel(System.Windows.Forms.DataGridView datagridview, string SheetName)
        {
            
int iRows = 0;
            
int iCols = 0;
            
int iTrueCols = 0;
            Microsoft.Office.Interop.Excel.Application app 
= new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook wb 
= app.Workbooks.Add(System.Reflection.Missing.Value);
            Microsoft.Office.Interop.Excel.Worksheet ws 
= null;

            
if (wb.Worksheets.Count > 0)
            {
                ws 
= (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
            }
            
else
            {
                wb.Worksheets.Add(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                ws 
= (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
            }
            
if (ws != null)
            {
                
if (SheetName.Trim() != "")
                {
                    ws.Name 
= SheetName;
                }
                iRows 
= datagridview.Rows.Count;      //加上列头行
                iTrueCols = datagridview.Columns.Count;   //包含隐藏的列,一共有多少列
                
//求列数,省略Visible = false的列
                for (int i = 0; i < datagridview.Columns.Count; i++)
                {
                    
if (datagridview.Columns[i].Visible) iCols++;
                }
                
string[,] dimArray = new string[iRows + 1, iCols];
                
for (int j = 0, k = 0; j < iTrueCols; j++)
                {
                    
//省略Visible = false的列
                    if (datagridview.Columns[j].Visible)
                    {
                        dimArray[
0, k] = datagridview.Columns[j].HeaderText;
                        k
++;
                    }
                }
                
for (int i = 0; i < iRows; i++)
                {
                    
for (int j = 0, k = 0; j < iTrueCols; j++)
                    {
                        
//省略Visible = false的列
                        if (datagridview.Columns[j].Visible)
                        {
                            
if (datagridview.Rows[i].Cells[j].Value != null)
                            {
                                dimArray[i 
+ 1, k] = datagridview.Rows[i].Cells[j].Value.ToString();
                            }
                            
else
                            {
                                dimArray[i 
+ 1, k] = "";
                            }
                            k
++;
                        }
                    }
                }
                ws.get_Range(ws.Cells[
11], ws.Cells[iRows + 1, iCols]).Value2 = dimArray;
                ws.get_Range(ws.Cells[
11], ws.Cells[1, iCols]).Font.Bold = true;
                ws.get_Range(ws.Cells[
11], ws.Cells[iRows + 1, iCols]).Font.Size = 10.0;
                ws.get_Range(ws.Cells[
11], ws.Cells[iRows + 1, iCols]).RowHeight = 14.25;
                
//ws.Columns[.ColumnWidth = datagridview.Columns[0].Width;
                for (int j = 0, k = 0; j < iTrueCols; j++)
                {
                    
//省略Visible = false的列
                    if (datagridview.Columns[j].Visible)
                    {
                        ws.get_Range(ws.Cells[
1, k + 1], ws.Cells[1, k + 1]).ColumnWidth = (datagridview.Columns[j].Width / 8.4> 255 ? 255 : (datagridview.Columns[j].Width / 8.4);
                        
//ws.Columns.c = datagridview.Columns[j].Width;
                        k++;
                    }
                }
            }
            app.Visible 
= true;
        }