Save/Close Excel Workbook and quit excel with no popups c# - c#

I have the below code which works to delete the first row of a specified excel workbook. After this is done I would like to save (Overwrite changes) and exit the excel applications.
I gathered this may be achieved by Workbook.Close(True) but the popups still occur and the Object workbook is not referenced.
Any help would be much appreciated.
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 = excelCell.EntireRow;
row.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlUp);
}

Honestly, all I think that's missing from your code is a save and close. Once you save, the warning should not be issued.
using Excelx = Microsoft.Office.Interop.Excel;
public void DeleteRows(string workbookPath)
{
// New Excel Application
Excelx.Application excelApp = new Excelx.Application();
//Open WorkBook
Excelx.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excelx.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Excelx.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excelx.Worksheet excelWorksheet = (Excelx.Worksheet)excelSheets.get_Item(currentSheet);
Excelx.Range excelCell = (Excelx.Range)excelWorksheet.get_Range("A1", "A1");
Excelx.Range row = excelCell.EntireRow;
row.Delete(Excelx.XlDirection.xlUp);
// This should be all you need:
excelWorkbook.Save();
excelWorkbook.Close();
}
I'd be puzzled if this doesn't work, but if it doesn't, it might help when you are debugging to make Excel visible and step through the code:
excelApp.Visible = true;
As a final last attempt, before you save, you can disable warnings which will tell Excel to dispense with any of the dialogs to try to save you from yourself:
excelApp.DisplayAlerts = false;
excelWorkbook.Save();
excelWorkbook.Close();
excelApp.DisplayAlerts = true;
And again, I think you shouldn't even get to step 2 for this to work, but let me know if it doesn't. We would have quite a puzzle.

Related

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;

How to check cell contain filter or not in excel sheet through oledb reader?

How to check cell contain filter or not in excel sheet through oledb excel reader?
want method or code for the same..
I had some solution with Interop you can check this , it may resolve your problem...
public bool IsFilterExistInExcel(string excelpath)
{
bool IsFilterExist=false;
Microsoft.Office.Interop.Excel.Application excelApp = null;
Microsoft.Office.Interop.Excel.Workbooks workBooks = null;
Microsoft.Office.Interop.Excel.Workbook workBook = null;
Microsoft.Office.Interop.Excel.Worksheet workSheet;
excelApp = new Microsoft.Office.Interop.Excel.Application();
workBooks = excelApp.Workbooks;
workBook = workBooks.Open(excelpath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
workSheet = workBook.Worksheets.get_Item(1);
IsFilterExist = workSheet.AutoFilterMode;
return IsFilterExist;
}

Creates a new excel file while inserting other time in excel file in c# windows form.

I have this below function which inserts value to particualr cell in excel file. When i call this function second time it opens a new excel file and both time the value is entered into differnt excel file. How to have open single excel file and both of my value are entered into same excel file.
private bool insertIntoExcel(string pathname , string sheetname ,int excelRow, int excelColumn,string value)
{
try
{
Microsoft.Office.Interop.Excel._Application oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = true;
oXL.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Workbook mWorkBook = oXL.Workbooks.Open(pathname, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
//Get all the sheets in the workbook
Microsoft.Office.Interop.Excel.Sheets mWorkSheets = mWorkBook.Worksheets;
//Get the allready exists sheet
Microsoft.Office.Interop.Excel._Worksheet mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item(sheetname);
Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;
mWSheet1.Cells[excelRow, excelColumn] = value;
}catch
{
return false;
}
return true;
}
Do not new a Excel Application and open a workbook every time. You should keep the workbook as a handler and keep it. Use the handler to do insertion or other operations.

Open an existing Excel file in the current instance

I've written an Excel Addin for Office 2013 with C# language.
I know there is a few ways to open an existing Excel file, but all of them open the file in a new instance on Excel.
I have a current instance of Excel and want when somebody clicks on a button, the new file is opened in the current isctance(not a new one)!
for example I use the following piece of code:
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
string workbookPath = (#"C:\Downloads\Sample.xlsx");
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
But this code opens the Sample.xlsx in a new instance of excel file.
Any Idea?
Thanks in advance.
Instead of creating a new object, use Marshal.GetActiveObject:
Excel.Application excelApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Plus #LS_dev solution, I added excelApp.ActiveWorkbook.Close() command to make it work.
Excel.Application excelApp = (Excel.Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
excelApp.ActiveWorkbook.Close();
string workbookPath = (#"C:\Downloads\Sample.xlsx");
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);

How to copy an Excel Worksheet from one Workbook to another

I have been searching online for an answer to this. There are dozens of "solutions", but nothing seems to work right. The application I'm building (using C#) pulls data from a mdb querydef and creates an Excel workbook with two worksheets. That part works perfectly. Now for the part that should be simple: I have a workbook with multiple worksheets (each worksheet will calculate the data differently) I need the code to open the "template" workbook, copy the correct worksheet, and place the copy in the newly created workbook with the other two worksheets. Here's a sample of the code that I feel "should" work:
_Application xlApp;
Workbook xlTemplateWB;
Workbook xlTempWB;
object missing = System.Reflection.Missing.Value;
xlApp = new ApplicationClass();
xlApp.Visible = true;
//Open Bench Sheet Template
xlTemplateWB = xlApp.Workbooks.Open(Elmnt.getDBPath() + TEMPLATENAME, 0, true, 5, "", "",
false, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
//Open temporary workbook
xlTempWB = xlApp.Workbooks.Open(XLTempDir + XLTempName, 0, false, 5, true, "", true,
XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
//Copy "BOD" Worksheet
xlTempWB.Worksheets.Copy(xlTemplateWB.Worksheets["BOD"]);
xlTempWB.Save();
//Close Workbooks
xlTempWB.Close(true, missing, missing);
xlTemplateWB.Close(true, missing, missing);
xlApp.Quit();
//Release Objects
releaseObject(xlTempWB);
releaseObject(xlTemplateWB);
releaseObject(xlApp);
Based on my reading of the MSDN documentation, Worksheet.Copy() will only work within a Workbook. Your C# syntax looks correct.
There are a couple of ways to approach this:
Duplicate the entire template workbook (using WorkBook.SaveCopyAs()) and delete what you don't need.
Select the range you want to move and use the clipboard (Range.Select; Range.Copy; Range.Paste;) to copy it.
Hope this helps.
Thank you for your help. After trying a few more things, I found that the only thing I could get to work was copying the whole workbook and deleting the non-applicable worksheets. I feel that this is very cumbersome, so if anyone finds a better way to do it, I'm all ears. Here's the code that I ended up using:
_Application xlApp;
Workbook xlTemplateWB;
Workbook xlTempWB;
object missing = System.Reflection.Missing.Value;
xlApp = new ApplicationClass();
xlApp.Visible = true;
//Open Bench Sheet Template
xlTemplateWB = xlApp.Workbooks.Open(Elmnt.getDBPath() + TEMPLATENAME, 0, true, 5, "", "",
false, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
//Save As a temporary workbook
xlTemplateWB.SaveAs(XLTempDir + XLTempName);
xlTemplateWB.Close(true, missing, missing);
releaseObject(xlTemplateWB);
//Open temporary workbook
xlTempWB = xlApp.Workbooks.Open(XLTempDir + XLTempName, 0, false, 5, true, "", true,
XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
//Remove non-applicable worksheets
for (int i = xlTempWB.Sheets.Count; i > 0; i--)
{
if (((Worksheet)xlTempWB.Sheets[i]).Name != bchSheet)
{
xlApp.DisplayAlerts = false;
((Worksheet)xlTempWB.Sheets[i]).Delete();
xlApp.DisplayAlerts = true;
}
}
xlTempWB.Save();
//Close Workbooks
xlTempWB.Close(true, missing, missing);
xlApp.Quit();
//Release Objects
releaseObject(xlTempWB);
releaseObject(xlApp);

Categories

Resources