json - 将记录从 Excel 文件上传到服务器 - 桌面应用程序
问题描述
我有以下要求:
在我的 PC 的D://驱动器中有一个名为BATCH的文件夹。
现在这个D://BATCH包含任何随机名称的 excel 文件。每个excel文件只有一条记录。
我必须创建 .exe 文件或任何允许用户浏览BATCH文件夹的桌面应用程序,然后可以将 excel 文件中的内容或数据上传到我们的服务器。我们必须将 excel 数据放入JSON中,然后将其上传到服务器。
另一件事是,如果我已经从 3 个文件上传了数据,然后用户再次尝试通过选择文件夹来上传,则应该上传新的或未上传的数据。BATCH 文件夹中的数据每天都会通过连接的机器生成。
我的背景是 Web 和移动开发。因此,如上所述实现这一目标有点困惑。
那么,我该如何实现呢?我必须使用哪些技术或哪些工具有用?
感谢您的支持。
解决方案
可能有很多方法可以给这只猫剥皮。也许将文件夹中所有 Excel 文件中的所有内容导入一个 Excel 文件,然后将其导出到 SQL Server。只是一个想法。
Sub InsertInto()
'Declare some variables
Dim cnn As adodb.Connection
Dim cmd As adodb.Command
Dim strSQL As String
'Create a new Connection object
Set cnn = New adodb.Connection
'Set the connection string
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Northwind;Data Source=Server_Name"
'Create a new Command object
Set cmd = New adodb.Command
'Open the Connection to the database
cnn.Open
'Associate the command with the connection
cmd.ActiveConnection = cnn
'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
cmd.CommandType = adCmdText
'Create the SQL
strSQL = "UPDATE TBL SET JOIN_DT = '2019-12-10' WHERE EMPID = 2"
'Pass the SQL to the Command object
cmd.CommandText = strSQL
'Execute the bit of SQL to update the database
cmd.Execute
'Close the connection again
cnn.Close
'Remove the objects
Set cmd = Nothing
Set cnn = Nothing
End Sub
只是为了稍微修饰一下我的原始答案......您可以使用如下概念轻松地将文件夹中的多个 Excel 文件中的数据导入到一个 Excel 文件中。
Sub Basic_Example_1()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1
'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
With mybook.Worksheets(1)
Set sourceRange = .Range("A1:C1")
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
'Copy the file name in column A
With sourceRange
BaseWks.cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(Fnum)
End With
'Set the destrange
Set destrange = BaseWks.Range("B" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Next Fnum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
有关如何从多个 Excel 文件/源中获取数据、合并到一个文件/源中的一些想法,请参阅下面的链接。
http://www.rondebruin.nl/win/s3/win008.htm
对这类事情保持开放的态度。还有其他几种方法可以做本质上相同的事情。
推荐阅读
- python - 无法选择列名列表并在 Streamlit 中查看这些列中的数据
- css - Removing a break line from the form text input
- configuration - Filebeat 中央管理替代方案
- laravel - 嵌套的雄辩关系
- dll - Uno WASM 在使用 DLL 时无法解析包
- javascript - NW.js 中的 nvm-windows Node.js 版本更改
- javascript - 使用Javascript遍历无序列表
- postgresql - 如何从包含数组的某个键的 jsonb 列中获取 uniq 数据?
- vue.js - 在 Nuxt JS 中重新加载页面后路由参数值消失
- c++ - 如何在 C++ 中调试未捕获的异常核心转储