Get current cell value from open Excel sheet using C# - c#

I have an open instance of Excel which is already connected to via a COM process, which I have changed the value of one of the cells. In C# I can't get a connection to the sheet, but in python i can
Working Python code:
import xlwings as xw
wb = xw.Book('c:/users/me/Desktop/mysheet.xlsx')
sheet = wb.sheets['Sheet1']
print(sheet.range('B1').value) # returns the current value
Non-working C# code:
var xlApp = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Workbook wb = xlApp.ActiveWorkbook; // returns null
Worksheet excelSheet = wb.ActiveSheet;
string test = excelSheet.Cells[1, 2].Value.ToString();
Is there a way to get C# to connect to this sheet, given it is already open via a COM automation from another software product?
Most of the online discussion around GetActiveObject is to talk to a specific instance of Excel if there are multiple instances running, I have the opposite problem, a single instance of Excel and multiple applications trying to connect to it.

Workbook wb = System.Runtime.InteropServices.Marshal.BindToMoniker(#"c:\users\me\Desktop\mysheet.xlsx") as Workbook;
should get the workbook even if it is already opened in Excel or not.
https://blogs.msdn.microsoft.com/eric_carter/2009/03/12/attaching-to-an-already-running-office-application-from-your-application-using-getactiveobject-or-bindtomoniker/

Related

automatically enabling macros in excel c#

I am accessing data from an embedded excel sheet through c#, but in order for the script to read the excel sheet I must manually press "enable macros" every time. How can I write a script such that it automatically enables macros every time? I am opening various excels so cannot change the settings on the excel sheet itself.
I saw something regarding the use of VBA but am unsure what to do? Would using this reference (using VBA = Microsoft.Vbe.Interop) be helpful?
The code below simply opens the excel, but I still have to click enable macros.
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
wordApp = new Word.Application();
xlApp = new Excel.Application();
wordDoc = wordApp.Documents.Open(#"Y:DocumentName");
Word.InlineShapes ils = wordDoc.InlineShapes;
ils[1].OLEFormat.Activate();
xlWorkBook = ils[1].OLEFormat.Object;
Have you tried setting the DisplayAlerts property to false?
xlApp.DisplayAlerts = false;

Excel Power Pivot export

I have a good skill in VBA and c# but a very low skill in Excel so I need help for my scenario.
I have an excel xlsx with some data inside.
Data comes from a Power Pivot table.
Assuming i'm using Excel 2018, i click on "Power Pivot" bar menu, then on "Manage".
I can see 4 tables.
I need a way "programatically" export the 4 tables to a csv.
With a simple c# app, I exported the sheet but data are incomplete because of filters and so on. And I cannot operate on the source excel because it come from a remote system.
Is there a way in VBA, or in .NET to iterate EVERY cell for every Power Pivot table in an Excel file?
I wrote something like:
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
xlApp = new Excel.Application();
var v = xlApp.Workbooks;
xlWorkBook = v.Open(bla bla...);
var s = xlWorkBook.Sheets[1];
var pt = s.PivotTables[1];
.. now I am stuck...
I had a try opening the xlsx file from another excel file, within a Module VBA, with no luck.
Please help me :-)
TY

Using the COM for opening Excel, can't select a particular sheet

I am using the COM to work with Excel 2007. When I use the following code, it opens to the first sheet by default.
Excel = Sys.OleObject("Excel.Application");
Delay (3000); // Wait until Excel starts
Excel.Visible = true;
Excel.Workbooks.Open("G:\\Documentation\\CalCit Excel Files\\2004 Test Data v3 FINAL_new.xlsx");
I need to select different sheets. I tryed using the following code from the DDTdriver code.
Excel = Sys.OleObject("Excel.Application");
Delay (3000); // Wait until Excel starts
Excel.Visible = true;
Excel.Workbooks.Open("G:\\Documentation\\CalCit Excel Files\\2004 Test Data v3 FINAL_new.xlsx", "sheet2", true);
But this does not work. I have tryed many other configs and still get nothing.
For Excel version 2013 or later :
According to learn.microsoft.com
this can be done with Sheets.Select method :
((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[1]).Select();
which in the code the selected sheet is 1
another method for older versions of Excel :
Excel.Worksheet sheet = (Excel.Worksheet)this.Sheets["Sheet2"];
sheet.Select(Type.Missing);
where Sheet2 is the name of the sheet

Open Excel workbook in background using interop

I'm wanting to import data from an Excel workbook without actually displaying the open workbook.
I could have sworn I had used the following code on a previous project and it had worked:
var excelApp = new Excel.Application { Visible = false };
var workbook = excelApp.Workbooks.Open(filePath);
Unfortunately when the workbook opens it is displayed to the user which is unnecessary for this application.
I'm using Microsoft Excel 15.0 Object Library on this project when previously I think it was version 12 or 13. Maybe this is the problem, or is my memory fading and the code is incorrect?
I know this is old but just in case anybody still needs this answer...
excelApp.Visible = false;
(excelApp being the name of the variable used for the excel application)

C# generating excel file and writing to cells

I am working on a project that runs MySQL queries on DB. I need to feed out the results to a excel sheet that I will be generating on the fly. I had started the code, using msdn tutorials as well as information I found on stackoverflow, however running the application leaves me with
"Object reference not set to an instance of an object."
At this point I just have the basic code started as I wanted to make this work before I started creating the entire spreadsheet in the code.
var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
((Excel.Range)workSheet.Cells[1, 1]).Value = "Print Date:";
If I can provide any more info just ask. I greatly appreciate any information or insight you can provide me with!
I'd say that workSheet is being set to null as excelApp.ActiveSheet doesn't exist yet.
Here's some code that'll create a worksheet object you can use...
var application = new Application();
var workbooks = application.Workbooks;
var workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
var worksheets = (Sheets)workbook.Worksheets;
var worksheet = worksheets[1];
A few things to note when working with Excel:
Release every object with System.Runtime.InteropServices.Marshal.ReleaseComObject(object) when you're finished with it. If you don't, Excel won't exit when you're done.
Don't use double-dot references (i.e., object1.object2.value). If you do you can't release it properly as above.
Indexes always start from 1, not 0 (in my experience anyway).
The reason you get that error is, you have not created any worksheets in the Application.
Try doing this.
Excel.Worksheet newWorksheet;
newWorksheet = (Excel.Worksheet)excelApp.Worksheets.Add();

Categories

Resources