NPOI設定單元格數字格式

2021-10-02 04:02:41 字數 3897 閱讀 3250

imports system.collections.generic

imports system.linq

imports system.text

imports system.threading.tasks

imports npoi.hssf.usermodel

imports npoi.ss.formula.eval

imports npoi.ss.formula.functions

imports npoi.ss.usermodel

imports npoi.xssf.usermodel

imports npoi.poifs.filesystem

imports npoi.hpsf

imports system.io

imports npoi.ss.util

imports system.drawing

imports npoi.hssf.util

public class nopidataformat

private shared sub main(args as string())

'說明:設定數字格式

'1.建立excel中的workbook

dim myworkbook as iworkbook = new xssfworkbook()

'2.建立workbook中的sheet

dim mysheet as isheet = myworkbook.createsheet("sheet1")

mysheet.setcolumnwidth(0, 20 * 256)

mysheet.setcolumnwidth(1, 20 * 256)

'3.建立row中的cell並賦值

dim row0 as irow = mysheet.createrow(0)

row0.createcell(0).setcellvalue(2013.143)

row0.createcell(1).setcellvalue("轉化為漢字大寫")

dim row1 as irow = mysheet.createrow(1)

row1.createcell(0).setcellvalue(123152013.143)

row1.createcell(1).setcellvalue("改變小數精度")

dim row2 as irow = mysheet.createrow(2)

row2.createcell(0).setcellvalue(123152013.143)

row2.createcell(1).setcellvalue("分段新增,號")

dim row3 as irow = mysheet.createrow(3)

row3.createcell(0).setcellvalue(123152013.143)

row3.createcell(1).setcellvalue("科學計數法")

dim row4 as irow = mysheet.createrow(4)

row4.createcell(0).setcellvalue(-123152013.143)

row4.createcell(1).setcellvalue("正數與負數的區分(負數紅色)")

dim row5 as irow = mysheet.createrow(5)

row5.createcell(0).setcellvalue(123152013.77)

row5.createcell(1).setcellvalue("整數部分+分數")

dim row6 as irow = mysheet.createrow(6)

row6.createcell(0).setcellvalue(123152013.77)

row6.createcell(1).setcellvalue("分數")

dim row7 as irow = mysheet.createrow(7)

row7.createcell(0).setcellvalue(0.333)

row7.createcell(1).setcellvalue("百分數")

'4.建立cellstyle與dataformat並載入格式樣式

dim dataformat as idataformat = myworkbook.createdataformat()

dim style0 as icellstyle = myworkbook.createcellstyle()

style0.dataformat = dataformat.getformat("[dbnum2][$-804]general")

'轉化為漢字大寫

dim style1 as icellstyle = myworkbook.createcellstyle()

style1.dataformat = dataformat.getformat("0.0")

'改變小數精度【小數點後有幾個0表示精確到小數點後幾位】

dim style2 as icellstyle = myworkbook.createcellstyle()

style2.dataformat = dataformat.getformat("#,##0.0")

'分段新增,號

dim style3 as icellstyle = myworkbook.createcellstyle()

style3.dataformat = dataformat.getformat("0.00e+00")

'科學計數法

dim style4 as icellstyle = myworkbook.createcellstyle()

style4.dataformat = dataformat.getformat("0.00;[red]-0.00")

'正數與負數的區分

dim style5 as icellstyle = myworkbook.createcellstyle()

style5.dataformat = dataformat.getformat("# ??/??")

'整數部分+分數

dim style6 as icellstyle = myworkbook.createcellstyle()

style6.dataformat = dataformat.getformat("??/??")

'分數dim style7 as icellstyle = myworkbook.createcellstyle()

style7.dataformat = dataformat.getformat("0.00%")

'百分數【小數點後有幾個0表示精確到顯示小數點後幾位】

'5.將cellstyle應用於具體單元格

row0.getcell(0).cellstyle = style0

row1.getcell(0).cellstyle = style1

row2.getcell(0).cellstyle = style2

row3.getcell(0).cellstyle = style3

row4.getcell(0).cellstyle = style4

row5.getcell(0).cellstyle = style5

row6.getcell(0).cellstyle = style6

row7.getcell(0).cellstyle = style7

'6.儲存

dim file as new filestream("e:\myworkbook7.xlsx", filemode.create)

myworkbook.write(file)

file.close()

end sub

end class

設定單元格數字格式 數字格式設定介紹

預設情況下,使用者輸入的數值都是一組數字組成的數字串,此數字串未經格式化,無法直觀地告訴使用者數字背後的具體意義。設定數字格式是指更改資料在單元格中外觀顯示。對數字進行格式化可以快速提公升工作表的外觀表現力和易讀性。例如,在圖 5 1 所示的 中,a 列是原始資料,b 列是格式化後的資料,使用者明顯...

npoi獲取合併單元格 NPOI合併單元格

var filepath d 練習 雜項 npoi合併單元格 aaa.xlsx iworkbook workbook null isheet sheet null using filestream fs file.openread filepath 2007版本 if filepath.indexo...

設定單元格數字格式 5 1 數字格式設定介紹

預設情況下,使用者輸入的數值都是一組數字組成的數字串,此數字串未經格式化,無法直觀地告訴使用者數字背後的具體意義。設定數字格式是指更改資料在單元格中外觀顯示。對數字進行格式化可以快速提公升工作表的外觀表現力和易讀性。例如,在圖5 1所示的 中,a列是原始資料,b列是格式化後的資料,使用者明顯看出設定...