Write to an Excel file while it's open - c#

I'm developing a WindowsForm application where I am storing an accumulator on a destinated variable. Actually, my code is working but what I want to do now is to see the accumulator integer on an Excell cell in real-time.
So I would like to know if there is a way to write/export values from my Form to my Excel file while it is open. The idea is to see the cells being refreshed every time I send the data.
Actually, I am storing the values but with the only condition that the file is closed, so I'm curious if this is possible?
This are the actual lines of code that I have:
SLDocument sl = new SLDocument (#"D:\Excel.xlsx"); sl.SetCellValue("A2","Accumulator");
sl.SetCellValue("A3", myaccumulator);
sl.Save();

I just fixed the problem in case that one of you want to handle with data as this way, i just created other File and programmed a Macro with vba to export the data of the closed File, so now I can see the cells being refreshed every time with one Click button.
Thanks for your help, regards to everyone.

Related

C# Excel Add in : Make worksheet read only but editable through code

I have created an Excel add-in in C#, In my add-in I have a form that retrieves data from web service and writes it to excel, I want to make the excel sheet read only, but I also want to edit it from behind the code.
What i want to do:
Basically I want to block user from accessing the worksheet, he can only make changes through the form that I have created.
I tried:
1- To add cell validation to forbid any changes to the cell but it still allows other operations on sheet such as delete rows columns etc.
2- To protect worksheet using a password like this.
var activeWorksheet = ((Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet);
activeWorksheet.Protect("password");
But the above will not let me edit the worksheet through code, I have to unlock the worksheet whenever i want to make changes ie.
activeWorksheet.Unprotect("password");
// Do some changes.
activeWorksheet.Protect("password");
This way worksheet will stay unprotected while my Edit code is running (it may consume some time as there may be thousands or more records in sheet).
I can think of one way is to unlock invidual cells to edit them and lock them again, I tried the below code but its not working:
var cellToEdit = activeWorksheet.Range["A2"];
activeWorksheet.Protection.AllowEditRanges.Add("A1",cellToEdit , "password");
cellToEdit.value = "changed cell";
Please suggest me some solution.
This way worksheet will stay unprotected while my Edit code is running (it may consume some time as there may be thousands or more records in sheet).
If you display a modal form (e.g., progress dialog) during the update processing, the user will not be able to make edits while the sheet is unprotected.
I have tested writing a code than runs longer say 3 to 4 seconds and it seems that by default, excel doesnt allow worksheet to be edited while the code in still running.

How to Insert Values in Specific Excel Cells From DataGridView

Can you please help me to figure out how I can insert DataGridView values into a pre defined excel template (Into Specific Cells)?
I have a DataGridView on my windows Form which is getting the values from user input. Now I would like to enable users to export the DataGridView values into an excel file (A File like attached excel file).
As far as I know I have to create the headers and add them to the code programmatically but for the DataGridView part, honestly I have no idea how I can do that?
As you can see the Form (Box) is starting from B2 to K2 and end from B21 to K21 Now my question is how i can start importing values from B4 - k4 and so on?
Is there any way I can format the style of the cell (like Background color or font style and size) from C#? I mean generating a form like what is looking in attached Excel programmatically.
Thanks for your time in advance
Not sure if this is what you're looking for but ff you are using Office Interop, you can insert a 2-dimensional array into a range in Excel.
The following snippet might not be correct (no VS nearby and I haven't used excel automation for a long time) but you'll get the picture.
Excel.Range oRange = oSheet.Range("B2",Missing.Value);
oRange.Resize(myArray.GetLength(0),myArray.GetLength(1));
oRange.Value = myArray;

Excel Doc after c# modifications: Ctrl + DownArrow is not working properly

guys!
After I create Excel document in my c# application the Ctrl+DownArrow combination doesn't work properly.
It goes to the last row in the current column. So looks like it means all sheet as one data region WITHOUT empty cells.
so I guess empty cells in my excel sheet aren't empty for Excel.
How to figure it out?
Thank you,
Try creating a macro of what you want to happen. Then look at the macro code which is based off of interops calls. See if it sheds a light on your issue and how to resolve.
Just filter out the "Blanks" Ctrl+Down Arrow and all the blank cells would be selected, now press delete (From key board).
DONE. Now you can jump between entries with Ctrl+Down Arrow.

Read from text file and show in table C# form

I want to read data from a text file and show in a table (much like the DataGrid / DataGridView classes). The txt file must be local and not sql based (like the mentioned classes).
Doing this in C# windows form.
Any ideas?
If you have any questions I will edit this question as needed.
EDIT:
This seems to work:
this.DataGridView1.Rows(1).Cells(1).Value = stringReader;
Open up a filestream and parse your values into a DataTable... then set your grid's datasource to the datatable. Try that out and update if you have any problems :)

How do I clear particular range of cells in VSTO excel using C#

I have 2 questions.
I want to clear a range of cells in Excel VSTO apps.
When the user opens the Excel for the first time, I want to populate a particular cell with the user's name and address. After that I want it to be saved in the excel and it should not be populated if the user opens again. How do I accomplish this task
For question 1:
private void ClearRange(string rangeAddress)
{
var rng = worksheet.get_Range(rangeAddress, Type.Missing);
rng.Clear();
}
For question 2: You're going to need to implement an event handler. Unfortunately there is no event handler for "open Excel for the first time". So you'll need to figure out a programmatic solution for keeping track of what the "first time" is, and saving that information somehow. I'd recommend grabbing a good book on VSTO (here's my personal recommendation).
That being said, I think you're going to have to work with Application.WorkbookOpen.
In the WorkbookOpen event handler, you should put the code to set the user's name and address to their respective cells. You can also choose to save a CustomProperty to mark the fact that you've populated these fields at this time.

Categories