WEB开发网
开发学院数据库MSSQL Server DataGridView导出到Excel 阅读

DataGridView导出到Excel

 2008-09-06 10:03:43 来源:WEB开发网   
核心提示: 3、成功引用了Excel之后,怎么使用?对于将DataGridView导出成Excel的应用来说,DataGridView导出到Excel(2),估计大家需要考虑的就是性能问题,如果一个导出操作要用户等几分钟甚至十几分钟的话,一共有多少列//求列数,省略Visible = false的列f

DataGridView导出到Excel

3、成功引用了Excel之后,怎么使用?对于将DataGridView导出成Excel的应用来说,估计大家需要考虑的就是性能问题,如果一个导出操作要用户等几分钟甚至十几分钟的话,是很难让很多人接受的。具体的解释我也不做了,就把代码贴在这里好了。

view plaincopy to clipboardprint?

public void DataGridView2Excel(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) 
        { 
          dimArray[i + 1, k] = datagridview.Rows[i].Cells[j].Value.ToString(); 
          k++; 
        } 
      } 
    } 
    ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows + 1, iCols]).Value2 = dimArray; 
    ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iCols]).Font.Bold = true; 
    ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows + 1, iCols]).Font.Size = 10.0; 
    ws.get_Range(ws.Cells[1, 1], 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; 
} 
public void DataGridView2Excel(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)
        {
          dimArray[i + 1, k] = datagridview.Rows[i].Cells[j].Value.ToString();
          k++;
        }
      }
    }
  
    ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows + 1, iCols]).Value2 = dimArray;
    ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iCols]).Font.Bold = true;
    ws.get_Range(ws.Cells[1, 1], ws.Cells[iRows + 1, iCols]).Font.Size = 10.0;
    ws.get_Range(ws.Cells[1, 1], 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;
}

上一页  1 2 

Tags:DataGridView 导出 Excel

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接