Excel接入deepseek

先进入deepseek官网:DeepSeek | 深度求索

点击API开放平台:

确保余额里有钱:

创建APIkey:

复制到.txt文件中储存好


插入VBA代码:

vbnet 复制代码
Function OptimizeEbayTitle(originalTitle As String) As String
    Dim Prompt As String
    Prompt = "作为专业eBay运营人员,请优化以下标题:[[" & originalTitle & "]]" & vbCrLf & _
             "要求:" & vbCrLf & _
             "1. 控制在80个字符以内" & vbCrLf & _
             "2. 保留核心信息" & vbCrLf & _
             "3. 直接输出优化结果,不加额外说明或符号"
             
    OptimizeEbayTitle = AskAI(Prompt)
    
    ' 如果结果包含引号,移除它们
    OptimizeEbayTitle = Replace(Replace(OptimizeEbayTitle, """", ""), """, "")
End Function

Function AskAI(Prompt As String) As String
    Dim jsonResponse As String
    Dim contentStart As Long
    Dim contentEnd As Long
    Dim contentText As String
    
    ' 获取API原始响应
    jsonResponse = DeepSeek_Query(Prompt)
    
    ' 检查是否有错误
    If Left(jsonResponse, 5) = "Error" Or Left(jsonResponse, 5) = "HTTP" Then
        AskAI = jsonResponse ' 直接返回错误信息
        Exit Function
    End If
    
    ' 尝试定位content字段
    contentStart = InStr(1, jsonResponse, """content"":""") + Len("""" & "content" & """:""")
    
    If contentStart > Len("""" & "content" & """:""") Then
        ' 查找content结束位置
        contentEnd = InStr(contentStart, jsonResponse, """")
        
        If contentEnd > contentStart Then
            ' 提取内容
            contentText = Mid(jsonResponse, contentStart, contentEnd - contentStart)
            
            ' 反转义特殊字符
            contentText = Replace(contentText, "\""", """")   ' 双引号
            contentText = Replace(contentText, "\n", vbCrLf)  ' 换行符
            contentText = Replace(contentText, "\\", "\")     ' 反斜杠
            
            AskAI = contentText
            Exit Function
        End If
    End If
    
    ' 如果无法解析,返回原始JSON的前100字符
    AskAI = "无法解析响应: " & Left(jsonResponse, 100)
End Function

' 原始API调用函数(保持不变)
Function DeepSeek_Query(Prompt As String) As String
    Dim Http As Object
    Dim Url As String, APIKey As String
    Dim Body As String
    
    APIKey = "" ' 替换为真实API密钥
    Url = "https://api.deepseek.com/v1/chat/completions"
    
    Set Http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    
    On Error GoTo ErrorHandler
    
    ' 特殊字符转义处理
    Dim SafePrompt As String
    SafePrompt = Replace(Prompt, """", "\""")
    SafePrompt = Replace(SafePrompt, vbCrLf, "\n")
    SafePrompt = Replace(SafePrompt, "\", "\\")
    
    Body = "{""model"":""deepseek-chat"",""messages"":[{""role"":""user"",""content"":""" & SafePrompt & """}]}"
    
    Http.Open "POST", Url, False
    Http.setRequestHeader "Content-Type", "application/json"
    Http.setRequestHeader "Authorization", "Bearer " & APIKey
    Http.send Body
    
    If Http.Status <> 200 Then
        DeepSeek_Query = "HTTP错误 " & Http.Status & ": " & Http.statusText
        Exit Function
    End If
    
    DeepSeek_Query = Http.responseText
    Exit Function
    
ErrorHandler:
    DeepSeek_Query = "VBA错误: " & Err.Description
End Function

效果展示:

相关推荐
LAM LAB5 天前
【VBA】Excel指定单元格范围内字体设置样式,处理导出课表单元格
excel·vba
在这habit之下5 天前
Keepalived学习总结
excel
Youngchatgpt6 天前
如何在 Excel 中使用 ChatGPT:自动化任务和编写公式
人工智能·chatgpt·自动化·excel
开开心心就好6 天前
安卓开源应用,超时提醒紧急人护独居安全
windows·决策树·计算机视觉·pdf·计算机外设·excel·动态规划
D_C_tyu6 天前
Vue3 + Element Plus | el-table 多级表头表格导出 Excel(含合并单元格、单元格居中)第二版
vue.js·elementui·excel
骆驼爱记录6 天前
WPS页码设置:第X页共Y-1页
自动化·word·excel·wps·新人首发
Cxiaomu7 天前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
2501_930707787 天前
如何使用C#代码从 PDF 中提取表格并另存为Excel文件
pdf·excel
pacong7 天前
B生所学EXCEL
人工智能·excel