You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Intouch R2 SP1 SCADA Excel数据导出适配Office2016技术咨询

在Intouch R2 SP1中解决Office 2016环境下Excel报表写入失败问题及替代方案

首先来看你的场景:你正在用Intouch R2 SP1开发SCADA系统,需要生成Excel报表并写入10个值到指定单元格。现有代码在Office 2010环境下运行正常,但部署到Office 2016的机器上后,只有单元格写入操作失效,其他步骤(复制模板、打开Excel)都没问题。下面针对你的两个问题逐一解答:

问题1:如何通过Intouch Archestra Graphics的VB.NET实现数据导出至Excel?

你的现有代码使用了WWPoke函数,这个函数本质是通过Windows消息和Excel进程交互,但Office 2016及后续版本对进程模型、COM对象访问权限做了调整,导致WWPoke无法正确定位工作表或完成数据写入。更可靠的方案是直接使用Excel Interop组件(Microsoft.Office.Interop.Excel)操作Excel对象,这种方式兼容性更强,适配不同Office版本。

实现步骤:

  1. 在Archestra Graphics的VB.NET项目中,添加对Microsoft.Office.Interop.Excel的引用(可通过NuGet安装,或直接引用系统中对应Office版本的Interop库)。
  2. 编写代码完成模板复制、Excel对象初始化、数据写入、保存关闭的完整流程。

示例代码:

Imports Microsoft.Office.Interop.Excel

' 定义核心变量
Dim sourceDir As String = "C:\MIGRA\SCADA\Acesur_Cogeneracion"
Dim destDir As String = "C:\InformesAcesur"
Dim templateFileName As String = "Informe.xls"
' 生成带时间戳的目标文件名
Dim destFileName As String = $"{InTouch:$Day}{InTouch:$Month}{InTouch:$Year}_{InTouch:$Hour}{InTouch:$Minute}.xls"
Dim sourceFile As String = System.IO.Path.Combine(sourceDir, templateFileName)
Dim destFile As String = System.IO.Path.Combine(destDir, destFileName)

' 1. 复制模板文件到目标路径
System.IO.File.Copy(sourceFile, destFile, True)

' 2. 初始化Excel应用对象
Dim excelApp As New Application()
Dim excelWorkbook As Workbook = Nothing
Dim excelWorksheet As Worksheet = Nothing

Try
    ' 打开生成的目标文件
    excelWorkbook = excelApp.Workbooks.Open(destFile)
    ' 获取指定工作表(对应原代码中的Hoja1)
    excelWorksheet = excelWorkbook.Worksheets("Hoja1")

    ' 3. 写入数据到指定单元格
    excelWorksheet.Range("F10").Value = FechaInicio ' 对应原F10C4
    excelWorksheet.Range("F11").Value = FechaFin ' 对应原F11C4
    excelWorksheet.Range("F20").Value = StringFromReal(E, 2, "f") ' 对应原F20C4
    excelWorksheet.Range("F21").Value = StringFromReal(Q, 2, "f") ' 对应原F21C4
    excelWorksheet.Range("F22").Value = StringFromReal(V, 2, "f") ' 对应原F22C4
    excelWorksheet.Range("F28").Value = StringFromReal(REE, 2, "f") ' 对应原F28C4
    excelWorksheet.Range("F34").Value = StringFromReal(FT001, 2, "f") ' 对应原F34C4
    excelWorksheet.Range("F35").Value = StringFromReal(FT002, 2, "f") ' 对应原F35C4
    excelWorksheet.Range("F36").Value = StringFromReal(FT003, 2, "f") ' 对应原F36C4
    excelWorksheet.Range("F37").Value = StringFromReal(FT004, 2, "f") ' 对应原F37C4

    ' 4. 保存并关闭工作簿
    excelWorkbook.Save()
    excelWorkbook.Close()
    ' 退出Excel应用
    excelApp.Quit()

    ' 可选:自动打开生成的报表
    System.Diagnostics.Process.Start(destFile)
Catch ex As Exception
    ' 异常处理(可添加日志记录逻辑)
    MessageBox.Show($"Excel操作失败:{ex.Message}", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
    ' 确保异常情况下Excel进程被关闭
    If excelWorkbook IsNot Nothing Then excelWorkbook.Close(False)
    If excelApp IsNot Nothing Then excelApp.Quit()
Finally
    ' 释放COM对象,避免内存泄漏
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorksheet)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
    excelWorksheet = Nothing
    excelWorkbook = Nothing
    excelApp = Nothing
End Try

注意事项:

  • 确保目标机器安装对应版本的Office,且Interop组件可用。
  • 必须正确释放COM对象,否则会导致Excel进程残留占用资源。
  • 64位系统下,项目编译平台(x86/x64)需与Office版本匹配。

问题2:若上述方案不可行,能否通过SQL查询从SQL Server导出数据至Excel?

完全可以!如果你的SCADA数据已存储在SQL Server中,这种方案可摆脱对Office客户端的依赖(或实现无界面自动化导出),适合批量定时场景。下面提供两种常见实现方式:

方式1:使用SQL Server Integration Services (SSIS)

  • 创建SSIS包,配置数据流向任务:从SQL Server数据源读取目标数据,写入Excel文件。
  • 可通过SQL Server代理定时执行包,或在Intouch中通过VB.NET调用DTSExec.exe命令行工具触发执行。

方式2:在Intouch中用VB.NET查询SQL Server并写入Excel

这种方式和问题1的方案结合,数据源替换为SQL Server:

Imports System.Data.SqlClient
Imports Microsoft.Office.Interop.Excel

' 1. 从SQL Server查询目标数据
Dim connString As String = "Data Source=你的SQL服务器地址;Initial Catalog=目标数据库名;User ID=用户名;Password=密码;"
Dim sqlQuery As String = "SELECT FechaInicio, FechaFin, E, Q, V, REE, FT001, FT002, FT003, FT004 FROM 数据表名 WHERE 查询条件"
Dim dataTable As New DataTable()

Using conn As New SqlConnection(connString)
    Using cmd As New SqlCommand(sqlQuery, conn)
        conn.Open()
        dataTable.Load(cmd.ExecuteReader())
    End Using
End Using

' 2. 将查询结果写入Excel(参考问题1的Excel操作代码)
If dataTable.Rows.Count > 0 Then
    Dim targetRow As DataRow = dataTable.Rows(0)
    ' 示例:将查询值写入对应单元格
    excelWorksheet.Range("F10").Value = targetRow("FechaInicio")
    ' 其他字段同理...
End If

补充:无Office依赖的导出方式

如果不想安装Office,可使用第三方库(如EPPlus、NPOI)生成Excel文件,这些库无需依赖Office客户端,兼容性更强。只需在Archestra Graphics项目中添加对应库的引用,即可实现无依赖导出。

现有代码问题分析

你的现有代码中WWPoke失效的核心原因:Office 2016对进程权限、COM对象访问逻辑做了调整,WWPoke无法正确获取Excel窗口句柄或工作表对象。替换为Excel Interop的方式可彻底解决该兼容性问题。

' 你的现有代码
dim sourceDir as string; dim destDir as string; dim fileName as string; dim destName as string; dim sourceFile as string; dim destFile as string; dim modo as System.IO.FileMode; dim fechaini as string; 
sourceDir = "C:\MIGRA\SCADA\Acesur_Cogeneracion"; 
destDir = "C:\InformesAcesur"; 
fileName = "Informe.xls"; 
destName = InTouch:$Day + "" + InTouch:$Month + "" + InTouch:$Year + "_" + InTouch:$Hour+ "" + InTouch:$Minute + ".xls"; 
sourceFile = System.IO.Path.Combine(sourceDir,fileName); 
destFile = destDir + "\" + destName; 
'I copy the original file to the new location 
System.IO.File.Copy(sourceFile,destFile,true); 
'I open the copied file 
System.Diagnostics.Process.Start("excel.exe",destFile); 
'I send the data to excel - THIS IS THE PART THAT'S NOT WORKING 
WWPoke( "excel.exe", "Hoja1", "F10C4", FechaInicio); 
WWPoke( "excel.exe", "Hoja1", "F11C4", FechaFin); 
WWPoke( "excel.exe", "Hoja1", "F20C4", StringFromReal(E,2,"f")); 
WWPoke( "excel.exe", "Hoja1", "F21C4", StringFromReal(Q,2,"f")); 
WWPoke( "excel.exe", "Hoja1", "F22C4", StringFromReal(V,2,"f")); 
WWPoke( "excel.exe", "Hoja1", "F28C4", StringFromReal(REE,2,"f")); 
WWPoke( "excel.exe", "Hoja1", "F34C4", StringFromReal(FT001,2,"f")); 
WWPoke( "excel.exe", "Hoja1", "F35C4", StringFromReal(FT002,2,"f")); 
WWPoke( "excel.exe", "Hoja1", "F36C4", StringFromReal(FT003,2,"f")); 
WWPoke( "excel.exe", "Hoja1", "F37C4", StringFromReal(FT004,2,"f"));

内容的提问来源于stack exchange,提问作者salonso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:46:21