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;
}
}
}
Related
I am trying to overwrite/edit a column in a sheet in my excel file, even though it is not throwing any error, but still it is not even updating the excel sheet.
Here is my code:
Cursor.Current = Cursors.WaitCursor;
string path = Path.Combine(Application.StartupPath, ConfigurationManager.AppSettings["ExportTemplate"]);
oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = false;
oXL.DisplayAlerts = false;
mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
//Get all the sheets in the workbook
mWorkSheets = mWorkBook.Worksheets;
//Get the allready exists sheet
mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("MetaData");
Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;
foreach (Excel.Worksheet oworksheet in mWorkBook.Worksheets)
{
if (oworksheet.Name == "Disicpline")
{
long fullrow = oworksheet.Rows.Count;
int rowcount = oworksheet.UsedRange.Rows.Count; //get row count
//long lastrow = oworksheet.Cells[fullrow, 1].get_End(XlDirection.xlUp).Row;
oworksheet.Cells[rowcount, 1].value = txtDiscCode.Text;
oworksheet.Cells[rowcount, 2].value = txtDiscplinName.Text;
oworksheet.Cells[rowcount, 3].value = txtDiscCode.Text + txtDiscplinName.Text;
oworksheet.Cells[rowcount, 6].value = txtDiscCode.Text;
oworksheet.Cells[rowcount, 7].value = txtDiscCode.Text;
}
}
oXL.DisplayAlerts = false;
mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
mWorkBook.Close(true, path, Type.Missing);
Marshal.ReleaseComObject(mWorkBook);
GC.Collect();
GC.WaitForPendingFinalizers();
oXL.Quit();
File is just a file:
You can use File.Exist()
if (File.Exists("ExcelFilenameHere"))
{
File.Delete("ExcelFilenameHere");
}
use it before saving
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 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);
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;
}
public bool StoreInExecel(String name,String csFIleName)
{
int cellno = ;//Initialize to current row no
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = (Microsoft.Office.Interop.Excel.Workbook)excelApp.Workbooks.Add(Missing.Value);
Worksheet worksheet;
// Opening excel file
workbook = excelApp.Workbooks.Open(fileName, 0, false, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// Get first Worksheet
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.get_Item(1);
// Setting cell values
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[cellno,"B"]).Value2 = "5";
((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[cellno,"B"]).Value2 = "7";
workbook.Save();
workbook.Close(0, 0, 0);
//excelApp.Quit();
return true;
}
I want to initialize "cellno" to current row index of excel file so that data should not get overridden. Is there any specific function to get filled row count?
You can use
Excel.Range range = (Excel.Range)excelApp.ActiveCell;
int cellno = range.Row;
You can also get the used range in a worksheet using the UsedRange property.