Hello friends,
right now i m facing a very big problem.. actually i have export data into excel from data grid view.. its done perfectly .. but i want to lock cells like first column and second column of Excel file.. and also validate locked cells.. only enter numeric value or decimal and it can not be greater then max values.. so hope u understand what i want.. if you have any solution regarding for this issues the reply me ASAP..
thanks in Advance
Loading

Sunny SharmaPosted Oct 7, 2013, 4:12 AM
You just need to set the EntireColumn as Locked.
Here is a sample code for demo:
------------------------------------------------------
Missing mv = Missing.Value;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wkb = excelApp.Workbooks.Add(mv);
Microsoft.Office.Interop.Excel.Worksheet wks = (Microsoft.Office.Interop.Excel.Worksheet)wkb.Sheets[1];
wks.Columns.Locked = false;
((Microsoft.Office.Interop.Excel.Range)wks.get_Range((object)wks.Cells[1, 1], (object)wks.Cells[1, 3])).EntireColumn.Locked = true;
wks.EnableSelection = Microsoft.Office.Interop.Excel.XlEnableSelection.xlUnlockedCells;
wks.Protect(mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv, mv);
wks.SaveAs("D:\\myFile.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, mv, mv, mv, mv, mv, mv, mv, mv);
wks = null;
wkb = null;
excelApp.Quit();
-----------------------------------------------------
Cheers !!
Tapan ParidaPosted Jul 17, 2018, 3:57 AM
santhosh PBPosted Dec 12, 2014, 4:36 AM
Sunny SharmaPosted Oct 8, 2013, 1:12 AM
I'm really glad I could be of help, thanks for the compliment :)
Like I already have mentioned on that but to elaborate on that I'm gonna explain it in detail. I think you're not clear on this point.
Of course you can do produce the grades in C# itself using switch... case statement and the output would be same but, that would not dynamically change. It means, after you generate the file and somebody modifies the marks, then the Grade is supposed to be automatically calculated and reflect the appropriate Grade. It would not happen if you just write the grades from within C#. Excel will not know it has to do something if marks change. When you put a Formula in a cell, it gets calculated automatically on every change in sheet. All the formulas in a worksheet gets re-calculated by default to reflect the change made.
This is why i suggested you to keep the criteria in the sheet and use a VLOOKUP, so that the grade changes automatically if marks is changes.
Yes, I know, you might have a thought of hiding the criteria/logic behind the Grades. If that is the case, you can set the ForeGround and BackGround Color of those cells as White or any same color, Lock it up so nobody can see it or check it.
Hope I answered your question.
Happy Learning :)
Naeem KhanPosted Oct 8, 2013, 12:21 AM
Sunny SharmaPosted Oct 7, 2013, 7:53 AM
You can do it in either way.
First is to do it using switch case from within C#, but this would not change the Grades dynamically, they will be static. It means, changing the values in sheet wouldn't change the grade.
Second one is to place a criteria on worksheet itself and let Lookup formula do this work.
See the modified sheet on how to lookup the data using VLOOKUP in excel for a range.
Steps would be as follows:
1. Add the criteria to the sheet.
2. Write data in cells.
3. Put Formulas to first cell.
4. Drag Down the formula to the end of row.
That's it :)
Naeem KhanPosted Oct 7, 2013, 6:14 AM
i m sending you excel file please download it..we have locked 2 columns now what i want that column no 3 would be accept marks from 10 . if user puts marks in column no 3 such as 5 then grade should be come autometicaly .. now where would be define it programically.. if its possible then try to come online on Gmail.. my id is [email protected]
thank u so much for helping me..
Sunny SharmaPosted Oct 7, 2013, 5:39 AM
For the current scenario, I don't see any point in calculating it using C# if It is handled by Excel Automatically. You can just set the formula in other cells and they will get calculated automatically. No need to perform validation using C# at all.
If you're still not clear on this point, show me what is your plan for doing this in C# and I'll help you with that.
Naeem KhanPosted Oct 7, 2013, 5:04 AM
Naeem KhanPosted Oct 7, 2013, 3:44 AM
Prashant SrivastavaPosted Oct 5, 2013, 8:27 AM
You can do this by setting the excel worksheet to protected via a password.
It will help in such that --
If you will normally set it to protected then that can be changed by changing the Protected setting mentioned under review TAB of Excel sheet.
But if you will protect it with password no body will be able to unprotect it (i.e. probability of making modification to excel will be less).
Regards,
Prashant Srivastava
Sunny SharmaPosted Oct 5, 2013, 7:50 AM
So here's how it's done.
By Default every cell is locked, so you can do it in two ways:
1. Either you choose to Lock only the cells that you do not want to get Edited, OR
2. you choose to Unlock only the cells that you want to get edited, all other cells will remain locked.
try this:
Here, I'm going with the first approach:
--------------------------------------------------------
...
xlsWorksheet.Cells.Locked = false; // this will unlock all the cells
xlsWorksheet.get_Range("A2", "B2").Locked = true; //select a range and set locked=true.
xlsWorksheet.Protect(missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing); //activate the protection and that's it.
...
--------------------------------------------------------
Happy Coding :)
Naeem KhanPosted Oct 5, 2013, 6:58 AM
Veena SardaPosted Oct 4, 2013, 6:49 AM
Sunny SharmaPosted Oct 4, 2013, 6:46 AM
what do you mean by locking? Do you want to Freeze a column or you want it to lock so nobody can modify the values?