有人可以帮我解决如何将数据从datagridview导入Microsoft Excel以及列名和数据。另一件事是日期成为主题标签。我必须扩展列以显示日期。
我有一个按钮,当我按下它时,Microsoft excel应该打开导出的数据。
答案 0 :(得分:-1)
Excel方法
此方法与您将看到的许多方法不同。其他人使用循环来编写每个单元格并使用文本数据类型写入单元格。
此方法从DataTable
或DataGridView
创建一个对象数组,然后将该数组写入Excel。这意味着我可以在没有循环的情况下写入Excel并保留数据类型。
我从我的库中提取了这个,我认为我改变了它只能使用这个代码,但可能需要更多的小调整。如果您收到错误,请告诉我,我会为您纠正错误。通常,我创建一个类的实例并调用这些方法。如果您想使用我的图书馆,请使用此链接进行下载,如果您需要帮助,请告诉我 https://zomp.co/Files.aspx?ID=zExcel
将代码复制到解决方案后,您将使用它。
在按钮代码中添加此项并将名称更改为控件。
WriteDataGrid("Sheet1", grid)
导出后打开文件使用此行
System.Diagnostics.Process.Start("The location and filename of your file")
在WriteArray
方法中,您需要将保存工作簿的行更改为要保存的位置。将此作为参数添加可能是有意义的。
wb.SaveAs("C:\MyWorkbook.xlsx")
Public Function WriteArray(Sheet As String, ByRef ObjectArray As Object(,)) As String
Try
Dim xl As Excel.Application = New Excel.Application
Dim wb As Excel.Workbook = xl.Workbooks.Add()
Dim ws As Excel.Worksheet = wb.Worksheets.Add()
ws.Name = Sheet
Dim range As Excel.Range = ws.Range("A1").Resize(ObjectArray.GetLength(0), ObjectArray.GetLength(1))
range.Value = ObjectArray
range = ws.Range("A1").Resize(1, ObjectArray.GetLength(1) - 1)
range.Interior.Color = RGB(0, 70, 132) 'Con-way Blue
range.Font.Color = RGB(Drawing.Color.White.R, Drawing.Color.White.G, Drawing.Color.White.B)
range.Font.Bold = True
range.WrapText = True
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
range.Application.ActiveWindow.SplitColumn = 0
range.Application.ActiveWindow.SplitRow = 1
range.Application.ActiveWindow.FreezePanes = True
wb.SaveAs("C:\MyWorkbook.xlsx")
wb.CLose()
xl.Quit()
xl = Nothing
wb = Nothing
ws = Nothing
range = Nothing
ReleaseComObject(xl)
ReleaseComObject(wb)
ReleaseComObject(ws)
ReleaseComObject(range)
Return ""
Catch ex As Exception
Return "WriteArray()" & Environment.NewLine & Environment.NewLine & ex.Message
End Try
End Function
Public Function WriteDataGrid(SheetName As String, ByRef dt As DataGridView) As String
Try
Dim l(dt.Rows.Count + 1, dt.Columns.Count) As Object
For c As Integer = 0 To dt.Columns.Count - 1
l(0, c) = dt.Columns(c).HeaderText
Next
For r As Integer = 1 To dt.Rows.Count
For c As Integer = 0 To dt.Columns.Count - 1
l(r, c) = dt.Rows(r - 1).Cells(c)
Next
Next
Dim errors As String = WriteArray(SheetName, l)
If errors <> "" Then
Return errors
End If
Return ""
Catch ex As Exception
Return "WriteDataGrid()" & Environment.NewLine & Environment.NewLine & ex.Message
End Try
End Function
Public Function WriteDataTable(SheetName As String, ByRef dt As DataTable) As String
Try
Dim l(dt.Rows.Count + 1, dt.Columns.Count) As Object
For c As Integer = 0 To dt.Columns.Count - 1
l(0, c) = dt.Columns(c).ColumnName
Next
For r As Integer = 1 To dt.Rows.Count
For c As Integer = 0 To dt.Columns.Count - 1
l(r, c) = dt.Rows(r - 1).Item(c)
Next
Next
Dim errors As String = WriteArray(SheetName, l)
If errors <> "" Then
Return errors
End If
Return ""
Catch ex As Exception
Return "WriteDataTable()" & Environment.NewLine & Environment.NewLine & ex.Message
End Try
End Function
我实际上并没有在我的数据库程序中使用这个方法,因为当你有很多行/列时它是一个很慢的方法。我改为从DataGridView创建一个CSV。使用Excel Automation写入Excel仅在需要格式化数据和单元格时才有用,否则应使用CSV。您可以使用图像后的代码进行CSV导出。
CSV方法
Private Sub DataGridToCSV(ByRef dt As DataGridView, Qualifier As String)
Dim TempDirectory As String = "A temp Directory"
System.IO.Directory.CreateDirectory(TempDirectory)
Dim oWrite As System.IO.StreamWriter
Dim file As String = System.IO.Path.GetRandomFileName & ".csv"
oWrite = IO.File.CreateText(TempDirectory & "\" & file)
Dim CSV As StringBuilder = New StringBuilder()
Dim i As Integer = 1
Dim CSVHeader As StringBuilder = New StringBuilder()
For Each c As DataGridViewColumn In dt.Columns
If i = 1 Then
CSVHeader.Append(Qualifier & c.HeaderText.ToString() & Qualifier)
Else
CSVHeader.Append("," & Qualifier & c.HeaderText.ToString() & Qualifier)
End If
i += 1
Next
'CSV.AppendLine(CSVHeader.ToString())
oWrite.WriteLine(CSVHeader.ToString())
oWrite.Flush()
For r As Integer = 0 To dt.Rows.Count - 1
Dim CSVLine As StringBuilder = New StringBuilder()
Dim s As String = ""
For c As Integer = 0 To dt.Columns.Count - 1
If c = 0 Then
'CSVLine.Append(Qualifier & gridResults.Rows(r).Cells(c).Value.ToString() & Qualifier)
s = s & Qualifier & gridResults.Rows(r).Cells(c).Value.ToString() & Qualifier
Else
'CSVLine.Append("," & Qualifier & gridResults.Rows(r).Cells(c).Value.ToString() & Qualifier)
s = s & "," & Qualifier & gridResults.Rows(r).Cells(c).Value.ToString() & Qualifier
End If
Next
oWrite.WriteLine(s)
oWrite.Flush()
'CSV.AppendLine(CSVLine.ToString())
'CSVLine.Clear()
Next
'oWrite.Write(CSV.ToString())
oWrite.Close()
oWrite = Nothing
System.Diagnostics.Process.Start(TempDirectory & "\" & file)
GC.Collect()
End Sub
答案 1 :(得分:-1)
这是您可以尝试的另一个解决方案,它使用GemBox.Spreadsheet库,它将保留数据类型和DataGridView中使用的格式:
Dim ef As New ExcelFile()
Dim ws = ef.Worksheets.Add("DGV Sheet")
' From DataGridView to ExcelFile.
DataGridViewConverter.ImportFromDataGridView(ws, dataGridView1,
New ImportFromDataGridViewOptions() With {.ColumnHeaders = True})
' Auto fit excel columns.
Dim count As Integer = ws.CalculateMaxUsedColumns()
For index As Integer = 0 To count - 1
ws.Columns(index).AutoFit()
Next
ef.Save("DGV Book.xlsx")
另外here你可以找到另一个样本。