If you use poi to process problems that cannot be automatically calculated by formulas in Excel, templates

Source: Internet
Author: User

Import org. Apache. Poi. hssf. usermodel .*;
Import org. Apache. Poi. hssf. util .*;
Import org. Apache. Poi. hssf. usermodel .*;
Import org. Apache. Poi. poifs. filesystem. poifsfilesystem;

Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;

 

/**
* Test the poi Processing Formula.
*
* Problem description: After data is imported through poi, the original formula cells that reference the imported data cannot be displayed. You need to locate the formula unit again.
* The result is obtained after the formula is transferred again.
*
* Solution: Set the formula again for the formula cell.
*
* Related Files: manually set cell b2 = C2 + D2 in the test.xls File
*
*/

 

Private Static void readset (string filename ){
Poifsfilesystem FS;
Try {
FS = new poifsfilesystem (New fileinputstream (filename ));
Hssfworkbook WB = new hssfworkbook (FS );
Hssfsheet sheet = WB. getsheet ("sheet1 ");
Hssfrow ROW = sheet. getrow (short) 1 );
Hssfcell cell = row. getcell (short) 2 );
Cell. setcellvalue (short) 5 );
Cell = row. getcell (short) 3 );
Cell. setcellvalue (short) 40 );
Hssfcell cell1 = row. getcell (short) 1 );
If (hssfcell. cell_type_formula = cell1.getcelltype ()){
// Obtain the formula cell formula and reset it.
Cell1.setcellformula (cell1.getcellformula ());
}
Fileoutputstream fileout = new fileoutputstream (filename );
WB. Write (fileout );
Fileout. Close ();
}
Catch (ioexception e ){
E. printstacktrace ();
}
}

Public static void ylb (string ym ){
System. Out. println ("Poi ");
// Createfile ("C:/test.xls ");
Createsheet ("C:/gisoracle.xls ");
Readset ("C:/test.xls ");
System. Out. println ("================ ");

}

Reference: http://gohands.5d6d.com/redirect.php? Tid = 197 & goto = lastpost

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.