I have an excel sheet which contains a Signature Line. I tried to copy this sheet to another workbook, but the values of the sheet can be copied, except the signature line. I think this copy method is based on cells.
How to get signature line to another workbook. Here is my code
Excel._Application xlApp;
Excel.Workbook xlTemplateWB;
Excel.Workbook xlTempWB;
object missing = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlApp.Visible = true;
//Open Bench Sheet Template
xlTemplateWB = xlApp.Workbooks.Open(#"C:\TempProjects\test.xlsx", 0, true, 5, "",
"", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
//Save As a temporary workbook
xlTemplateWB.SaveAs(#"C:\\TempProjects\mytmp.xlsx");
//Open temporary workbook
xlTempWB = xlApp.Workbooks.Open(#"C:\TempProjects\my.xlsx", 0, false, 5, true, "",
true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlTemplateWB.Worksheets.Copy(xlTempWB.Worksheets["Sheet1"]);
xlTemplateWB.Close(true, missing, missing);
releaseObject(xlTemplateWB);
xlTempWB.Save();
//Close Workbooks
xlTempWB.Close(true, missing, missing);
xlApp.Quit();
//Release Objects
releaseObject(xlTempWB);
releaseObject(xlApp);
Related
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.
I am trying to copy an existing excel sheet into my current excel file. I am using this code.
Workbook wkActive = Globals.ThisAddIn.Application.ActiveWorkbook;
Microsoft.Office.Interop.Excel.Workbook workbook = Globals.ThisAddIn.Application.Workbooks.Open(IdsTemplatePath, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
worksheet.Copy(Type.Missing, wkActive);
wkActive.Save();
but when in copy method i get error
Exception from HRESULT: 0x800A03EC
what i am doing wrong and what should i do for completing my task
I got the solution
Microsoft.Office.Interop.Excel.Workbook CurrentWk
= ((Microsoft.Office.Interop.Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook);
if (Path.GetExtension(CurrentWk.Name) != ".xlsx")
{
MessageBox.Show("Please save the document before complete this task", "warning");
}
else
{
Microsoft.Office.Interop.Excel.Workbook workbook = Globals.ThisAddIn.Application.Workbooks.Open(IdsTemplatePath, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
totalSheet = workbook.Worksheets.Count;
for (int sheetNum = 1; sheetNum <= totalSheet; sheetNum++)
{
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[sheetNum];
sheetname = worksheet.Name.Replace("\r", "").Replace("\a", "").Trim().ToLower();
if (sheetname == "ids_template")
{
try
{
worksheet.Copy(CurrentWk.Sheets[1]);
workbook.Close(SaveChanges, Type.Missing, Type.Missing);
}
catch { }
ids_template_found = true;//here it is set true because template sheet is added above in current workbook.
break;
}
}
}
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;
}
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);
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);