Update excel sheet cell (AO7) value in c# - c#

I am trying to update the particular cell value, but getting an error. Below is the code which I tried.
Microsoft.Office.Interop.Excel.Application excel;
Microsoft.Office.Interop.Excel.Workbook workbook;
excel = new Microsoft.Office.Interop.Excel.Application();
workbook = excel.Workbooks.Open(#"D:\Ride\Ride Master Sheet 05 11 2020.xlsx", ReadOnly: false, Editable: true);
//Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.Worksheets.Item[1] as Microsoft.Office.Interop.Excel.Worksheet;
Microsoft.Office.Interop.Excel.Sheets sheets = excel.Worksheets;
// Select worksheets
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item("Concentric Pricing");
if (ws == null)
return;
Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)ws.get_Range("A7", "O7");
Microsoft.Office.Interop.Excel.Range row1 = worksheet.Rows[7].Cells[1, 1];
row1.Value = "";
excel.Application.ActiveWorkbook.Save();
excel.Application.Quit();
excel.Quit();
Getting error
Unable to cast COM object of type
'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type
'Microsoft.Office.Interop.Excel._Application'.
Any help on this?

Related

Writing a value to an already open workbook using c#

I have an excel workbook that has a button, that triggers a c# application. I need to add the returned value from the app to the excel workbook but I can't figure out how to do that while the excel workbook is already open.
I've basically been grasping at straws. Every example I've found opens a read only version of the excel workbook
Microsoft.Office.Interop.Excel._Application myExcelApp;
Microsoft.Office.Interop.Excel.Workbook myExcelWorkbook = new
Microsoft.Office.Interop.Excel.Workbook();
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
xlWorkSheet = myExcelWorkbook.Sheets["Sheet1"];
xlWorkSheet = myExcelWorkbook.ActiveSheet;
myExcelApp = new Microsoft.Office.Interop.Excel.Application();
myExcelApp =
(Microsoft.Office.Interop.Excel.Application)
System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
foreach (Microsoft.Office.Interop.Excel.Workbook item in myExcelApp.Workbooks)
{
//Select the excel target 'NAME'
if (item.Name == "DatagridviewTest.xlsm")
{
myExcelWorkbook = item;
break;
}
//Select the target workbook
xlWorkSheet = myExcelWorkbook.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
//Set cell value
xlWorkSheet.Cells[5, 1] = "new value";
}
I get an unhandled exception error "System runtime COM exception"

Copying excel worksheet to a different workbook

I'm trying to copy a worksheet to a different workbook into the last worksheet of the target workbook.
My workbooks and worksheets are created like this:
public Microsoft.Office.Interop.Excel.Workbook xlWorkbookMatrix;
public Microsoft.Office.Interop.Excel._Worksheet xlWorksheetMatrix;
I tried using worksheet.copy:
xlWorksheetMatrix.Copy(Type.Missing, xlWorkbookEvaluation.Sheets[xlWorkbookEvaluation.Sheets.Count]);
and worksheet.UsedRange.Copy:
xlWorksheetMatrix.UsedRange.Copy(xlWorkbookEvaluation.Sheets[xlWorkbookEvaluation.Sheets.Count]);
With both different methods I always get an error.
For worksheet.Copy:
System.Runtime.InteropServices.COMException occured in System.Dynamic.dll
The Copy-property of the worksheet object can't be assigned
For worksheet.UsedRange.Copy:
System.Runtime.InteropServices.COMException occured in System.Dynamic.dll
The Copy-property of the Range object can't be assigned
Having a template sheet you want to fill many times, Hope this helps :
public void test()
{
Excel.Application excelApp;
string fileTarget = "C:\target.xlsx";
string fileTemplate = "C:\template.xlsx";
excelApp = new Excel.Application();
Excel.Workbook wbTemp, wbTarget;
Excel.Worksheet sh;
//Create target workbook
wbTarget = excelApp.Workbooks.Open(fileTemplate);
//Fill target workbook
//Open the template sheet
sh = wbTarget.Worksheets["TEMPLATE"];
//Fill in some data
sh.Cells[1, 1] = "HELLO WORLD!";
//Rename sheet
sh.Name = "1. SHEET";
//Save file
wbTarget.SaveAs(fileTarget);
//Iterate through the rest of the files
for (int i = 1; i < 3; i++)
{
//Open template file in temporary workbook
wbTemp = excelApp.Workbooks.Open(fileTemplate);
//Fill temporary workbook
//Open the template sheet
sh = wbTemp.Worksheets["TEMPLATE"];
//Fill in some data
sh.Cells[1, 1] = "HELLO WORLD! FOR THE " + i + ".TH TIME";
//Rename sheet
sh.Name = i + ". SHEET";
//Copy sheet to target workbook
sh.Copy(wbTarget.Worksheets[1]);
//Close temporary workbook without saving
wbTemp.Close(false);
}
//Close and save target workbook
wbTarget.Close(true);
//Kill excelapp
excelApp.Quit();
}

Delete first row in Excel Workbook - Object Reference required error c#

I have been using the "Code Project" and "Stackoverflow" to create a script which will delete the first row of a specified excel workbook.
I am having an issue with line 13 "Range.EntireRow;" with the error "An object reference is required for the non-static field, method, or property Range.Entire.Row
Please see the script I have created below;
public void DeleteRows(string workbookPath)
{
// New Excel Application
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
//Open WorkBook
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);
Microsoft.Office.Interop.Excel.Range excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range("A1", "A1");
Microsoft.Office.Interop.Excel.Range row = Range.EntireRow; //ERROR Line
row.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlUp);
Any advice on how to fix this error would be greatly appreciated.
You could refer to the Cells of the Range to get the entire row:
Microsoft.Office.Interop.Excel.Range row = excelCell.Cells.EntireRow
or even omitting the Cells should work as well:
Microsoft.Office.Interop.Excel.Range row = excelCell.EntireRow
MSDN Range.EntireRow Property
You should use: Microsoft.Office.Interop.Excel.Range row = excelCell.EntireRow;

Excel range.Find returning null C#

Ok, so here is the scoop. I have an excel sheet that holds quote numbers for clients all stored in the C column in my worksheet. I have it so when selected the program will take the quote number from the datagridview and search for it in excel. Here is the code :
public static string pQuoteHolder;
file = Path.Combine(Application.StartupPath, "__Quote_Tracker.xlsm");
Excel.Application xlapp = new Excel.Application();
Excel.Workbook workbook = xlapp.Workbooks.Open(file);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1];
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Excel.Range range;
Excel.Range clients = xlapp.get_Range("C1", "C500");
pQuoteHolder = Convert.ToString(dataGridView2.CurrentRow.Cells[1].Value);
worksheet = (Excel.Worksheet)workbook.Sheets[1];
currentFind = clients.Find(pQuoteHolder, Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing);
if (currentFind == null)
{
MessageBox.Show("Nobody was found.");
}
So here is the problem, I see from debugging that qQuoteHolder is getting the correct value from datagridview, I can see for myself that all quote numbers in my excel sheet are indeed all in the C column and within range, so why is currentfind returning null? I am still learning excel and c#, Did I manage to code the range.Find method incorrectly?
Thanks in advance!

C# Excel casting

I have an existing project that creates an excel spreadsheet using the Microsoft.Office.Interop.Excel.WorkbookClass and I am trying to convert the workbook object to the Microsoft.Office.Tools.Excel.Workbook, but I am getting an exception thrown stating:
"Unable to cast COM object of type
'Microsoft.Office.Interop.Excel.WorkbookClass' to class type
'Microsoft.Office.Tools.Excel.Workbook'. Instances of types that
represent COM components cannot be cast to types that do not represent
COM components; however they can be cast to interfaces as long as the
underlying COM component supports QueryInterface calls for the IID of
the interface."
Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Tools.Excel.Workbook xlWorkbook = null;
Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
Microsoft.Office.Tools.Excel.Worksheet xlNewSheet = null;
xlApp = new Interop.Application();
xlApp.Visible = true;
xlWorkbook = (Microsoft.Office.Tools.Excel.Workbook)xlApp.Workbooks.Add(Interop.XlWBATemplate.xlWBATWorksheet);
xlSheets = xlWorkbook.Sheets as Interop.Sheets;
xlNewSheet = (Microsoft.Office.Tools.Excel.Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
xlNewSheet.Name = "SheetName1";
Is this possible if not, what other options can I take that since the excel sheet is already created using the interop class and
If possible try to avoid using the Microsoft.Office.Tools assembly which is internal to Visual Studio Tools For Office.
I've amended your code as below:
Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = null;
Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
Microsoft.Office.Interop.Excel.Worksheet xlNewSheet = null;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = true;
xlWorkbook = xlApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
xlSheets = xlWorkbook.Sheets as Microsoft.Office.Interop.Excel.Sheets;
xlNewSheet = xlSheets.Add(xlSheets[1], System.Type.Missing, System.Type.Missing, System.Type.Missing);
xlNewSheet.Name = "SheetName1";
It looks like to "convert" an interop workbook to a vsto workbook you just do this:
In application-level projects, you can create
Microsoft.Office.Tools.Excel.Workbook objects programmatically by
using the GetVstoObject method.
Microsoft.Office.Interop.Excel.Workbook nativeWorkbook = Globals.ThisAddIn.Application.ActiveWorkbook;
if (nativeWorkbook != null)
{
Microsoft.Office.Tools.Excel.Workbook vstoWorkbook =
Globals.Factory.GetVstoObject(nativeWorkbook);
}

Categories

Resources