In Excel, how to show data in hidden rows and columns - c#

This is what I have tried to do to have my chart show the data in the columns I have hidden:
Excel.Application oExcelApp = new Excel.Application;
Excel._Workbook oWB = oExcelApp.Workbooks.Add();
Excel._Worksheet oWS = oWB.ActiveSheet;
Excel.ChartObjects oCharts = (Excel.ChartObjects)oWS.ChartObjects();
Excel.ChartObject oChart = oCharts.Add(10, 80, 300, 250);
Excel.Chart chart = oChart.Chart;
// these three lines work
chart.HasTitle = true;
chart.ChartTitle.Text = "Chart Title";
chart.HasLegend = false;
// I get a compile error for this line
// because the HasHiddenContent property is read-only
chart.HasHiddenContent = true;
The HasHiddenContent property is the only one I can find that looks like it might be what I'm looking for, but it is read-only. Which property can I use to tell the chart to show data in hidden columns (and rows)?

After doing the "record the macro for what you want to do and then convert VBA to c#," I found the answer to my question.
The correct property to toggle showing data in hidden columns and rows is:
chart.PlotVisibleOnly = false;

Related

Excel OLEObject button loses click event after clicking a few times

I was trying to create an Excel button in a sheet from a C# code, using a C# event hander for the button, so I don't have to involve VBA and text scripts; and I found this great solution from #
Dummy yoyo that works but "only for a while".
The solution is simple: add a shape, get it as an ole object, get the ole as a button and add a click event:
int i = 1;
private void CreateButton()
{
Microsoft.Office.Interop.Excel.Worksheet sheet = #get a sheet from Excel
sheet.Shapes.AddOLEObject("Forms.CommandButton.1", Type.Missing, false, false, Type.Missing, Type.Missing, Type.Missing, 10, 10, 100, 30);
Microsoft.Office.Interop.Excel.OLEObject oleShape = sheet.OLEObjects(1);
Microsoft.Vbe.Interop.Forms.CommandButton button = oleShape.Object;
button.Caption = "Custom Buttom" + i;
button.Click += Button_Click;
i++; //for future use, see below
}
private void Button_Click()
{
MessageBox.Show("It works!");
}
The problem:
Now, the first time I click the button on the sheet, it works! The second time "might" work. The more clicks the less chance of it working. Inevitably, the button "loses" the click event and just does nothing. I can't figure out why, what can be causing this.
I even tried to recreate the button every click with:
private void Button_Click()
{
MessageBox.Show("It Works!");
sheet.Shapes.Item(1).Delete();
CreateButton();
}
I can see the button is being recreated because of the i++ that shows in the button's caption. Even though, some clicks later, even the newly created button simply does nothing when clicked.
What can be going on? How can I solve this?
Edit:
A partial solution was found, as #Hans Passant suggested: if I keep the shape, the ole object and the button as static members outside the method, the garbage collector doesn't kill them. Nevertheless, I would like to save, close and reopen the sheet and still get the events.
Great solutions could be:
Identifying why it happens and solving it
Suggesting another way of creating the button using a c# click handler
Making it possible to save this sheet, close and reopen it with the events still attached.
A few more details:
I am creating a new Excel application from C# code and taking the first sheet of the first workbook.
This is not a VSTO Addin, I don't have access to the Globals.Factory, although I'd love to be able to get an instance of this factory and try Microsoft.Office.Tools.Worksheet methods to add controls.
My project is inevitably a class library consumed by another non-related software
The sheet must be saved and reopened later independently from the source project.
The sheet is populated before adding the button, also via C# code.
Visual Studio debugger is able to step into the button's event handler without any special action
Following way every time I click on this button not getting any issue.
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
int i = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//add data
xlWorkSheet.Cells[1, 1] = "";
xlWorkSheet.Cells[1, 2] = "Student1";
xlWorkSheet.Cells[1, 3] = "Student2";
xlWorkSheet.Cells[1, 4] = "Student3";
xlWorkSheet.Cells[2, 1] = "Term1";
xlWorkSheet.Cells[2, 2] = "80";
xlWorkSheet.Cells[2, 3] = "65";
xlWorkSheet.Cells[2, 4] = "45";
xlWorkSheet.Cells[3, 1] = "Term2";
xlWorkSheet.Cells[3, 2] = "78";
xlWorkSheet.Cells[3, 3] = "72";
xlWorkSheet.Cells[3, 4] = "60";
xlApp.Visible = true;
#region
xlWorkSheet.Shapes.AddOLEObject("Forms.CommandButton.1", Type.Missing, false, false, Type.Missing, Type.Missing, Type.Missing, 10, 10, 100, 30);
Microsoft.Office.Interop.Excel.OLEObject oleShape = xlWorkSheet.OLEObjects(1);
Microsoft.Vbe.Interop.Forms.CommandButton button = oleShape.Object;
button.Caption = "Custom Buttom" +i ;
button.Click += Button_Click;
i++;
#endregion
}
private void Button_Click()
{
MessageBox.Show("It works!");
}
but sometime button seems not clickable or not fire click even. see below screenshot.
Message show already, but we do not see it in top of the worksheet. too many time click on the button it may happened. so if you do not see the dialog just open it from taskbar.
Reopen: Controls that are added at run time are not persisted when the document or workbook is saved and closed. The exact behavior is different for host controls and Windows Forms controls. In both cases, you can add code to your solution to re-create the controls when the user reopens the document.
xlApp = new Excel.Application();
string workbookPath = #"c:/Book.xlsx";
xlWorkBook = xlApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlApp.Visible = true;
Microsoft.Office.Interop.Excel.OLEObject oleShape = xlWorkSheet.OLEObjects(1);
Microsoft.Vbe.Interop.Forms.CommandButton button = oleShape.Object;
button.Caption = "Custom Buttom" + i;
button.Click += Button_Click;
i++;
NB: Microsoft Excel 2013, project reference Microsoft office 15.0 object Library and VS 2022 work perfectly but Excel 2016 has this issue you are telling about. so I think this issue has come from
Microsoft Office specific version.
if you want to make it count clicks try this..
const btn = document.querySelector('.btn');
btn.onclick = Counter;
const clicks = document.querySelector('.clicks');
clicks.id = document.querySelector('clicks');
var a = 0;
function Counter() {
a += 1;
clicks.innerHTML = a;
}
const reset = document.querySelector('.reset');
reset.onclick = resetCounter;
function resetCounter() {
a = 0;
clicks.innerHTML = a;
}

Display cell with vertical text using EPPlus

Using EPPlus, how can i change a cells text to display as a vertical text like this,
In excel, you can do this by clicking on this button when setting cell orientation,
I'm trying to play around with the .TextRotation but this does not achieve what I want, setting it to something like 180 degrees will give me something like this,
ws.Cells[row, 2].Style.TextRotation = 180;, .TextRotation only accepts an integer value so I was wondering how I can get the "Text" buttons value,
Its definitely a bug you found. There is a way but it is pretty ugly. You can use the StyleID created by the cell when you change it to anything other than the default:
[TestMethod]
public void Text_Rotate_Test()
{
//https://stackoverflow.com/questions/57603348/display-cell-with-vertical-text-using-epplus
var fileInfo = new FileInfo(#"c:\temp\Text_Rotate_Test.xlsx");
if (fileInfo.Exists)
fileInfo.Delete();
using (var pck = new ExcelPackage(fileInfo))
{
var workbook = pck.Workbook;
var worksheet = workbook.Worksheets.Add("Sheet1");
var cell = worksheet.Cells[1, 1];
cell.Value = "Test Text Value";
//Trigger epplus to create a new style specific for the cell.
//This needs to be done even thought it will be overridden in
//order to ref by index. But have to be careful not to step
//on other styles so make it as unique as it needs to be.
cell.Style.TextRotation = 180;
//Make sure the update the xml before looking up by index
workbook.Styles.UpdateXml();
workbook.Styles.CellXfs[cell.StyleID].TextRotation = 255;
pck.Save();
}
}
Which gives this:

C# - Select Cell at Bottom Right corner

I am looking for a way to move my selected cell from the top left to the bottom right. I was trying to use xlDirection but that highlights everything and doesnt allow me to do a combination of movements.
A B C
1 4 7
2 5 8
3 6 9
I start at A and now only want to focus on 9. The size of the excels change so i cant specify the actual cell to look for each time.
I was hoping there are similar commands as Ctrl+Down or Ctrl+Right that would put me on the cell.
can't you send keystrokes to the object?
A reference for all key strokes can be found here:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.sendkeys?view=excel-pia
public class Navigator
{
private Excel.Application excel;
private Excel.Workbook workbook;
public void NavigateToBottomRight(string filePath, string worksheetName)
{
excel = new Excel.Application();
excel.Visible = true;
workbook = excel.Workbooks.Open(filePath);
var worksheet = workbook.Worksheets.Cast<Excel.Worksheet>().FirstOrDefault(x => x.Name == worksheetName);
Excel.Range cell = worksheet.Cells[1, 1];
cell.Activate();
string controlRight = "^{Right}";
string controlDown = "^{Down}";
excel.SendKeys(controlRight, true);
excel.SendKeys(controlDown, true);
//Do other work here
workbook.Save();
excel.Quit();
}
}
Hope this helps!

How to increase the font size of a particular cell in excell using c# [duplicate]

I am working on a project that writes data to an Excel file.
Everything is finished now, however I need a few cells with a bigger size than the rest (title, etc).
I have read about this about the internet, but I keep having the same problem: when I execute my code (see below for what I have tried), everything in the worksheet becomes larger.
What I already have tried:
worksheet.Rows[1].Cells[7].Style.Font.Size = 20;
worksheet.get_Range("A7", "A7").Style.Font.Size = 20;
None of this seems to work; what is the correct way to increase a cell's font size?
I had to use:
worksheet.get_Range("A7", "A7").Cells.Font.Size = 20;
If the data is consistent and will always be written to the same cells then this is the simplest solution - works well for product details / contact info type exporting
// set cell A7
worksheet.get_Range("A7", "A7").Font.Size = 20;
// set cells A7, A8
worksheet.get_Range("A7", "A8").Font.Size = 20;
// set cells A7, B7
worksheet.get_Range("A7", "B7").Font.Size = 20;
// set cells A7, A8, B7, B8
worksheet.get_Range("A7", "B8").Font.Size = 20;
If the data varies and will sometimes be written to multiple rows/columns then something like this is more simple - works well for dataset / shopping list type exporting
int RowNum;
int ColNum;
// some code to set variables
worksheet.Cells[RowNum, ColNum].Font.Size = 20;
I would just use:
worksheet.Range["A7"].Style.Font.Size = 20;
edit: sorry, wrong brackets
When working with interop excel, try not to write your code with "two dots" in order to clean interop excel objects.
This also helps having your code more readable.
Anyway, to answer your question, and using what I have pointed out... all you have to do is:
//Declare your variables
Application excel = null;
Workbook excelworkBook = null;
Range excelCellrange = null;
Worksheet worksheet = null;
Font excelFont =null;
//start your application
excel = new Application();
try
{
...
//your code goes here...
excelCellrange = worksheet.Range[worksheet.Cells[1,7],worksheet.Cells[1,7]];
excelFont = excelCellrange.Font;
excelfont.Size = 20;
...
...
}
catch(Exception ex){
}
finally{
//here put something to clean the interop objects as the link above.
...
Marshal.ReleaseComObject(excelfont);
...
}

Can't display linklabel text in a datagridviewlinkcolumn

I have a little but pretty annoying problem:
I have created a datagridview and bound it to a datasource.
Then now I want to add a column which will display links for the user to click.
In order to do that i added a datagridviewlinkcolumn. For each rows of the datagrid I set the value of the cell in that column to the text i want to be displayed. But it shows nothing. All the datagridlinkcolumn is filled with "blank text".
Here is my code:
DataGridViewLinkColumn dgvColDeletion = new DataGridViewLinkColumn();
dgvColDeletion.Name = "Deletion";
dgvColDeletion.HeaderText = "";
dgvColDeletion.ReadOnly = false;
dgvTrainings.Columns.Add(dgvColDeletion);
foreach (DataGridViewRow row in dgvTrainings.Rows)
{
row.Cells["Deletion"].Value = "Delete";
}
dgvColDeletion.Update();
dgvTrainings.Update();
I also tried with setting directly linklabels or datagridviwlinkcells, but the problem still remains.
I cant get any clue why this isn't working.
Any help will be very much appreciated, thanks.
To display the same link text for every cell, set the UseColumnTextForLinkValue property to true and set the Text property to the desired link text.
DataGridViewLinkColumn dgvColDeletion = new DataGridViewLinkColumn();
dgvColDeletion.UseColumnTextForLinkValue = true;
dgvColDeletion.Text = "Delete";
Try this. I hope this will helps to you.
DataGridViewLinkColumn dgvColDeletion = new DataGridViewLinkColumn();
dgvColDeletion.UseColumnTextForLinkValue = true;<br/>
dgvColDeletion.Text = "Delete";<br/>
dgvColDeletion.ActiveLinkColor = Color.White;<br/>
dgvColDeletion.LinkBehavior = LinkBehavior.SystemDefault;<br/>
dgvColDeletion.LinkColor = Color.Blue;<br/>
dgvColDeletion.TrackVisitedState = true;<br/>
dgvColDeletion.VisitedLinkColor = Color.YellowGreen;<br/>
dgvColDeletion.Name = "Delete";<br/>
dgvColDeletion.HeaderText = "Delete";<br/>
if (grid_shared.Columns.Contains("Delete") == false)<br/>
{<br/>
dgvColDeletion.Columns.Add(lnkDelete);<br/>
dgvColDeletion.Columns["Delete"].Width = 40;<br/>
}<br/>
Happy coding..:)

Categories