从命令行应用程序刷新 Excel ListObject

发布于 2024-08-30 09:28:45 字数 4182 浏览 1 评论 0原文

我正在尝试使用命令行应用程序更新由 Excel 插件(示例 Code1)创建的工作簿中托管的 listObject 的信息。

我尝试创建 Excel 实例并访问所有 listObjects,但 GetVstoObject 始终返回空对象。我认为这是一个安全问题,但我不知道如何解决。 (示例代码2)。

我尝试过 ServerDocuments 但我没有 CachedData,并且无法在应用程序加载项级别使用。有什么建议吗?

提前致谢。

代码1

using System;    
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools.Excel.Extensions;
namespace ExcelAddIn2{
public partial class ThisAddIn{
    private void ThisAddIn_Startup(object sender, System.EventArgs e){            
        DataTable theDataTable = GetDataTable();
        Workbook workbook =  Globals.ThisAddIn.Application.ActiveWorkbook.GetVstoObject();            
        Worksheet worksheet = ((Excel.Worksheet)workbook.Worksheets[1]).GetVstoObject();            
        Excel.Range range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, 1]];
        Excel.ListObject interopList = worksheet.ListObjects.Add(Microsoft.Office.Interop.Excel.XlListObjectSourceType.xlSrcRange, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo,range);
        ListObject list = interopList.GetVstoObject();                         
        list.Name = "myListObject";
        list.DataSource = theDataTable;            
        workbook.SaveCopyAs(@"C:\theExcel.xlsx");
    }

    private DataTable GetDataTable() {
        /*Datatable example, I'm using my implementation of a OleDBCommand to get it*/
        DataTable table = new DataTable("myTable");                                   
        DataColumn columnId = new DataColumn();
        columnId.DataType = System.Type.GetType("System.String");
        columnId.ColumnName = "id";            
        DataColumn columnName = new DataColumn();
        columnName.DataType = System.Type.GetType("System.String");
        columnName.ColumnName = "Name";
        table.Columns.Add(columnId);
        table.Columns.Add(columnName);
        DataRow row;
        for (int i = 0; i <= 2; i++){
            row = table.NewRow();
            row["id"] = i;
            row["Name"] = "Name " + i;
            table.Rows.Add(row);
        }
        return table;            
     }
     ..etc..

代码2 参考文献: Microsoft.Office.Interop.Excel、Microsoft.Office.Tools.Common.v9.0、Microsoft.Office.Tools.Excel.v9.0、Microsoft.Office.Tools.v9.0、Microsoft.VisualStudio.Tools。应用程序.运行时.v9.0, System.Windows.Forms(它需要它??)

using System;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools.Excel.Extensions;
using System.Reflection;
namespace ConsoleApplication1{
 class Program {
static void Main(string[] args)
{
 Excel.Application app = new Excel.ApplicationClass();
 Excel.Workbook excelWorkbook = app.Workbooks.Open(@"C:\theExcel.xlsx",
             Type.Missing, true, Type.Missing, Type.Missing,
             Type.Missing, Type.Missing, Type.Missing, Type.Missing,
             Type.Missing, Type.Missing, Type.Missing, Type.Missing,
             Type.Missing, Type.Missing);
 Microsoft.Office.Tools.Excel.Worksheet worksheet = ((Excel.Worksheet)excelWorkbook.Worksheets[1]).GetVstoObject();
 foreach (ListObject list in worksheet.ListObjects) {
  list.DataSource = GetNewDataTable();
 }
 app.Quit();
}
private static DataTable GetNewDataTable()
{
 DataTable table = new DataTable("myTable");
 DataColumn columnId = new DataColumn();
 columnId.DataType = System.Type.GetType("System.String");
 columnId.ColumnName = "id";
 DataColumn columnName = new DataColumn();
 columnName.DataType = System.Type.GetType("System.String");
 columnName.ColumnName = "Name";
 table.Columns.Add(columnId);
 table.Columns.Add(columnName);
 DataRow row;
 for (int i = 0; i <= 2; i++)
 {
  row = table.NewRow();
  row["id"] = i;
  row["Name"] = "New Name " + i;
  table.Rows.Add(row);
 }
 return table;
  }
 }
}

I'm trying to update the information of a listObject hosted in a Workbook created by an Excel Addin (example Code1) with an commandline application.

I've tried creating an instance of Excel and accessing at all the listObjects but GetVstoObject always returns null objects. I think is a security problem, but I don't know how to resolve it. (example Code2).

I've tried with ServerDocuments but I don't have CachedData, and it's not possible to use at Application Add-in level. Any suggestion?

Thanks in advance.

Code1

using System;    
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools.Excel.Extensions;
namespace ExcelAddIn2{
public partial class ThisAddIn{
    private void ThisAddIn_Startup(object sender, System.EventArgs e){            
        DataTable theDataTable = GetDataTable();
        Workbook workbook =  Globals.ThisAddIn.Application.ActiveWorkbook.GetVstoObject();            
        Worksheet worksheet = ((Excel.Worksheet)workbook.Worksheets[1]).GetVstoObject();            
        Excel.Range range = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, 1]];
        Excel.ListObject interopList = worksheet.ListObjects.Add(Microsoft.Office.Interop.Excel.XlListObjectSourceType.xlSrcRange, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo,range);
        ListObject list = interopList.GetVstoObject();                         
        list.Name = "myListObject";
        list.DataSource = theDataTable;            
        workbook.SaveCopyAs(@"C:\theExcel.xlsx");
    }

    private DataTable GetDataTable() {
        /*Datatable example, I'm using my implementation of a OleDBCommand to get it*/
        DataTable table = new DataTable("myTable");                                   
        DataColumn columnId = new DataColumn();
        columnId.DataType = System.Type.GetType("System.String");
        columnId.ColumnName = "id";            
        DataColumn columnName = new DataColumn();
        columnName.DataType = System.Type.GetType("System.String");
        columnName.ColumnName = "Name";
        table.Columns.Add(columnId);
        table.Columns.Add(columnName);
        DataRow row;
        for (int i = 0; i <= 2; i++){
            row = table.NewRow();
            row["id"] = i;
            row["Name"] = "Name " + i;
            table.Rows.Add(row);
        }
        return table;            
     }
     ..etc..

Code2
References: Microsoft.Office.Interop.Excel, Microsoft.Office.Tools.Common.v9.0, Microsoft.Office.Tools.Excel.v9.0, Microsoft.Office.Tools.v9.0, Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0,
System.Windows.Forms (It needs it ¿?)

using System;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools.Excel.Extensions;
using System.Reflection;
namespace ConsoleApplication1{
 class Program {
static void Main(string[] args)
{
 Excel.Application app = new Excel.ApplicationClass();
 Excel.Workbook excelWorkbook = app.Workbooks.Open(@"C:\theExcel.xlsx",
             Type.Missing, true, Type.Missing, Type.Missing,
             Type.Missing, Type.Missing, Type.Missing, Type.Missing,
             Type.Missing, Type.Missing, Type.Missing, Type.Missing,
             Type.Missing, Type.Missing);
 Microsoft.Office.Tools.Excel.Worksheet worksheet = ((Excel.Worksheet)excelWorkbook.Worksheets[1]).GetVstoObject();
 foreach (ListObject list in worksheet.ListObjects) {
  list.DataSource = GetNewDataTable();
 }
 app.Quit();
}
private static DataTable GetNewDataTable()
{
 DataTable table = new DataTable("myTable");
 DataColumn columnId = new DataColumn();
 columnId.DataType = System.Type.GetType("System.String");
 columnId.ColumnName = "id";
 DataColumn columnName = new DataColumn();
 columnName.DataType = System.Type.GetType("System.String");
 columnName.ColumnName = "Name";
 table.Columns.Add(columnId);
 table.Columns.Add(columnName);
 DataRow row;
 for (int i = 0; i <= 2; i++)
 {
  row = table.NewRow();
  row["id"] = i;
  row["Name"] = "New Name " + i;
  table.Rows.Add(row);
 }
 return table;
  }
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文