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

我试图通过命令行应用程序更新由Excel加载项(示例代码1)创build的工作簿中承载的listObject的信息。

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

我已经尝试了与ServerDocuments,但我没有CachedData,并且不可能在应用程序加载项级别使用。 任何build议?

提前致谢。

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

Code2参考: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; } } } 

Interesting Posts