我的任务是将数据导出到 xls/ xlsx/ xlsm 文件。我成功完成了 xlsx 和 xlsm 文件,但遇到了 xls 问题。
我有带有模板的示例 xls 文件
我正在分享代码。
在这里,GetXLFile 方法使用 SaveFileDialog,它允许用户选择扩展名并选择 xls 文件的位置以导出数据。然后它将给定的文件名和路径保存到“xlFile”字符串。之后,它将模板化的示例 xls 文件复制到 xlFile。你可以在这里找到它的代码。
FillDGVDataToDataTbl 将 datagridview 的数据填充到名为“dataTbl”的数据表中。
在此之后,我将 dataTbl 的数据导出到 xlFile 的选定范围内。“InsertRecords”将数据插入每一行,“InsertColumns”在模板之后的工作表第一行中插入列。
Public Sub ExporttoExcelFile(ByVal dgv As DataGridView)
Dim dataTbl As New System.Data.DataTable
Dim xlFile As String = String.Empty
GetXLFile(xlFile)
FilDGVDataToDataTbl(dgv, dataTbl)
Dim ext As String = Path.GetExtension(xlFile)
If ext.Equals(".xlsx") Or ext.Equals(".xlsm") Then
connStr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = '" & xlFile & "';" &
" Extended Properties = 'Excel 12.0; HDR = NO; MAXSCANROWS = 0';"
ElseIf ext.Equals(".xls") Then
connStr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = '" & xlFile & "';" &
" Extended Properties = 'Excel 8.0; HDR = NO; MAXSCANROWS = 0';"
End If
connStr = String.Format(connStr)
Try
conn = New OleDb.OleDbConnection(connStr)
conn.Open()
cmd = New OleDbCommand()
cmd.Connection = conn
Dim Tbls = conn.GetSchema("Tables", Nothing)
Dim xlSheet As String
xlSheet = Tbls.Rows.Item(0)(2).ToString '("TABLE_NAME").ToString()
' If excelSheets(0).EndsWith("$") Then
Dim lastCol As Char = ChrW(65 + dataTbl.Columns.Count - 1)
xlSheet = xlSheet.Replace("'", "")
InsertRecords(dataTbl, xlSheet, lastCol)
Catch ex As Runtime.Exception
MsgBox(resManager.GetString("ErrorOccurredWhileExportingPartsListToExcel"), , Me.Text)
Catch ex As System.Exception
MsgBox(resManager.GetString("ErrorOccurredWhileExportingPartsListToExcel"), , Me.Text)
Finally
conn.Close()
conn.Dispose()
End Try
End Sub
Private Sub InsertRecords(ByVal dataTbl As System.Data.DataTable, ByVal xlSheet As String, ByVal lastCol As Char)
InsertColumns(xlSheet, dataTbl, lastCol)
For i = 0 To dataTbl.Rows.Count - 1
query = $"INSERT INTO [{xlSheet}A{i + 3}:{lastCol}{i + 3}] VALUES ("
For j = 0 To d![96102-desired-view.png][2]ataTbl.Columns.Count - 1
If j = dataTbl.Columns.Count - 1 Then
query += " ? );"
Else
query += " ?,"
End If
Next
cmd.CommandText = query
For j = 0 To dataTbl.Columns.Count - 1
Dim str As String = dataTbl.Rows(i)(j).ToString()
cmd.Parameters.AddWithValue("@p" & j, str)
Next
Try
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(resManager.GetString("ErrorOccurredWhileExportingPartsListToExcel"), , Me.Text)
Catch ex As System.Exception
MsgBox(resManager.GetString("ErrorOccurredWhileExportingPartsListToExcel"), , Me.Text)
End Try
Next
End Sub
Private Sub InsertColumns(ByVal xlSheet As String, ByVal dataTbl As System.Data.DataTable, ByVal lastCol As Char)
Dim columnNames As String = ""
query = $"INSERT INTO [{xlSheet}A2:{lastCol}2] VALUES ("
For i = 0 To dataTbl.Columns.Count - 1
If i = dataTbl.Columns.Count - 1 Then
query += " ? );"
Else
query += " ?,"
End If
Next
cmd.CommandText = query
If dataTbl.Columns.Count <> 0 Then
For i = 0 To dataTbl.Columns.Count - 1
Dim str As String = dataTbl.Columns(i).ToString()
cmd.Parameters.AddWithValue("@p" & i, str)
Next
Try
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Catch ex As Runtime.Exception
MsgBox(resManager.GetString("ErrorOccurredWhileExportingPartsListToExcel"), , Me.Text)
Catch ex As System.Exception
MsgBox(resManager.GetString("ErrorOccurredWhileExportingPartsListToExcel"), , Me.Text)
End Try
End If
End Sub
所有这些都运行良好,但视图存在一些问题。
这是所需的视图。
但是,当我打开 xlFile 时,它看起来像这样。
(请注意第一行“公司名称”的字体和B、F、H列合并单元格的颜色。这些列的标题字体也发生了变化。)
我没有对 xlFile 进行任何其他更改。谁能告诉我如何获得所需的视图?
提前致谢。