打印设置主要包括方向设置、缩放、纸张设置、页边距等。NPOI 1.2支持大部分打印属性,能够让你轻松满足客户的打印需要。
一、方向设置
Excel支持两种页面方向,即纵向和横向。
在NPOI中如何设置呢?你可以通过HSSFSheet.PrintSetup.Landscape来设置,Landscape是布尔类型的,在英语中是横向的意思。如果Landscape等于true,则表示页面方向为横向;否则为纵向。
二、缩放和页面宽高
这里的缩放比例对应于HSSFSheet.PrintSetup.Scale属性,而页宽和页高分别对应于HSSFSheet.PrintSetup.FitWidth和HSSFSheet.PrintSetup.FitHeight。要注意的是,这里的PrintSetup.Scale应该被设置为0-100之间的值,而不是小数。
三、纸张设置
由HSSFSheet.PrintSetup.PaperSize属性来设置,但这里的PaperSize并不是随便设置的,而是由一些固定的值决定的,具体的值与对应的纸张如下表所示:
值 | 纸张 |
1 | US Letter 8 1/2 x 11 in |
2 | US Letter Small 8 1/2 x 11 in |
3 | US Tabloid 11 x 17 in |
4 | US Ledger 17 x 11 in |
5 | US Legal 8 1/2 x 14 in |
6 | US Statement 5 1/2 x 8 1/2 in |
7 | US Executive 7 1/4 x 10 1/2 in |
8 | A3 297 x 420 mm |
9 | A4 210 x 297 mm |
10 | A4 Small 210 x 297 mm |
11 | A5 148 x 210 mm |
12 | B4 (JIS) 250 x 354 |
13 | B5 (JIS) 182 x 257 mm |
14 | Folio 8 1/2 x 13 in |
15 | Quarto 215 x 275 mm |
16 | 10 x 14 in |
17 | 11 x 17 in |
18 | US Note 8 1/2 x 11 in |
19 | US Envelope #9 3 7/8 x 8 7/8 |
20 | US Envelope #10 4 1/8 x 9 1/2 |
21 | US Envelope #11 4 1/2 x 10 3/8 |
22 | US Envelope #12 4 \276 x 11 |
23 | US Envelope #14 5 x 11 1/2 |
24 | C size sheet |
25 | D size sheet |
26 | E size sheet |
27 | Envelope DL 110 x 220mm |
28 | Envelope C5 162 x 229 mm |
29 | Envelope C3 324 x 458 mm |
30 | Envelope C4 229 x 324 mm |
31 | Envelope C6 114 x 162 mm |
32 | Envelope C65 114 x 229 mm |
33 | Envelope B4 250 x 353 mm |
34 | Envelope B5 176 x 250 mm |
35 | Envelope B6 176 x 125 mm |
36 | Envelope 110 x 230 mm |
37 | US Envelope Monarch 3.875 x 7.5 in |
38 | 6 3/4 US Envelope 3 5/8 x 6 1/2 in |
39 | US Std Fanfold 14 7/8 x 11 in |
40 | German Std Fanfold 8 1/2 x 12 in |
41 | German Legal Fanfold 8 1/2 x 13 in |
42 | B4 (ISO) 250 x 353 mm |
43 | Japanese Postcard 100 x 148 mm |
44 | 9 x 11 in |
45 | 10 x 11 in |
46 | 15 x 11 in |
47 | Envelope Invite 220 x 220 mm |
48 | RESERVED–DO NOT USE |
49 | RESERVED–DO NOT USE |
50 | US Letter Extra 9 \275 x 12 in |
51 | US Legal Extra 9 \275 x 15 in |
52 | US Tabloid Extra 11.69 x 18 in |
53 | A4 Extra 9.27 x 12.69 in |
54 | Letter Transverse 8 \275 x 11 in |
55 | A4 Transverse 210 x 297 mm |
56 | Letter Extra Transverse 9\275 x 12 in |
57 | SuperA/SuperA/A4 227 x 356 mm |
58 | SuperB/SuperB/A3 305 x 487 mm |
59 | US Letter Plus 8.5 x 12.69 in |
60 | A4 Plus 210 x 330 mm |
61 | A5 Transverse 148 x 210 mm |
62 | B5 (JIS) Transverse 182 x 257 mm |
63 | A3 Extra 322 x 445 mm |
64 | A5 Extra 174 x 235 mm |
65 | B5 (ISO) Extra 201 x 276 mm |
66 | A2 420 x 594 mm |
67 | A3 Transverse 297 x 420 mm |
68 | A3 Extra Transverse 322 x 445 mm |
69 | Japanese Double Postcard 200 x 148 mm |
70 | A6 105 x 148 mm |
71 | Japanese Envelope Kaku #2 |
72 | Japanese Envelope Kaku #3 |
73 | Japanese Envelope Chou #3 |
74 | Japanese Envelope Chou #4 |
75 | Letter Rotated 11 x 8 1/2 11 in |
76 | A3 Rotated 420 x 297 mm |
77 | A4 Rotated 297 x 210 mm |
78 | A5 Rotated 210 x 148 mm |
79 | B4 (JIS) Rotated 364 x 257 mm |
80 | B5 (JIS) Rotated 257 x 182 mm |
81 | Japanese Postcard Rotated 148 x 100 mm |
82 | Double Japanese Postcard Rotated 148 x 200 mm |
83 | A6 Rotated 148 x 105 mm |
84 | Japanese Envelope Kaku #2 Rotated |
85 | Japanese Envelope Kaku #3 Rotated |
86 | Japanese Envelope Chou #3 Rotated |
87 | Japanese Envelope Chou #4 Rotated |
88 | B6 (JIS) 128 x 182 mm |
89 | B6 (JIS) Rotated 182 x 128 mm |
90 | 12 x 11 in |
91 | Japanese Envelope You #4 |
92 | Japanese Envelope You #4 Rotated |
93 | PRC 16K 146 x 215 mm |
94 | PRC 32K 97 x 151 mm |
95 | PRC 32K(Big) 97 x 151 mm |
96 | PRC Envelope #1 102 x 165 mm |
97 | PRC Envelope #2 102 x 176 mm |
98 | PRC Envelope #3 125 x 176 mm |
99 | PRC Envelope #4 110 x 208 mm |
100 | PRC Envelope #5 110 x 220 mm |
101 | PRC Envelope #6 120 x 230 mm |
102 | PRC Envelope #7 160 x 230 mm |
103 | PRC Envelope #8 120 x 309 mm |
104 | PRC Envelope #9 229 x 324 mm |
105 | PRC Envelope #10 324 x 458 mm |
106 | PRC 16K Rotated |
107 | PRC 32K Rotated |
108 | PRC 32K(Big) Rotated |
109 | PRC Envelope #1 Rotated 165 x 102 mm |
110 | PRC Envelope #2 Rotated 176 x 102 mm |
111 | PRC Envelope #3 Rotated 176 x 125 mm |
112 | PRC Envelope #4 Rotated 208 x 110 mm |
113 | PRC Envelope #5 Rotated 220 x 110 mm |
114 | PRC Envelope #6 Rotated 230 x 120 mm |
115 | PRC Envelope #7 Rotated 230 x 160 mm |
116 | PRC Envelope #8 Rotated 309 x 120 mm |
117 | PRC Envelope #9 Rotated 324 x 229 mm |
118 | PRC Envelope #10 Rotated 458 x 324 mm |
(此表摘自《Excel Binary File Format (.xls) Structure Specification.pdf》)
NPOI 1.2.4中的纸张常量由PaperSize表示,如PaperSize.A4, 但都是非常常用的纸张大小,如果满足不了你的需要,可以根据上表自己给PaperSize属性赋值。所以,如果你要设置纸张大小可以用这样的代码:
HSSFSheet.PrintSetup.PaperSize=(short)PaperSize.A5;
或
HSSFSheet.PrintSetup.PaperSize=9; (A4 210*297mm)
四、打印起始页码
它对应于HSSFSheet.PrintSetup.PageStart和HSSFSheet.PrintSetup.UsePage,如果UsePage=false,那么就相当于“自动”,这时PageStart不起作用;如果UsePage=true,PageStart才会起作用。所以在设置PageStart之前,必须先把UsePage设置为true。
五、网格线
由HSSFSheet.IsPrintGridlines设置。但要注意,这里不是HSSFSheet.PrintSetup下面,所以别搞混了。这里之所以不隶属于PrintSetup是由底层存储该信息的record决定的,底层是把IsGridsPrinted放在GridsetRecord里面的,而不是PrintSetupRecord里面的,尽管界面上是放在一起的。
六、单色打印
它对应于HSSFSheet.PrintSetup.NoColors属性,布尔类型,值为true时,表示单色打印。
七、草稿品质
它对应于HSSFSheet.PrintSetup.IsDraft属性,布尔类型,值为true时,表示用草稿品质打印。
八、打印顺序
由HSSFSheet.PrintSetup.LeftToRight属性控制,布尔类型,当为true时,则表示“先行后列”;如果是false,则表示“先列后行”。
九、“错误单元格打印为”设置
由HSSFSheet.PrintSetup.CellError属性控制,DisplayCellErrorType枚举类型,下面是的定义
1 2 3 4 5 6 7 |
|
这里的ErrorAsNA就是截图中的#N/A的意思。
十、批注
由HSSFSheet.PrintSetup.Notes和HSSFSheet.PrintSetup.EndNote设置,HSSFSheet.PrintSetup.Notes是否设置需不需要打印批注,布尔类型;而HSSFSheet.PrintSetup.EndNote负责设置是否显示在工作表末尾,否则就是“如同工作表中的显示”。
有关打印的范例请参考NPOI.Examples中的 SetPrintSettingsInXls 项目。
区域打印:WorkBook.SetPrintArea(sheet页index, 开始列号, 结束列号, 开始行号, 结束行号)
打印标题:
顶端标题行:Sheet.RepeatingRows = New CellRangeAddress(开始行号, 结束行号, 开始列号, 结束列号)
左端标题行:Sheet.RepeatingColumns = New CellRangeAddress(开始行号, 结束行号, 开始列号, 结束列号)