.NET(VB,C#)导出DATAGRIDVIEW到EXCEL中

来源:互联网 发布:一个域名对应多个ip 编辑:程序博客网 时间:2024/05/17 21:57

    引用EXCEL库就不说了,直接贴上代码

c#:

  1.         #region 导出DataGridView中数据为EXCEL文件
  2.         public Boolean OutExcel(DataGridView grView, string sPath, string exName)
  3.         {
  4.             //函数说明:导出DataGridView中数据为EXCEL文件
  5.             //输入参数:grView-一个DtaGridView对象;sPath-需要存放的路径;exName-导出的EXCEL文件中的表名
  6.             //返回:True-成功;False-失败
  7.             // 创建Excel对象                    
  8.             Excel.Application xlExcel = new Excel.ApplicationClass();
  9.             if (xlExcel == null)
  10.             {
  11.                 MessageBox.Show("Excel无法启动");
  12.                 return false;
  13.             }
  14.             // 创建Excel工作薄
  15.             Excel.Workbook xlBook = xlExcel.Workbooks.Add(true);
  16.             Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];
  17.             Excel.Range range = xlSheet.get_Range(xlExcel.Cells[1, 1], xlExcel.Cells[1, grView.ColumnCount]);
  18.             // 设置标题
  19.             if (exName != "")
  20.             {
  21.                 range.MergeCells = true;//合并单元格
  22.                 xlExcel.ActiveCell.FormulaR1C1 = exName;
  23.                 xlExcel.ActiveCell.Font.Size = 20;
  24.                 xlExcel.ActiveCell.Font.Bold = true;
  25.                 xlExcel.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
  26.             }
  27.             // 列索引,行索引,总列数,总行数
  28.             int colIndex = 0;
  29.             int RowIndex = 0;
  30.             int colCount = grView.ColumnCount;
  31.             int RowCount = grView.Rows.Count;
  32.             // 创建缓存数据
  33.             object[,] objData = new object[RowCount + 1, colCount - 1];
  34.             // 获取列标题
  35.             for (int i = 1; i < colCount; i++)
  36.             {
  37.                 objData[0, i - 1] = grView.Columns[i].HeaderCell.Value;
  38.             }
  39.             // 获取数据
  40.             for (RowIndex = 1; RowIndex <= RowCount; RowIndex++)
  41.             {
  42.                 for (colIndex = 0; colIndex < colCount - 1; colIndex++)
  43.                 {
  44.                     objData[RowIndex, colIndex] = grView.Rows[RowIndex - 1].Cells[colIndex + 1].Value.ToString();
  45.                 }
  46.                 Application.DoEvents();
  47.             }
  48.             // 写入Excel
  49.             xlExcel.get_Range(xlExcel.Cells[2, 1], xlExcel.Cells[2, colIndex]).Font.Bold = true;
  50.             range = xlSheet.get_Range(xlExcel.Cells[2, 1], xlExcel.Cells[RowCount + 2, colCount - 1]);
  51.             range.Value2 = objData;
  52.             // 保存
  53.             try
  54.             {
  55.                 xlExcel.Cells.EntireColumn.AutoFit();
  56.                 xlExcel.Cells.VerticalAlignment = Excel.Constants.xlCenter;
  57.                 xlExcel.Cells.HorizontalAlignment = Excel.Constants.xlCenter;
  58.                 //xlExcel.Visible   =   true;   
  59.                 xlBook.Saved = true;
  60.                 xlBook.SaveCopyAs(sPath);
  61.                 return true;
  62.             }
  63.             catch
  64.             {
  65.                 MessageBox.Show("导出EXCEL文件失败!");
  66.                 return false;
  67.             }
  68.             finally
  69.             {
  70.                 xlExcel.Quit();
  71.                 GC.Collect();
  72.                 KillProcess("excel");
  73.             }
  74.         }
  75.         #endregion
  76.         #region 杀死进程
  77.         public void KillProcess(string processName)
  78.         {
  79.             System.Diagnostics.Process myproc = new System.Diagnostics.Process();
  80.             //得到所有打开的进程 
  81.             try
  82.             {
  83.                 foreach (Process thisproc in Process.GetProcessesByName(processName))
  84.                 {
  85.                     thisproc.Kill();
  86.                 }
  87.             }
  88.             catch (Exception Exc)
  89.             {
  90.                 throw new Exception("", Exc);
  91.             }
  92.         }
  93.   #endregion 

VB.NET:

  1. Private Sub CmdExcel_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles CmdExcel.Click
  2.         '函数说明:导出网格中数据为EXCEL文件
  3.         Dim VBExcel As Microsoft.Office.Interop.Excel.Application
  4.         Dim ExBook As Microsoft.Office.Interop.Excel.Workbook
  5.         Dim ExSheet As Microsoft.Office.Interop.Excel.Worksheet
  6.         Dim ExeclArray As Object
  7.         Dim sPath As String
  8.         Dim I As Object
  9.         Dim J As Short
  10.         Me.Cursor = Cursors.WaitCursor
  11.         SvFlg.Filter = "Excel文件(*.xls)|*.xls"
  12.         SvFlg.ShowDialog()
  13.         sPath = SvFlg.FileName
  14.         If sPath = "" Then
  15.             Me.Cursor = Cursors.AppStarting
  16.             Exit Sub
  17.         End If
  18.         VBExcel = CreateObject("Excel.Application")
  19.         ExBook = VBExcel.Workbooks.Add
  20.         ExSheet = ExBook.Worksheets(1)
  21.         Try
  22.             ReDim ExeclArray(Msfg.Rows, Msfg.Cols)
  23.             VBExcel.ScreenUpdating = False
  24.             For I = 0 To Msfg.Rows - 1
  25.                 For J = 0 To Msfg.Cols - 1
  26.                     ExeclArray(I + 1, J + 1) = Msfg.get_TextMatrix(I, J)
  27.                 Next
  28.             Next
  29.             ExSheet.Columns._Default("B:B").ColumnWidth = 17
  30.             '从第一行第一列开始
  31.             With ExSheet
  32.                 .Range(.Cells._Default(1, 1), .Cells._Default(Msfg.Rows, Msfg.Cols + 1)).Value = ExeclArray
  33.             End With
  34.             ExSheet.SaveAs(sPath)
  35.             VBExcel.Quit()
  36.             VBExcel = Nothing
  37.             ExBook = Nothing
  38.             ExSheet = Nothing
  39.             Me.Cursor = Cursors.AppStarting
  40.             MessageBox.Show("导出报表成功!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
  41.         Catch ex As Exception
  42.             VBExcel.Quit()
  43.             VBExcel = Nothing
  44.             ExBook = Nothing
  45.             ExSheet = Nothing
  46.             Me.Cursor = Cursors.AppStarting
  47.             MessageBox.Show("导出报表失败!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
  48.         End Try
  49.     End Sub
原创粉丝点击