从Commandline应用程序刷新Excel ListObject

时间:2010-04-29 16:17:42

标签: excel datatable vsto console-application listobject

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

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

我尝试使用ServerDocuments,但我没有CachedData,并且无法在Application Add-in级别使用。有什么建议吗?

提前致谢。

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..

代码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。 Applications.Runtime.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;
  }
 }
}

0 个答案:

没有答案