之前跟大家分享了, 如何將Deepseek嵌入Word,有粉絲就問道如何將Deepseek嵌入到Excel呢?這不,今天方法就來了,跟嵌入Word的使用方法類似

一、使用方法
先來簡單地說下使用的方法,操作非常的簡單,跟嵌入Word類似
首先我們需要先選中對應(yīng)的數(shù)據(jù)區(qū)域,然后在上方點(diǎn)擊Deepseek,最后會(huì)跳出窗口,在窗口中提出問題,等待一段時(shí)間后就能得到對應(yīng)的結(jié)果了,下面來看下如何構(gòu)建這個(gè)效果

二、代碼準(zhǔn)備
首先需要復(fù)制下方的代碼,關(guān)鍵點(diǎn)是需要修改API為自己的API,如何獲取API的話,大家可以翻下之前的文章,是需要在Deepseek的官網(wǎng)獲取的。
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.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 "請先設(shè)置API密鑰", vbExclamation
Exit Sub
End If
On Error Resume Next
Set selectedRange = Selection
On Error GoTo 0
If selectedRange Is Nothing Then
MsgBox "請先選擇要處理的數(shù)據(jù)區(qū)域", vbExclamation
Exit Sub
End If
userQuestion = InputBox("請輸入您的問題(選中的單元格內(nèi)容將作為處理數(shù)據(jù)):", "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
' 組合問題和單元格內(nèi)容
combinedInput = userQuestion & vbCrLf & vbCrLf & "數(shù)據(jù)內(nèi)容:" & vbCrLf & cell.Value
' 轉(zhuǎn)義特殊字符
combinedInput = Replace(combinedInput, "\", "\\")
combinedInput = Replace(combinedInput, """", "\""")
combinedInput = Replace(combinedInput, vbCrLf, "\n")
combinedInput = Replace(combinedInput, vbCr, "\n")
combinedInput = Replace(combinedInput, vbLf, "\n")
' 調(diào)用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)
' 處理轉(zhuǎn)義字符
outputText = Replace(outputText, "\""", """")
outputText = Replace(outputText, "\\", "\")
outputText = Replace(outputText, "\n", vbCrLf)
' 寫入右側(cè)相鄰單元格
cell.Offset(0, 1).Value = outputText
Else
cell.Offset(0, 1).Value = "解析失敗"
End If
Else
cell.Offset(0, 1).Value = "API錯(cuò)誤"
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 "請先設(shè)置API密鑰", vbExclamation Exit Sub End If On Error Resume Next Set selectedRange = Selection On Error GoTo 0 If selectedRange Is Nothing Then MsgBox "請先選擇要處理的數(shù)據(jù)區(qū)域", vbExclamation Exit Sub End If userQuestion = InputBox("請輸入您的問題(選中的單元格內(nèi)容將作為處理數(shù)據(jù)):", "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 ' 組合問題和單元格內(nèi)容 combinedInput = userQuestion & vbCrLf & vbCrLf & "數(shù)據(jù)內(nèi)容:" & vbCrLf & cell.Value ' 轉(zhuǎn)義特殊字符 combinedInput = Replace(combinedInput, "\", "\\") combinedInput = Replace(combinedInput, """", "\""") combinedInput = Replace(combinedInput, vbCrLf, "\n") combinedInput = Replace(combinedInput, vbCr, "\n") combinedInput = Replace(combinedInput, vbLf, "\n") ' 調(diào)用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) ' 處理轉(zhuǎn)義字符 outputText = Replace(outputText, "\""", """") outputText = Replace(outputText, "\\", "\") outputText = Replace(outputText, "\n", vbCrLf) ' 寫入右側(cè)相鄰單元格 cell.Offset(0, 1).Value = outputText Else cell.Offset(0, 1).Value = "解析失敗" End If Else cell.Offset(0, 1).Value = "API錯(cuò)誤" End If End If Next cell Application.ScreenUpdating = True MsgBox "處理完成!", vbInformation Set regex = Nothing Set selectedRange = NothingEnd Sub
三、代碼粘貼
在Excel中點(diǎn)擊【開發(fā)工具】然后點(diǎn)擊【Visiual Basic】進(jìn)入編輯窗口,在右側(cè)空白區(qū)域點(diǎn)擊鼠標(biāo)右鍵找到插入,找到【模塊】,然后在右側(cè)的窗口那里直接粘貼即可
在這里一定記得,API替換為自己的API

四、制作按鈕
需要在右側(cè)點(diǎn)擊文件,然后最下放找到【選項(xiàng)】來調(diào)出Excel選項(xiàng),在Excel選項(xiàng)中找到【自定義功能區(qū)】
我們需要在左側(cè)將類別設(shè)置【宏】選中【DEEPSeekExcel】這個(gè)宏,然后在右側(cè)的窗口中點(diǎn)擊對應(yīng)的選項(xiàng)卡,最后點(diǎn)擊添加,即可將宏作為按鈕添加到Excel表格中,至此就設(shè)置完畢了

五、加載宏
如果想要將這個(gè)宏按鈕永久的保留在Excel中是需要使用加載宏的,之前發(fā)過,大家可以搜一下
DeepSeek搭配Excel,制作自定義按鈕,實(shí)現(xiàn)辦公自動(dòng)化!
視頻Excel從零到一
熱門跟貼