In npoi, there is no grid method. However, we know that a grid is actually composed of a horizontal line and a vertical line. We can simulate a grid by drawing a line.
Hssfsheet sheet1 = Hssfworkbook. createsheet ( " Sheet1 " );
Hssfrow row=Sheet1.createrow (2);
Row. createcell (1);
Row. heightinpoints= 240;
Sheet1.setcolumnwidth (2,9000);
IntLinescount= 20;
Hssfpatriarch patriarch= Sheet1.createdrawingpatriarch ();
// Because dx in hssfclientanchor can only be between 0 and, and Dy can only be between 0 and, the proportional method is used here.
Double Xratio = 1023.0 / (Linescount * 10 );
Double Yratio = 255.0 / (Linescount * 10 );
// Draw a vertical line
Int X1 = 0 ;
Int Y1 = 0 ;
Int X2 = 0 ;
Int Y2 = 200 ;
For ( Int I = 0 ; I < Linescount; I ++ )
{
Hssfclientanchor A2 = New Hssfclientanchor ();
A2.setanchor (( Short ) 2 , 2 ,( Int ) (X1 * Xratio ),( Int ) (Y1 * Yratio ),
( Short ) 2 , 2 ,( Int ) (X2 * Xratio ),( Int ) (Y2 * Yratio ));
Hssfsimpleshape shape2 = Patriarch. createsimpleshape (A2 );
Shape2.shapetype = (Hssfsimpleshape. object_type_line );
X1+ = 10;
X2+ = 10;
}
// Draw a horizontal line
X1 = 0 ;
Y1 = 0 ;
X2 = 200 ;
Y2 = 0 ;
For ( Int I = 0 ; I < Linescount; I ++ )
{
Hssfclientanchor A2 = New Hssfclientanchor ();
A2.setanchor (( Short ) 2 , 2 ,( Int ) (X1 * Xratio ),( Int ) (Y1 * Yratio ),
( Short ) 2 , 2 ,( Int ) (X2 * Xratio ),( Int ) (Y2 * Yratio ));
Hssfsimpleshape shape2 = Patriarch. createsimpleshape (A2 );
Shape2.shapetype = (Hssfsimpleshape. object_type_line );
Y1+ = 10;
Y2+ = 10;
}
Note that dx in the hssfclientanchor object can only take the number between 0 and, and Dy can only take the number between 0 and. We can understand that the cell width and height are divided into 1023 and 255 parts. When Dx and Dy are set, the corresponding coordinates are obtained proportionally. The final Excel file is as follows:
Returned directory