C# vsto 添加工作表

标签 c# excel vsto

我的所有代码都有效,但是当我写入新工作表中的单元格时,写入第一个 Sheet1。在单击事件结束之前不会创建工作表。

我可以在点击事件后触发另一个事件来填充创建的新工作表吗?

我该如何解决这个问题?谢谢...

using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using Microsoft.Office.Tools.Ribbon;
using Excel = Microsoft.Office.Interop.Excel;


namespace Prototype
{
    public partial class Ribbon1
    {
        private Excel.Application xlApp;
        private Workbook xlWorkBook;
        private Worksheet xlWorkSheet;
        private dynamic excelSheet;
        private Range range;

        private void Ribbon1_Load(object sender, RibbonUIEventArgs e) { }

        private void Button1_Click(object sender, RibbonControlEventArgs e)
        {
            xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
            xlApp.Visible = true;
            xlWorkBook = xlApp.ActiveWorkbook;
            xlWorkSheet = xlWorkBook.Worksheets.Item[1];
            excelSheet = xlWorkBook.ActiveSheet;
            xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
            xlWorkBook = xlApp.ActiveWorkbook;

            xlWorkBook.Sheets.Add(After: xlWorkBook.Sheets[xlWorkBook.Sheets.Count]);
            xlWorkSheet = xlWorkBook.Worksheets.Item[2];
            xlWorkSheet.Select();
            xlWorkSheet.Name = "Dashboard";

            xlWorkBook.Sheets.Add(After: xlWorkBook.Sheets[xlWorkBook.Sheets.Count]);
            xlWorkSheet = xlWorkBook.Worksheets.Item[3];
            xlWorkSheet.Select();
            xlWorkSheet.Name = "Dashboard2";

            xlWorkBook.Worksheets.Add(After: xlWorkBook.Sheets[xlWorkBook.Sheets.Count]);
            xlWorkSheet = xlWorkBook.Worksheets.Item[4];
            xlWorkSheet.Select();
            xlWorkSheet.Name = "Dashboard3";

            xlWorkSheet = (Worksheet)xlWorkBook.Worksheets["Dashboard"];
            xlWorkSheet.Select();
            range = excelSheet.Cells[1, 1];
            range.Value2 = "Test";

            xlWorkSheet = (Worksheet)xlWorkBook.Worksheets["Dashboard2"];
            xlWorkSheet.Select();
            range = excelSheet.Cells[1, 1];
            range.Value2 = "Test";
        }
    }
}

最佳答案

您正在根据 excelSheet 定义范围,您在创建新工作表之前将其定义为事件工作表。仅仅因为您选择了 xlWorkSheet 并不意味着 excelSheet 改变了值。你的代码应该是

xlWorkSheet = (Worksheet)xlWorkBook.Worksheets["Dashboard"];
range = xlWorkSheet.Cells[1, 1];
range.Value2 = "Test";

xlWorkSheet = (Worksheet)xlWorkBook.Worksheets["Dashboard2"];
range = xlWorkSheet.Cells[1, 1];
range.Value2 = "Test";

关于C# vsto 添加工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51405991/

相关文章:

vba - Excel 和 VBA : 'If value is less than 1' condition being triggered by a value that is exactly 1

.net - 一款适用于 Office 2010 和 2007 的 Office 插件

c# - GlyphIndices 属性如何在 GlyphRun 对象中编码?

c# - 这样的集合是否存在(Dictionary 和 HashSet 的功能)?

c# - 我可以使用 OwinContext 在单个 ASP.NET MVC5 应用程序之间共享数据吗?

javascript - 使用javascript将大型html表格导出到excel

c# - ANCM InProcess 启动失败,因为 runtimeconfig.json 无效

c# - 使用 OpenXML 从 MVC Controller 将工作簿设为只读

excel - 如何获取当前工作簿窗口的句柄?

c# - 如何检测 Excel 是否通过自动化启动(VSTO Addin 上下文)