数据导出之--将GridView数据导出到Excel( 原)

//将GridView导出到Excel

 public static void DataGridToExcel(DataGridView dgv)       

{                      

if (dgv.Columns.Count == 0)           

{  

return;           

}           

Excel.Application exc = new Excel.ApplicationClass();           

if (exc == null)           

{               

throw new Exception("Excel无法启动");           

}           

ProcessQueryingForm pProcessfrm = new ProcessQueryingForm();           

pProcessfrm.SetProcessText("正在打开Excel,请稍候....");           

pProcessfrm.Show();           

System.Windows.Forms.Application.DoEvents();//'转让控制权,没有这一句的话提示窗口不能正常显示

Excel.Workbooks workbooks = exc.Workbooks;           

Excel._Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

Excel.Sheets sheets = exc.Sheets;           

Excel._Worksheet worksheet = (Excel._Worksheet)sheets[1];           

if (worksheet == null)           

{               

throw new Exception("Excel启动错误");           

}           

//填充表头           

Excel.Range range;           

int j = 1;           

for (int i = 0; i < dgv.ColumnCount; i++)           

{               

if (dgv.Columns[i].Visible == true)               

{                   

worksheet.Cells[1, j] = dgv.Columns[i].HeaderText;                   

range = (Excel.Range)worksheet.Cells[1, j];                   

range.Borders.LineStyle = 1.5;                             //表格边框粗细                   

range.EntireColumn.AutoFit();                              //自动调整列宽                   

range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; //文字居中                   

range.Font.Bold = true;                                    //粗体                   

range.RowHeight = 25;                                      //行高

exc.ActiveCell.Font.Size = 12;                   

exc.ActiveCell.Font.Bold = true;                   

exc.ActiveCell.EntireColumn.AutoFit();                   

j++;               

}           

}

for (int r = 0; r < dgv.Rows.Count - 1; r++)           

{               

int jj = 1;               

for (int i = 0; i < dgv.Columns.Count; i++)               

{                   

if (i + 1 != dgv.Columns.Count + 1 && dgv.Columns[i].Visible == true)                       

if (dgv.Rows[r].Cells[i].Value != null)                       

{                           

worksheet.Cells[r + 2, jj] = dgv.Rows[r].Cells[i].Value.ToString();                           

jj++;

}                       

else                       

{                           

worksheet.Cells[r + 2, jj] = "";                           

jj++;                       

}        

}            

}           

range = worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[dgv.Rows.Count + 2, dgv.Columns.Count]);            range.EntireColumn.AutoFit();           

range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;           

range.RowHeight = 20;

exc.Visible = true;           

exc.DisplayAlerts = false;           

pProcessfrm.Dispose();

}

posted @ 2011-03-22 22:11  @龙飞凤舞@  阅读(709)  评论(4编辑  收藏  举报