Marshal.ReleaseComObject(...) issues - c#

I use said invocation to release some Excel objects after I'm done. Is it necessary to set references to null afterwards (like in the following code) ?
var dateCol = sheet1.get_Range("C4", "C" + rowOffset);
dateCol.NumberFormat = "Text";
Marshal.ReleaseComObject(dateCol);
dateCol = null;

This is what I always use and it works very well
using Excel = Microsoft.Office.Interop.Excel;
Excel.ApplicationClass _Excel;
Excel.Workbook WB;
Excel.Worksheet WS;
try
{
_Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
WB = _Excel.Workbooks.Open("FILENAME",
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
//do something
}
catch (Exception ex)
{
WB.Close(false, Type.Missing, Type.Missing);
throw;
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WB);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_Excel);
}
Do a
System.Runtime.InteropServices.Marshal.FinalReleaseComObject
On all referenced Excel objects

Related

Block Excel Column using C#

I am developing a windows form application using C#. In this application the user will click a button then the program will copy some columns and rows from the clipboard and past them on a new excel workbook where user can edit the information.
In Excel, I want to block one column only which is the ID such that the user can edit all cells except
that column because this column is system generated. I am not able to get it working. below is my code
DataObject dataObj = null;
dataGridView1.SelectAll(); // copying data to clipboard
dataObj = dataGridView1.GetClipboardContent(); //
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
object misValue = System.Reflection.Missing.Value;
xlexcel = new Microsoft.Office.Interop.Excel.Application();
xlexcel.DisplayFullScreen = true;
Microsoft.Office.Interop.Excel.Range CR = xlWorkSheet.get_Range("A1", "A1");
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, false);
// for example block column A only
xlWorkSheet.Range["A1"].EntireColumn.Style.Locked = true;
// protect the sheet
xlWorkSheet.Protect(Type.Missing, true, true, true,
Type.Missing, Type.Missing, true, true, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
My problem is that after running this code and then unprotecting the sheet, the user can still edit column A. Is there a way I can protect only column A from editing?
I am using Microsoft.Office.Interop.Excel version 15. and .Net Framework 4.5.1
Any help is really appreciated.
Here try this. I don't exactly know where the issue is occurring but this might help.
DataObject dataObj = null;
dataGridView1.SelectAll(); // copying data to clipboard
dataObj = dataGridView1.GetClipboardContent(); //
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
object misValue = System.Reflection.Missing.Value;
xlexcel = new Microsoft.Office.Interop.Excel.Application();
xlexcel.DisplayFullScreen = true;
Microsoft.Office.Interop.Excel.Range CR = xlWorkSheet.get_Range("A1", "A1");
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, false);
// for example unblock columns B - Z only
xlWorkSheet.Range("B1", "Z1").EntireColumn.Locked = false;
// protect the sheet
xlWorkSheet.Protect(Type.Missing, true, true, true,
Type.Missing, Type.Missing, true, true, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
This is in no way a perfect solution to all your problems. Especially since I don't quite know how many other columns you plan to use, but this code will leave column A locked and then leave B through Z unlocked. If you have any others questions toss them in the comments.

How to export excel data using XMLMap and EPPlus

I was wondering if someone can assist me in achieving the C# code below in either EPPlus or OpenOffice XML:
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
string excelFileName = "Sample.xlsx";
string currentDirectory = Directory.GetCurrentDirectory();
string excelFilePath = System.IO.Path.Combine(currentDirectory, excelFileName);
app.Workbooks.Open(excelFilePath,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
string xmlContent = string.Empty;
// Use the map (XSD inside the Excel file)
app.ActiveWorkbook.XmlMaps[1].ExportXml(out xmlContent);
app.Workbooks.Close();
Using Microsoft.Office.Interop.Excel.Application requires installation of Microsoft Office on the server. Is there any way around this other than installing Microsoft Engine?. Basically how to read XMLMap without office.Interop?
Thank you

Microsoft.Office.Interop.Excel Exception de HRESULT : 0x800A03EC

I working on the Excel in the server and when I trying to open my Excel file with Microsoft.Office.Interop.Excel, I have this error:
Exception de HRESULT : 0x800A03EC
I check my access to my file, and my IIS account have a full control on this. This is my code :
var excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Open(
Filepath,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing,
true
);
Thanks

Excel not Visible

I am opening an existing Excel file with following process:
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = true;
Workbook w = excel.Workbooks.Open(#"E:\ishu\Test.xlsx"
, Type.Missing,
true, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
Worksheet ws = (Worksheet)w.Worksheets[1];
Microsoft.Office.Interop.Excel.Range xlRange = ws.UsedRange;
excel.Visible = true;
Excel process gets started in task manager but no Excel window get visible.
This probably isn't the answer you are looking for, but my application uses
System.Diagnostics.Process.Start(ExcelFileToOpen);
The above is run only when the Excel file exists.

Why am I getting Show method of Dialog class failed in c# when using the xlDialogPrint.show method?

I am trying to print a Microsoft Excel document in C# using visual studio and I get a Show method of Dialog class failed exception every time I attempt to execute this piece of code:
bool userDidntCancel =
ExcelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].Show(
Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Here is the full code I'm using:
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook wb = ExcelApp.Workbooks.Open(
PATH,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
try
{
bool userDidntCancel =
ExcelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].Show(
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch
{
ExcelApp.Quit();
File.Delete("Container Status.xls");
}
Is there something I'm missing?

Categories

Resources