I have an excel file(VSTO), which will be uploaded with data.
However I want to make one cell invisible, as it contains one of the ID's which should not be revealed to the user.
I use Excel.Range.Value2 property to set the value.
Now I want to make the value inside it invisible
I am using C#, how do I accomplish the above.
This will make cell content invisible, untill you click on them then you can change the data. Here
Microsoft.Office.Interop.Excel.Range descriptionRange31 = xlApp.get_Range("A18,A25,B18,B25");
descriptionRange31.NumberFormat = ";;;";
I am not sure how to make it invisible. However, you can just copy and save the value in a variable in memory and replace it by string.Empty.
Declare the variable in your plug-ins application object in such a way that it stays alive as long as Excel is open.
If you want to persist it even when Excel application is closed, then either write it to disk or save it in the properties of the Excel workbook (the same place that is visible in Properties page of the workbook) after encoding it.
I have worked on the VSTO Excel applications.
In my project i have used specials Ids to identify each column uniquely..
So, i would suggest rather than getting into coding you can do following 2 actions to make it invisible.
Go to ur visual studio, open design mode of the workbook.
Enter ur ids in columns or rows , wherever u want to (lets assume in column C and Row 5,6)
1.You can see the horizontal / vertical strips at the top and left side of excel which we use to resize the columns n rows ..just adjust it in a such a way that that particular column will be hidden
as u can see the image, i adjusted columns to Hide Column "C" and Rows to hide row no 5 and 6.
2.Now open excel (not from Visual stodio, from windows explorer) pres Alt+T+P+P
and protect the sheet using a perticular password and your are done.
Now user can not make any changes in columns or rows and date remains invisible.
Regards,
Sangram Nandkhile
I'm not sure you can make the contents of a cell "invisible." However, you could go a long way toward that goal by:
Placing the hidden information within a hidden column in a separate worksheet
Password protecting the worksheet structure to prevent unhiding of that hidden column
Hiding the protected worksheet
Password protecting the workbook's VBA project from editing
The security of this approach is not 100% because Excel protection is not exceedingly difficult to break. However, the combination of obscurity (since the worksheet tab is not even visible to the user) and protection of the VBA Project from editing (so even if someone knows the hidden sheet is there, it won't be as easy to code the unhide method) and password protection of the worksheet structure (so it won't be easy to unhide the protected column) should prevent all but the most determined peekers from seeing what you're hiding.
I have not worked with VSTO, so I can't code this for you. The approach is valid though, and all API hooks to achieve it are probably available.
I'm not sure if this answer match your level of question, but this if I would into your problem description. I would just make the font color same as the background and lock the cell from editing (which you of course handle from code, while write/change the value) and go for locking the excel sheet except the cells/regions that explicitly marked as opened.
Related
I got stuck for setting "ReadOnly" column using "ClosedXML" utility for export to Excel.
I am able to export records into Excel, however the first "ID" column I need to keep it read only, user should not type anything. If he want to add new row in the exported excel he can, except "ID" column. Please help me to solve this functionality (ws is my worksheet).
ws.Column("ID").Style.Protection.SetLocked(true);
Even I am setting "SetLocked" it is editable and I need non-editable field.
You need to also switch protection on for the worksheet with
ws.Protect()
See the documentation and this question for more details.
at first you need to protect the whole sheet then unlock only the cells you want to be editable as following
ws.Protect("yourPassword"); //hint: password parameter is optional
ws.Column("ID").Style.Protection.SetLocked(false); //set to false
I've managed to copy from excel and paste it into a datagridview in C#. But when I hold down ctrl, select columns that aren't adjacent to each other, copy and paste into datagridview - all the columns in between were also pasted. Is there a way around this without actually opening the Workbook?
Current implementation simply retrieves Clipboard.GetDataObject().GetData, formats that into a string and delimit by tab.
Unfortunately excel just copies everything not just your selected columns for outside world. You can view what is copied to the clipboard using a clipboard viewer (I've used Free Clipboard Viewer). I've created a worksheet with following
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
and selected and copied columns 2, 4 and 6. clipboard viewer shows me what I've copied not three columns but all columns from 2 to 6 (actually Sheet!C2:C6).
You can always open excel using interop or 3rd party component and get what you need and paste it manually which I had to do for converting a grid to excel like grid. I manually parsed comma separated values and set the cells individually.
edokan is right when he says that the middle columns are also copied. Is there a setting in Excel which can prevent that? NO.
However here is the simplest way to do achieve what you want.
Before copying Delete unnecessary columns. Then copy and paste. Once done you have two options
Close the Excel file without saving
Press CTRL + Z to undo.
If you want a Code solution then yes there is a possibility. Create a VSTO Add-In. What this Add-In will do is copy the relevant columns to clipboard. Not the same as CTRL + C
I have a C# WinForm DataGridView.
The program should be able to change the cells values in the DataGridView, but the user should not be allowed to.
I.e.: how can I differentiate whether the user or the program is editing a cell in a DataGridView?
So far I only found the readonly property (e.g. of a column). But then the program is not able to edit the column either.
I could leave the column readonly=true and when the program wants to change a cell value, make readonly=false, change value, readonly=true...but this solution seems very bad to me, especially if a column needs constant update from the program (e.g. a financial price data stream), but i don't want the user to change the price (even though it would be overwritten soon by the program, which might or might not happen soon).
Thanks,
Imran
You mean this?
dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
You can use the 'Enabled' property of the DataGridView to enable user input of the entire data grid.
I have a ListView that I need to export to an Excel spread sheet. There a bunch of documentation to export GridViews and to a lesser extent ListViews to Excel but not so much in terms of doing some customization before export.
Before exporting I need to prep the data, as a) only a subset of the columns need to be exported b) some of these columns are editable textbox controls.
Doing a simple export without preping the table means that the control gets exported as well which obviously looks really ugly.
At the moment I can iterate the controls and remove the formatting from both the Grid and ListViews thus solving b.
In the GridView and I can just set some the columns I want to hide using Visible = false; and then set back to true when I have finished.
The thing I can't figure is how to do this with the listview.
I am wondering what is the best way to do this as I am kind of stuck.
Thanks,
Michael
I actually think this is a dumb question. If I want to export the list view to excel then i'll export all columns removing the unnecessary formatting. Then users can do any customization of the columns in Excel.
To do any customization of the data prior to export then I should use the underlying data types.
The above approach (admittedly my own) is simply bad.
m trying to break one large Excel spreadsheet into several. I've made good progress, but I'm running into some problems. Specifically, the values that get copied over don't retain their format (for instance, 40322 instead of 5/24/2010 and -101 instead of (101.00) ). I've tried using the style (see below) but that doesn't even get me the font, let alone the number format. Any help or a poke in the right direction would be appreciated.
There are 2 loops, one for row, one for column.
destinationSheet.Cells[i, j].Style = sourceSheet.Cells[i, j].Style;
Instead of looping for each cell, you can copy/paste the entire range of cells using the pastespecial method.
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.namedrange.pastespecial(VS.80).aspx