I wrote a strange question about exporting Excel using poi a few days ago. Today I used poi, but this time it is not suitable for using templates.Program. So I encountered some unexpected problems. In fact, there are two problems: Setting the Row Height and setting the column width.
First, I checked the poi3.0 API and found that the hssfrow object has the setheight (Short height) method. Then I wrote a vertex test.Code
Public Static Void Main (string [] ARGs) {
Try {
Hssfworkbook WB = New Hssfworkbook ();
Hssfsheet Sheet = WB. createsheet ();
Hssfrow row = Sheet. createrow ( 0 );
Row. setheight (( Short ) 25 ); // The purpose is to set the Row Height to 25px.
Fileoutputstream fileout = New Fileoutputstream ( " C: \ a.xls " );
WB. Write (fileout );
Fileout. Close ();
}
Catch (Exception E) {
E. printstacktrace ();
}
}
When I opened a.xls, I found that the result was not what I wanted, and the height of the first line was not. If no error was reported, the code was faulty. Why didn't I return the height? Is the unit different? I changed row. setheight (short) 25); To row. setheight (short) 250). The first line is displayed, but how is the conversion relationship? I checked that the first line of the exported Excel is 16 pixels in height, and the row is obtained. setheight (short) 15.625); indicates that the row height is a pixel, so you can set it to several pixels. For example
Row. setheight (short) (15.625 * n); // n indicates the number of rows that are high.
In fact, there is also an hssfrow object in the API and a setheightinpoints (float height) function that sets the row height. The parameter in this function is the number of rows with a higher value, which is much more convenient than the setheight function.
The Row Height setting is complete. Next, set the column width.
Public Static Void Main (string [] ARGs) {
Try {
Hssfworkbook WB = New Hssfworkbook ();
Hssfsheet Sheet = WB. createsheet ();
Hssfrow row = Sheet. createrow ( 0 );
Row. setheight (( Short ) 250 );
Sheet. setcolumnwidth (( Short ) 0 ,( Short ) 250 );
Fileoutputstream fileout = New Fileoutputstream ( " C: \ a.xls " );
WB. Write (fileout );
Fileout. Close ();
}
Catch (Exception E) {
E. printstacktrace ();
}
}
Next let's talk about sheet. setcolumnwidth (short) 0, (short) 250 );
The first parameter indicates the number of columns to be set, and the second parameter table shows the column width. The code above shows that the cell shape in the first column of the First row should be a square, because the width and height are both 250, but the Excel file after opening and exporting shows that the width is not high, it is a rectangle, and the width of the column is only 7 pixels, the Row Height and column width are different. setcolumnwidth (short) 0, (short) (35.7); indicates that the height is a pixel, and the pixel of the column width is also set to sheet. setcolumnwidth (short) 0, (short) (35.7 * n); // n indicates the number of shards with the column height.
However, at present, I have not found any other simpler function than this function. If you have any friends, I hope you can communicate more easily.