This article is a mirror article of machine translation, please click here to jump to the original article.

View: 22079|Reply: 0

[Source] NPOI sets the width and height of the cells

[Copy link]
Posted on 2/26/2017 3:31:32 PM | | | |


In Excel, the width of a cell is actually the width of a column, because Excel assumes that the width of the cells in this column must be the same. So to set the width of a cell, we have to start with the width of the column, HSSFSheet has a method called SetColumnWidth, there are two parameters: one is the index of the column (starting from 0), and the other is the width.
Now suppose you want to set the width of column B, you can use the following code:




Here you will find an interesting phenomenon, the second parameter of SetColumnWidth is multiplied by 256, what is going on? In fact, the unit of this parameter is 1/256 character width, that is, the width of column B is set to 100 characters.
What we just said is how to set, so how to read the width of a column? Using the GetColumnWidth method directly, this method has only one parameter, which is the index number of the column. It looks like this:



After talking about width, let's talk about height, in Excel, the height of each line is also required to be the same, so setting the height of the cell, in fact, is to set the height of the row, so the relevant attribute should also be on HSSFRow, it is HSSFRow.Height and HeightInPoints, the difference between these two attributes is that the unit of HeightInPoints is points, and the unit of Height is 1/20 of a point, So the value of Height is always 20 times the HeightInPoints.
To set the height of the first line, you can use the following code:



or


If you want to get the row height of a certain line, you can directly take the return value of the HSSFRow.Height property.

If you may find it troublesome to set the row height one row at a time or the column width for a column, then you can consider using the HSSFSheet.DefaultColumnWidth, HSSFSheet.DefaultRowHeight, and HSSFSheet.DefaultRowHeightInPoints properties.
Once these properties are set, if a row or column does not have a width set, the default width or height is used. The code is as follows:







Previous:.net/c# to get the parameter value of the URL
Next:How is Session.Abandon different from Session.Clear?
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com