Npoi_2.1.3-excel set decimal, percent, currency, date, scientific notation, and amount capitalization

Source: Internet
Author: User

In the operation of Excel, some special value conversion is unavoidable, the following gives the conversion method is similar, the code is as follows:

Hssfworkbook Hssfworkbook = new Hssfworkbook () isheet sheet = hssfworkbook.createsheet ("Sheet1");//Set first column (Cell) width sheet . Setcolumnwidth (0, 5000);//Create a formatted instance object IDataFormat format = Hssfworkbook.createdataformat ();//Preserve two-bit decimal format//Create a cell "1.20" Icell cell = sheet. CreateRow (0). Createcell (0);//sets the cell's value to cell. Setcellvalue (1.2); Icellstyle CellStyle = Hssfworkbook.createcellstyle ();//Format Value Cellstyle.dataformat = Hssfdataformat.getbuiltinformat ("0.00"); cell. CellStyle = cellstyle;//Currency "¥20,000" icell cell2 = sheet. CreateRow (1). Createcell (0); cell2. Setcellvalue (20000); Icellstyle cellStyle2 = Hssfworkbook.createcellstyle (); cellstyle2.dataformat = format. GetFormat ("¥#,# #0"); cell2. CellStyle = cellstyle2;//Scientific Counting Method "3.15E+00" icell Cell3 = sheet. CreateRow (2). Createcell (0); Cell3. Setcellvalue (3.151234); Icellstyle cellStyle3 = Hssfworkbook.createcellstyle (); Cellstyle3.dataformat = Hssfdataformat.getbuiltinformat ("0.00E+00"); Cell3. CellStyle = cellstyle3;//percent "99.33%" icell cell4 = sheet. CreateRow (3). Createcell (0); Cell4. SEtcellvalue (0.99333); Icellstyle cellStyle4 = Hssfworkbook.createcellstyle (); Cellstyle4.dataformat = Hssfdataformat.getbuiltinformat ("0.00%"); Cell4. CellStyle = cellstyle4;//Phone number "021-65881234" icell cell5 = sheet. CreateRow (4). Createcell (0); cell5. Setcellvalue (02165881234); Icellstyle cellStyle5 = Hssfworkbook.createcellstyle (); cellstyle5.dataformat = format. GetFormat ("000-00000000"); cell5. CellStyle = cellstyle5;//Amount Capital-one and three icell cell6 = sheet. CreateRow (5). Createcell (0); cell6. Setcellvalue (123); Icellstyle cellStyle6 = Hssfworkbook.createcellstyle (); cellstyle6.dataformat = format. GetFormat ("[dbnum2][$-804]0 Yuan"); cell6. CellStyle = cellstyle6;//date format Icell cell7 = sheet. CreateRow (6). Createcell (0); Cell7. Setcellvalue (New DateTime (2004, 5, 6)), Icellstyle CellStyle7 = Hssfworkbook.createcellstyle (); Cellstyle7.dataformat = Format. GetFormat ("yyyy year M D Day"); Cell7. CellStyle = cellstyle7;//date format Icell cell8 = sheet. CreateRow (7). Createcell (0); Cell8. Setcellvalue (New DateTime (2005, one, 6)); Icellstyle CellStyle8 = Hssfworkbook.createcellstyle (); cellstyle8.dataformat = format. GetFormat ("yyyy year M D Day"); Cell8. CellStyle = CellStyle8; FileStream file = new FileStream (@ "Test.xls", FileMode.Create); hssfworkbook.write (file); file. Close ();

Reprinted from: http://www.cnblogs.com/Murray/archive/2015/01/20/4235601.html

  

Npoi_2.1.3-excel set decimal, percent, currency, date, scientific notation, and amount capitalization

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.