2.4.4 use npoi to Operate Excel -- draw a grid

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.