Change width and height of column using ExcelPackage - c#

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; ?

Related

C# TableLayoutPanel make all cells the same size

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

Getting the height of excel row after auto-fitting and then increase it Epplus

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.

C# EPPLUS Chart SetPosition Method Row Offset does not work

I am creating a chart to export to excel. I need to create several, so I would offset them using the SetPosition() method with the 4 parameters:
SetPosition(int row, int rowoffset in pixel, int col, int coloffset in pixel)
thus
chart.SetPosition(startRow, 350*i, 0, 50);
The problem is that the second row offset parameter stretches the chart by 350*i pixels higher. This must be a bug since the col offset 4th parameter works fine and as expected.
I need to use startRow to start at a specific row cell in the sheet, so I need to get the row offset to work somehow.
Any ideas?
The RowOffset and ColumnOffset have given me trouble as well and I avoid using them in that function. If you look at the source could you can see it doing alot of match with the chart height/width so it seems to do more then just set and top/left position. I have not gone through the effort to fully reverse engineer it.
I just do the math myself. Knowing the default row height in pixels is the only thing that you have to watch out for since this can be customized by the user which you cannot know at design time Here is what I do:
using (var pck = new ExcelPackage(fi))
{
var workbook = pck.Workbook;
var worksheet = workbook.Worksheets.Add("Sheet1");
worksheet.Cells.LoadFromDataTable(datatable, true);
//Set specific values to make predictable
const int EXCELCHARTWIDTH = 375;
const int EXCELCHARTHEIGHT = 350;
//Have to assume a standard screen dpi but can be customized by the user
const double EXCELDEFAULTROWHEIGHT = 20.0;
var startCell = (ExcelRangeBase)worksheet.Cells["A1"];
for (var i = 0; i < 10; i++)
{
var chart = worksheet.Drawings.AddChart("chart " + i, eChartType.Pie);
chart.SetSize(EXCELCHARTWIDTH, EXCELCHARTHEIGHT);
chart.SetPosition(startCell.Start.Row, 0, startCell.Start.Column, 0);
var chartcellheight = (int)Math.Ceiling(EXCELCHARTHEIGHT / EXCELDEFAULTROWHEIGHT);
startCell = startCell.Offset(chartcellheight, 0);
}
pck.Save();
}
The offset is an offset within one cell.
So if you have a cell 64 x 20 pixels (default) it should not usually exceed 64 or 20 resp. To set the top left corner of a chart just in the middle of a cell, call:
chart.SetPosition(row , 10, col, 32);
also note that if you read the position from From.RowOff, you need to divide it by 9525

Adjusting a merged column's width using Excel Interop

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;

How can I control table column width in Word documents using DocX?

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.

Categories