将工作表添加到当前的Excel工作簿

时间:2017-02-13 13:47:16

标签: c# excel vsto

我正在尝试使用C#向我现有的Excel实例添加工作表,但是我收到错误

  

对象未设置为对象的实例

抛出代码的行是以newSheet =

开头的行

我不确定为什么抛出这个因为我声明我的变量newSheet并且我已经实例化了xlApp ----为了使这个语法按预期工作,我需要改变什么?

public static void AddNewSheet()
{
    Excel.Application xlApp;
    xlApp = new Excel.Application();
    Excel.Worksheet Jobs, Estimates;
    string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Test.xlsx";
    var xlWorkBook = xlApp.Workbooks.Open(filePath);
    Excel.Worksheet newSheet;

    //Add a new worksheet to the workbook
    newSheet = (Excel.Worksheet)xlApp.Worksheets.Add(System.Reflection.Missing.Value,
                                            xlWorkBook.Worksheets[xlWorkBook.Worksheets.Count],
                                            System.Reflection.Missing.Value,
                                            System.Reflection.Missing.Value);
}

修改
附加代码 - 希望有助于将工作表添加到已创建的工作簿中。

namespace GenerateExcelWorkbookData
{
    public partial class Form1 : Form
    {
        public static Excel.Application xlApp;
        public static Excel.Workbook xlWorkBook;
        public static Excel.Worksheet xlWorkSheet;
        public static Excel.Worksheet newSeet;

        Main()
        {
            //Query Excel To Get DataTable Here

            //Excel Info
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            CreateSubWorksheets();
        }
        public static void CreateSubWorksheets()
        {
            string sheetNameprefix = "SS";
            Excel.Worksheet sheet1;

            sheet1 = xlApp.ActiveWorkbook.Worksheets.Item["Newly Added"];
            Excel.Range range = sheet1.Range["A1"].CurrentRegion;
            object OriginalData = range.Value;
            string OriginalAddress = range.Address;

            while (range.Cells[2, 1].Value != null && range.Cells[2, 1].Value != "")
            {
                range.AutoFilter(1, range.Cells[2, 1].Value);

                newSeet = (Excel.Worksheet)xlApp.Worksheets.Add(System.Reflection.Missing.Value,
                                                xlWorkBook.Worksheets[xlWorkBook.Worksheets.Count],
                                                System.Reflection.Missing.Value,
                                                System.Reflection.Missing.Value);

                //Do more stuff here
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个......

Microsoft.Office.Interop.Excel.Application Excel = null;
        Microsoft.Office.Interop.Excel._Workbook oWB = null;
        Microsoft.Office.Interop.Excel._Worksheet oSheet = null;

        try
        {
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

            if (xlApp == null)
            {
                return false;
            }

            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Add();
            xlWorkSheet.Name = "Details";


 }