3

I'm trying to declare a Worksheet to handle cells of .xlsx file, but my C# script fails when I declare Worksheet object :

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

Workbook excelBook = xlApp.Workbooks.Open(fileFullPath);

MySheet = (Excel.Worksheet)excelBook.Worksheets[Data_Sheet];

I've tried all of this statements :

MySheet workSheet = (Worksheet)excelBook.Application.Sheets[1];  
MySheet = (Excel.Worksheet)excelBook.Worksheets[1];

Even

Worksheet MySheet = new Worksheet();
MySheet = excelBook.Worksheets[Data_Sheet];

I'm using this code in a script task in SSIS package and it doesn't show me the error message, I have only the error window telling me that the contained scripts have error compilation.

Thank you for your help.

Hadi
  • 36,233
  • 13
  • 65
  • 124
MedEc
  • 169
  • 1
  • 14

2 Answers2

3

I really didn't understood if you are looking to add a new worksheet or just edit a current one. I will give some suggestions for both cases:

(1) Edit an existing Worksheet

If you are looking to edit an existing Worksheet, try one of the following:

(a) Using _Worksheet instead of Worksheet

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

As example:

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;

For more information, check to following links:

(b) Interop Library Version

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

(c) Permissions and Protection issues

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

(d) Hidden Worksheets issue

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.


(2) Add a new Worksheet

If you are looking to add a new worksheet to an existing workbook you can:

(a) Add it via Script-Task

You can use a similar code:

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

For more information, you can check the following links:

(b) Use Execute SQL Task

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

)

For more information, you can check the following links:

Hadi
  • 36,233
  • 13
  • 65
  • 124
  • Thank you for your precious answers. Actually, I'm trying to edit an existing worksheet, after looking and trying all the declarations I've found this one in one of the links above and it's the only one that works for me : `Worksheet xlSheets = excelBook.Sheets[1] as Excel.Worksheet;` and I don't know whey and I've checked all the points that you mentioned : DLL : Assembly Microsoft.Office.Interop.Excel C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll, the workbook is not ReadOnly. – MedEc Feb 04 '19 at 14:03
  • But I'm still having issue with range and cells. I'm trying to use : `Range rng = xlSheets.Cells[RowIndex, ColumnIndex] as Excel.Range; rng.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);` but this generates exception without compilation error. Can you help me with please ? – MedEc Feb 04 '19 at 14:08
  • actually it works when I'm replacing `Cells[RowIndex, ColumnIndex]` by `Cells[1, 1]`. But I need to get indexes from variables that I handle before in the script : `int RowIndex = 0; int ColumnIndex = 0; ColumnIndex = ColumnData.Table.Columns[ColumnDataCellsValue].Ordinal; RowIndex = dtDataRows.Rows.IndexOf(rowDataID);`. Thank you for your help – MedEc Feb 04 '19 at 14:32
  • the cell row and column index are not zero based, so you cannot start with `0`, you have to start looping from `1` -> `int RowIndex = 1; int ColumnIndex = 1; ColumnIndex = ColumnData.Table.Columns[ColumnDataCellsValue].Ordinal + 1; RowIndex = dtDataRows.Rows.IndexOf(rowDataID) + 1;` – Hadi Feb 04 '19 at 19:56
  • 1
    I could not try this before today and it works like a charm. I'm really very thankful for your precious help and clear answers. – MedEc Feb 07 '19 at 09:02
1

The Office Interop / Object Model API is based on running Office code in the context of your application. It expects to be running on the UI thread of an interactive (i.e. non-server) application.

See Considerations for server-side Automation of Office

If you ever do get this working, it will fail at just about the most inconvenient time.

If you need to manipulate Office documents on a server, use a server-side appropriate API like OpenXML

Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • thanks for your answer, actually my need is to change color of a cell based on RowIndex and ColumnIndex that's why I'm using Office Interop. The rest of the scripts works fine I can get column value correctly but when trying to highlight the cell of dataset a person recommended me to use Office Interop because according to what he said it's not possible to change color of cell from dataset : 'object CellToHigh = dsDataCell.Tables[Data_Sheet].Rows[RowIndex][ColumnIndex];' There is any other solution without using OpenXML ? because it looks complicated to me to use this solution. – MedEc Feb 01 '19 at 16:10
  • No good ever comes from running Excel on a server. I used to work in Microsoft support, and that KB article was probably the one that I sent customers the most often. – Flydog57 Feb 01 '19 at 16:19
  • My issue will be solved by installing Open XML SDK 2.5 ? and adding DocumentFormat.OPENXML to references ? is this the solution please or I have ? And FYI I use Office Interop in a previous scripttask in the same package to retrieve spreadsheet name and it works well. – MedEc Feb 01 '19 at 16:29
  • All I'm saying is that using Office interop on a server may or may not work. It may work for a while and then fail inexplicably. Microsoft doesn't support it, and any advice you get about making this work is likely wrong. Yeah, the OpenXML SDK is a very big stick (but, if you go that route, make sure to download the OpenXML Productivity Tool and get used to what it can do for you). There are also other Office file APIs (though I can't think of their names - dig around stack overflow, looking at the `[Excel]` tag – Flydog57 Feb 01 '19 at 16:47
  • Otherwise, can you give me please an alternative solution to server-side Automation it's not clear on Microsoft page. – MedEc Feb 01 '19 at 17:02