且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用脚本任务声明工作表?

更新时间:2023-12-05 14:34:40

如果您要添加新工作表或只是编辑当前工作表,我真的不理解.对于这两种情况,我都会给出一些建议:

如果要编辑现有的工作表,请尝试以下操作之一:

Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];

例如:

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"file.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;

有关更多信息,请检查以下链接:

  • Read Excel File in C#
  • Reading data from excel 2010 using Microsoft.Office.Interop.Excel
  • Excel interop: _Worksheet or Worksheet?

检查您使用的Office.Interop DLL是否与计算机上安装的officeversion相关.

Check that the Office.Interop DLL you are using are relevant to the officeversion installed on the machine.

检查工作簿是否不是只读或受保护的,可以参考以下SO问题:

Check that the workbook is not ReadOnly or it is protected, you can refer to the following SO question:

还要确保工作簿中不包含隐藏的工作表或临时工作表,请尝试遍历工作簿中的所有工作表并调试代码以查看发生了什么.

Also make sure that the workbook does not contains hidden or temp worksheets, try to loop over all Worksheets in the Workbook and Debug the code to see what is going on.

如果您要向现有工作簿中添加新工作表,则可以:

您可以使用类似的代码:

You can use a similar code:

Excel._Worksheet newWorksheet;
newWorksheet = (Excel._Worksheet)ThisWorkbook.Worksheets.Add();

有关更多信息,您可以检查以下链接:

  • How to: Programmatically add new worksheets to workbooks
  • How to create a new worksheet in Excel file c#?

首先,您必须创建一个Excel Connection Manager,然后添加一个Execute SQL Task,选择Excel Connection并编写一个CREATE语句,例如:

First you have to create an Excel Connection Manager, then add an Execute SQL Task, choose the Excel Connection and write a CREATE Statement, as example:

CREATE TABLE
`Excel Destination` (

    `PromotionKey` INTEGER,
    `PromotionAlternateKey` INTEGER,
    `EnglishPromotionName` NVARCHAR(255),
    `SpanishPromotionName` NVARCHAR(255),
    `FrenchPromotionName` NVARCHAR(255),
    `DiscountPct` DOUBLE PRECISION,
    `EnglishPromotionType` NVARCHAR(50),
    `SpanishPromotionType` NVARCHAR(50),
    `FrenchPromotionType` NVARCHAR(50),
    `EnglishPromotionCategory` NVARCHAR(50),
    `SpanishPromotionCategory` NVARCHAR(50),
    `FrenchPromotionCategory` NVARCHAR(50),
    `StartDate` DATETIME,
    `EndDate` DATETIME,
    `MinQty` INTEGER,
    `MaxQty` INTEGER

)

有关更多信息,您可以检查以下链接: