I am creating a program which writes data into Excel cells. The problem is that Excel does not allow autofit on merged cells. Thus, I have to manually adjust width and height.
I have tried the following:
xlWorksheet.Range[xlWorksheet.Cells[1, 4], xlWorksheet.Cells[1, 4]].ColumnWidth = 10;
and
xlWorksheet.Range[xlWorksheet.Cells[1, 4], xlWorksheet.Cells[1, 4]].EntireColumn.Width = 10;
and all other codes I searched but all of those returns an error which says Unable to set column width
I don't have anything else left to do. Your help will be very much appreciated. Thanks.
you can directly set a column width in this way
xlWorkSheet.Columns[3].ColumnWidth = 16;
for the worksheet, you need to loop through the columns that you need to set their width
for(i=0; i<10; i++)
xlWorkSheet.Columns[i].ColumnWidth = 16;
Related
I need a TableLayoutPanel which should be able to change to different grids: 6x6, 10x10 and 16x16. The cells contain a PictureBox with black or white squares as images. Currently I have it where all cells are the same size except those in the last col and row. I am calculating and setting RowStyle to percent in code. E.g 16x16 is 6.25% per row and col.This is the current outcome.
private void AddRowAndCols(int rowColCount)
{
//default is 6x6 so do nothing
if (rowColCount == 6)
return;
else
{
//set row & col count
TlpGameBoard.RowCount = rowColCount;
TlpGameBoard.ColumnCount = rowColCount;
//Calc % each row and col should be
float rowColPercentage = CalculateRowColPercentages(rowColCount);
//clear all current row & col styles as these are still set to the default
TlpGameBoard.ColumnStyles.Clear();
TlpGameBoard.RowStyles.Clear();
//Add new row & col styles
for (int i = 0; i < rowColCount; i++)
{
TlpGameBoard.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, rowColPercentage));
TlpGameBoard.RowStyles.Add(new RowStyle(SizeType.Percent, rowColPercentage));
}
}
I am calculating the % by doing: 100F/numberOfRows as type Float.
The TLP Size is 800, 800.
I have also tried to implement the solution from
Creating same size cells with TableLayoutPanel however this is not working for me.
New to Stack Overflow and programming so apologies for any poor coding/posting practises.
Edit- how do I ensure the cells always stay equal size compared to each other no matter what grid they are? The size of the TLP need to stay the same too.
Thanks
I'm trying to increase the height of rows after auto fitting them.
For example: first row height is 15
I autofit them by:
//The first row
var start = sheet.Dimension.Start;
//The last row
var end = sheet.Dimension.End;
for (int row = start.Row; row <= end.Row; row++)
{
//This makes Excel auto fit the row but content
sheet.Row(row).CustomHeight = true;
}
So the first row height is now :30
Then, I want to increase each row size by adding this code in the loop
double x = sheet.Row(row).Height;
sheet.Row(row).Height = 1.25 * x;
The Problem is Excel doing the auto fitting when opening the Excel file so EPPlus doesn't get the updated row height information but the old infomation.
So the row height is now 18.75 (15 * 1.25) instead of 37.5 (30 * 1.25)
You cannot. As you say "Excel doing the auto fitting when opening the Excel file" - so that is long after EPPlus has been working on that file. All you do (and can do) is to tell Excel to adjust the height accordingly.
(That is different with columns: EPPlus can perform sheet.Column(column).AutoFit(); and set the width to an appropriate value - but not for row heights, unfortunately. Mayby this is added in the new, commercial version of EPPlus?)
I think your option is do some experimenting and try to calculate and set the height yourself in EPPlus.
Or you run a VBA Script in your Excel File to do the job - this is no problem, it goes like this:
With Sheets("Formular").Range(Cells(14, 1), Cells(14 + i - 1, 4))
.EntireRow.AutoFit
.VerticalAlignment = xlTop
For Each r In .Rows
r.RowHeight = r.Height + 5
Next r
End With
This might not be an option for you, of course, when you are not allowed to run Macros in your environment.
I am trying to recreate a table like this:
I am using the DocX library to manipulate Word files, but I'm having trouble getting the widths right. Trying to set the widths of cells only seems to work when it's not set to the window autofit mode, and it only seems to resize when the specified width is greater than half of the table width, or rather, I can make a cell bigger than half the width but not smaller.
What would be the simplest way to reproduce the intended table?
I found the answer to this myself. In order to properly set the width, you have to loop through each cell in a column and set every width. This will not work with any autofit options selected.
Try this :
Table table = doc.AddTable(2, 2);
table.SetColumnWidth(0, 500);
//first is column index, the second is column width
Bit of an old post to tag to, but after having the same issue it would appear that none of the widths on either the cells or columns actually work, so as a dirty workaround, you can loop through each column and cell adding text to each of the cells, make the text white and finally use the autofit option to autofit to contents eg.
Table t2 = doc.AddTable(2, 8);
for (int i = 0; i < t2.RowCount; i ++)
{
for(int x = 0; x < t2.ColumnCount; x++)
{
t2.Rows[i].Cells[x].Paragraphs.First().Append("12").Color(Color.White);
}
}
t2.AutoFit = AutoFit.Contents;
doc.InsertTable(t2);
This is the way:
Table t = doc.AddTable(1, 5);
t.SetWidthsPercentage(new[] { 20f, 20f, 40f, 10f, 10f }, 500);
The float array sets width percentage for each of the columns, second parameter is the total width of the table.
I'm writing some code for my NopCommerce webshop, that needs to export an excel sheet.
I'm trying to edit the width of the columns, so the text will fit in.
I already tried the following code(for 26 columns):
//adjust excel column width
for (int i = 0; i < 26; i++)
{
worksheet.Column(i, 1).Width = 45;
}
The code executes and the Width of the columns is 45, so you could say it works.
But it gives me the following error at start up:
How can I get rid of this error?
Thanks in advance!
have you tried worksheet.Column(i).Width = 45; ?
I am currently using the following code to fill cells in the range A1 to A5, and B1 to B5, with yellow:
chartRange1 = xlWorkSheet.get_Range("A1", "A5");
chartRange1.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
Excel.Range chartRange2;
chartRange2 = xlWorkSheet.get_Range("B1", "B5");
chartRange2.Interior.Color= System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
But it takes 2 chartRange objects in order to do this. What if I want to set the same color over a wide range of cells?
Is there a way to do this using a single statement that sets the same color for a larger range of cells?
In your case (A1:A5, B1:B5, C1:C5) you can merge the cells into a contiguous range A1:C5:
xlWorkSheet.get_Range("A1:C5");
But a range does not have to be contiguous. You can also use code like the following:
xlWorkSheet.get_Range("A1:A5,C1:C5,F10:F15");
Have you tried
chartRange = xlWorkSheet.get_Range("A1", "B5");
Are you able to just set the chartRange1 object to null, then reinitialize and set the next range?