为什么导出为Excel文件时,dataGridView中的计算行不会被保存?

时间:2014-05-08 21:39:56

标签: vb.net excel datagridview

我正在处理的代码将XML文件加载到dataTable中并将其显示在dataGridView中。然后,我对dataGridView中的每一列进行平均,并将它们显示为dataGridView中的新行。然后我将dataGridView导出为Excel文件,但它不包括添加的平均行。手动输入到dataGridView的任何输入都会保存在导出中吗?我知道使用SQL数据库,您必须在dataGridView源上调用更新。我不太确定在这种情况下该怎么做。关于如何保存更改的任何想法?

打开XML文件并将其加载到dataGridView

Private Sub ExOpen_Click(sender As Object, e As EventArgs) Handles ExOpen.Click
' Uses a openFileDialog to find the file path for the file the user wishes to
' use. It then displays the path in a textBox. Load a DataTable with the 
' information found in the XML file and then set it as the dataSource for the 
' DataGridView()
OpenFileDialog1.FileName = Nothing
OpenFileDialog1.Filter = "XML|*.xml"
If OpenFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
  lblExFile.Text = OpenFileDialog1.FileName
End If
Dim dtDataTbl As New System.Data.DataTable
Dim dsDataSet As New DataSet
For Each dtDataTbl In dsDataSet.Tables
  dtDataTbl.BeginLoadData()
Next
Try
  dsDataSet.ReadXml(lblExFile.Text)
  For Each dtDataTbl In dsDataSet.Tables
    dtDataTbl.EndLoadData()
  Next
  DataGridView1.DataSource = dtDataTbl
Catch
Finally
  dsDataSet = Nothing
  dtDataTbl = Nothing
End Try
  End Sub

执行列的平均值并将其添加为dataGridView

中的新行
  Private Sub ExAvg_Click(sender As Object, e As EventArgs) Handles ExAvg.Click
' Goes through each column and finds the average.  The averages are printed out
' into the DataGridView.
Dim decAvg As Decimal = 0
Dim sValue As String = Nothing
Dim sAppend As String = Nothing
Dim iCount As Integer = 0
For iColumn As Integer = 1 To DataGridView1.Columns.Count - 1
  iCount = 0
  For iRow As Integer = 0 To DataGridView1.Rows.Count - 1
    sAppend = Nothing
    If Not DataGridView1.Rows(iRow).Cells(iColumn).Value Is Nothing Then
      sValue = CStr(DataGridView1.Rows(iRow).Cells(iColumn).Value)
      Dim cChars() As Char = sValue.ToCharArray()
      ' Each character is only taken if a digit or "."  
      For Each cCharacter In cChars
        If Char.IsDigit(cCharacter) OrElse cCharacter = "." Then
          sAppend &= cCharacter
        End If
      Next
      iCount += 1
      decAvg += CDec(sAppend)
    End If
  Next
  decAvg = Math.Round(decAvg / iCount, 2)
  ' Print the average row in the DataGridView.
  Try
    If iColumn = 1 Then
      DataGridView1.Rows(iCount).Cells(iColumn).Value = "$" & CStr(decAvg)
    Else
      DataGridView1.Rows(iCount).Cells(iColumn).Value = CStr(decAvg)
    End If
    DataGridView1.Rows(iCount).Cells(0).Value = "Average"
  Catch
  End Try
Next
DataGridView1.v()
  End Sub

将dataGridView导出为Excel文件(缺少添加的平均行)

Private Sub ExSave_Click(sender As Object, e As EventArgs) Handles ExSave.Click
' Information from the DataGridView is transferred into an excel object
' row by row and column by column.  The file name is picked by the user
' using a saveFileDialog.
SaveFileDialog1.FileName = Nothing
SaveFileDialog1.Filter = "Excel File|*.xls"
If SaveFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
  lblExFile.Text = SaveFileDialog1.FileName
End If

Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value

xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = CType(xlWorkBook.Worksheets(1), Excel.Worksheet)

Dim dc As DataColumn
Dim dr As DataRow
Dim dt As System.Data.DataTable
dt = CType(DataGridView1.DataSource, System.Data.DataTable)
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
Try
  'Export the Columns to excel file
  For Each dc In dt.Columns
    colIndex = colIndex + 1
    xlWorkSheet.Cells(1, colIndex) = dc.ColumnName
  Next

  'Export the rows to excel file
  For Each dr In dt.Rows
    rowIndex = rowIndex + 1
    colIndex = 0
    For Each dc In dt.Columns
      colIndex = colIndex + 1
      xlWorkSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
    Next
  Next

  xlWorkSheet.Columns.AutoFit()
  xlWorkBook.SaveAs(lblExFile.Text, XlFileFormat.xlWorkbookNormal, Type.Missing, _
                    Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, _
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
  xlWorkBook.Close()

1 个答案:

答案 0 :(得分:0)

您正在向GridView添加平均值,但将数据表写入Excel。