之前跟大家分享了, 如何将Deepseek嵌入Word,有粉丝就问道如何将Deepseek嵌入到Excel呢?这不,今天方法就来了,跟嵌入Word的使用方法类似
一、使用方法
先来简单地说下使用的方法,操作非常的简单,跟嵌入Word类似
首先我们需要先选中对应的数据区域,然后在上方点击Deepseek,最后会跳出窗口,在窗口中提出问题,等待一段时间后就能得到对应的结果了,下面来看下如何构建这个效果
二、代码准备
首先需要复制下方的代码,关键点是需要修改API为自己的API,如何获取API的话,大家可以翻下之前的文章,是需要在Deepseek的官网获取的。
api_key = "你的api"
在这里将你的api直接替换为deepseek的api秘钥即可
Function CallDeepSeekAPI(api_key As String, inputText As String) As String
Dim API As String
Dim SendTxt As String
Dim Http As Object
Dim status_code As Integer
Dim response As String
API = "https://api.deepseek.com/chat/completions"
SendTxt = "{""model"": ""deepseek-chat"", ""messages"": [{""role"":""system"", ""content"":""You are a Excel assistant""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}"
Set Http = CreateObject("MSXML2.XMLHTTP")
With Http
.Open "POST", API, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Bearer " & api_key
.send SendTxt
status_code = .Status
response = .responseText
End With
If status_code = 200 Then
CallDeepSeekAPI = response
Else
CallDeepSeekAPI = "Error: " & status_code & " - " & response
End If
Set Http = Nothing
End Function
Sub DeepSeekExcel()
Dim api_key As String
Dim userQuestion As String
Dim selectedRange As Range
Dim cell As Range
Dim combinedInput As String
Dim response As String
Dim regex As Object
Dim matches As Object
api_key = "你的api"
If api_key = "" Then
MsgBox "请先设置API密钥", vbExclamation
Exit Sub
End If
On Error Resume Next
Set selectedRange = Selection
On Error GoTo 0
If selectedRange Is Nothing Then
MsgBox "请先选择要处理的数据区域", vbExclamation
Exit Sub
End If
userQuestion = InputBox("请输入您的问题(选中的单元格内容将作为处理数据):", "DeepSeek 提问")
If userQuestion = "" Then Exit Sub
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = """content"":""(.*?)"""
regex.Global = False
regex.MultiLine = True
Application.ScreenUpdating = False
For Each cell In selectedRange
If Trim(cell.Value) <> "" Then
' 组合问题和单元格内容
combinedInput = userQuestion & vbCrLf & vbCrLf & "数据内容:" & vbCrLf & cell.Value
' 转义特殊字符
combinedInput = Replace(combinedInput, "\", "\\")
combinedInput = Replace(combinedInput, """", "\""")
combinedInput = Replace(combinedInput, vbCrLf, "\n")
combinedInput = Replace(combinedInput, vbCr, "\n")
combinedInput = Replace(combinedInput, vbLf, "\n")
' 调用API
response = CallDeepSeekAPI(api_key, combinedInput)
If Left(response, 5) <> "Error" Then
Set matches = regex.Execute(response)
If matches.Count > 0 Then
Dim outputText As String
outputText = matches(0).SubMatches(0)
' 处理转义字符
outputText = Replace(outputText, "\""", """")
outputText = Replace(outputText, "\\", "\")
outputText = Replace(outputText, "\n", vbCrLf)
' 写入右侧相邻单元格
cell.Offset(0, 1).Value = outputText
Else
cell.Offset(0, 1).Value = "解析失败"
End If
Else
cell.Offset(0, 1).Value = "API错误"
End If
End If
Next cell
Application.ScreenUpdating = True
MsgBox "处理完成!", vbInformation
Set regex = Nothing
Set selectedRange = Nothing
End Sub
Function CallDeepSeekAPI(api_key As String, inputText As String) As String Dim API As String Dim SendTxt As String Dim Http As Object Dim status_code As Integer Dim response As String API = "https://api.deepseek.com/chat/completions" SendTxt = "{""model"": ""deepseek-chat"", ""messages"": [{""role"":""system"", ""content"":""You are a Excel assistant""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}" Set Http = CreateObject("MSXML2.XMLHTTP") With Http .Open "POST", API, False .setRequestHeader "Content-Type", "application/json" .setRequestHeader "Authorization", "Bearer " & api_key .send SendTxt status_code = .Status response = .responseText End With If status_code = 200 Then CallDeepSeekAPI = response Else CallDeepSeekAPI = "Error: " & status_code & " - " & response End If Set Http = NothingEnd FunctionSub DeepSeekExcel() Dim api_key As String Dim userQuestion As String Dim selectedRange As Range Dim cell As Range Dim combinedInput As String Dim response As String Dim regex As Object Dim matches As Object api_key = "你的api" If api_key = "" Then MsgBox "请先设置API密钥", vbExclamation Exit Sub End If On Error Resume Next Set selectedRange = Selection On Error GoTo 0 If selectedRange Is Nothing Then MsgBox "请先选择要处理的数据区域", vbExclamation Exit Sub End If userQuestion = InputBox("请输入您的问题(选中的单元格内容将作为处理数据):", "DeepSeek 提问") If userQuestion = "" Then Exit Sub Set regex = CreateObject("VBScript.RegExp") regex.Pattern = """content"":""(.*?)""" regex.Global = False regex.MultiLine = True Application.ScreenUpdating = False For Each cell In selectedRange If Trim(cell.Value) <> "" Then ' 组合问题和单元格内容 combinedInput = userQuestion & vbCrLf & vbCrLf & "数据内容:" & vbCrLf & cell.Value ' 转义特殊字符 combinedInput = Replace(combinedInput, "\", "\\") combinedInput = Replace(combinedInput, """", "\""") combinedInput = Replace(combinedInput, vbCrLf, "\n") combinedInput = Replace(combinedInput, vbCr, "\n") combinedInput = Replace(combinedInput, vbLf, "\n") ' 调用API response = CallDeepSeekAPI(api_key, combinedInput) If Left(response, 5) <> "Error" Then Set matches = regex.Execute(response) If matches.Count > 0 Then Dim outputText As String outputText = matches(0).SubMatches(0) ' 处理转义字符 outputText = Replace(outputText, "\""", """") outputText = Replace(outputText, "\\", "\") outputText = Replace(outputText, "\n", vbCrLf) ' 写入右侧相邻单元格 cell.Offset(0, 1).Value = outputText Else cell.Offset(0, 1).Value = "解析失败" End If Else cell.Offset(0, 1).Value = "API错误" End If End If Next cell Application.ScreenUpdating = True MsgBox "处理完成!", vbInformation Set regex = Nothing Set selectedRange = NothingEnd Sub
三、代码粘贴
在Excel中点击【开发工具】然后点击【Visiual Basic】进入编辑窗口,在右侧空白区域点击鼠标右键找到插入,找到【模块】,然后在右侧的窗口那里直接粘贴即可
在这里一定记得,API替换为自己的API
四、制作按钮
需要在右侧点击文件,然后最下放找到【选项】来调出Excel选项,在Excel选项中找到【自定义功能区】
我们需要在左侧将类别设置【宏】选中【DEEPSeekExcel】这个宏,然后在右侧的窗口中点击对应的选项卡,最后点击添加,即可将宏作为按钮添加到Excel表格中,至此就设置完毕了
五、加载宏
如果想要将这个宏按钮永久的保留在Excel中是需要使用加载宏的,之前发过,大家可以搜一下
DeepSeek搭配Excel,制作自定义按钮,实现办公自动化!
视频Excel从零到一
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.