Block Excel Column using C# - 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.

Related

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.

Remove excel document action pane

We can attach document action pane to any excel file using Developer-> expansion pack-> Microsoft action pane -> attach.
Once the excel is saved and opening it again will have previously stored excel document.
I have an Excel file (refer image) and I wish to remove(detach) document action pane using inter op programming in c#. Is it possible do so?
code snippet :
Application xlApp = null;
try
{
xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
string versionNo = xlApp.Version;
xlApp.Visible = true;
Microsoft.Office.Core.MsoAutomationSecurity mso = xlApp.AutomationSecurity;
xlApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
Workbook newWb = xlApp.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);
// Here want code to detach document action pane.
}
Try this:
Microsoft.Office.Interop.Excel.Application.DisplayDocumentActionTaskPane = false;

Exporting excel sheet to xml spread sheet 2003

I'm trying to export an excel sheet into xml spread sheet 2003 in order to apply some xsl on the result xml file ,
so , i have tried to save the sheet i'm working on using XlFormat enum as follows :
Excel.Application app = new Excel.Application();
Excel.Workbook workbook;
Excel.Worksheet NwSheet;
workbook = app.Workbooks.Open(#"C:\file.xlsx",
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);
NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
string outfile = #"c:\temp.xml";
NwSheet.SaveAs(outfile, Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
This code generated an xml file with the following problems :
1-cells of type date are generated as :
2011-10-10T00:00:00.000 , i want it to be 10/10/2011 exactly as it was in the excel sheet so how can i access to these cell's types in order to change them ??
2-numbers as 8.2 were generated as 8.1999999999999993 why did that happen and how can i get it just as is it in the excel sheet
i guess both are faces of the same problem , i just need the data as it appears in the excel sheet.

How to read data from Excel file in C#?

I create an Excel file from c# with data validation-it seem like combo with chosen possibility
string mList1 = "=ProductCode";
oRng = oSheet.get_Range("H8", "H9");
oRng.Name = "ProductCode";
int t = dt.Rows.Count + 2;
string st = "F" + t;
oRng = oSheet.get_Range("F2", st);
oRng.Validation.Add(XlDVType.xlValidateList,
XlDVAlertStyle.xlValidAlertStop,
Missing.Value, mList1, Missing.Value);
Now I want to read the Excel file and also the chosen item from the combo. I have successfully read all the data but the data validation.
Read the data-
Microsoft.Office.Interop.Excel.Application ExcelObj = null;
ExcelObj = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open("C:\\Documents and Settings\\rachelg\\My Documents\\xxx.xls"
,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);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
for(int x = 1; x <= 5; x++)
{
string sd = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[x, 1]).Text.ToString();
System.Console.WriteLine(sd);//this one column
}
in different column I have the data validation but I don't know to access into it.
That seems a bit much for what is largely a simple operation.
Whilst not answering your question directly, this post I made a while back might help: accessing data record from Excel in VB.NET.

Programatically setting page setup options in Excel

I have a ASP.NET Web App which is copying a Report to an Excel Sheet by creating an HTML table and copying the contents.
I want to fit the Excel report into 1 page before firing the print option. This needs to be done programmatically while I'm generating the Excel workbook.
Following is the code that I used to open an Excel workbook and print it with custom printer settings:
Dim xl As New Excel.Application
xl.DisplayAlerts = False
xl.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, Type.Missing)
Dim sheet As Excel.Worksheet
Dim ws
Try
For Each ws In xl.ActiveWorkbook.Worksheets
ws.Select(Type.Missing)
With ws.PageSetup
.PaperSize = Excel.XlPaperSize.xlPaperA4
.Orientation = Excel.XlPageOrientation.xlLandscape
.Zoom = 80
.BottomMargin = 0.25
.LeftMargin = 0.25
.RightMargin = 0.25
.TopMargin = 0.25
.FitToPagesWide = 1
End With
ws.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
Next
Catch exp As Exception
MsgBox("Unable to setup printing properties for the sheet." & Chr(13) & "Check if you have printer installed on your machine.", MsgBoxStyle.OKOnly)
Finally
xl.Workbooks.Close()
xl.Quit()
While (System.Runtime.InteropServices.Marshal.ReleaseComObject(xl) > 0)
''do nothing
End While
xl = Nothing
End Try

Categories

Resources