POI设置EXCEL单元格格式为文本.docx
《POI设置EXCEL单元格格式为文本.docx》由会员分享,可在线阅读,更多相关《POI设置EXCEL单元格格式为文本.docx(35页珍藏版)》请在冰豆网上搜索。
POI设置EXCEL单元格格式为文本
实际开发过程中通常用到的就是从数据库导出EXCEL表格了,JXL可以这样做,其实POI也可以(关于JXL与POI的异同可访问我之前总结的文章),之前写过POI对七种文档(当然也包括EXCEL)的内容读取操作的文章,这次要写的就非常重要了,就是开发中经常会用到的POI读取数据库导出EXCEL的操作,所谓导出EXCEL也就是生成带数据内容的新的EXCEL文件
目前的POI版本是3.7
下载地址:
http:
//poi.apache.org/download.html#POI-3.7
必须包只有一个:
poi-3.7-20101029.jar
整理思路:
1)数据库中的字段对应EXCEL的最顶层一行各个CELL名称[也就是上面图片中序号版本...的]
2)将每个数据一次插入到对应名称CELL的对应记录位置
3)为了方便操作,顶层的cell各个名称可以抽取出来成为一个单独类
具体代码
第一部分:
单独的EXCEL表头类
publicclassCachetable{
Java代码
1.// Fields
2.private int recnum;
3.private String devIp;
4.private String srcaddr;
5.private String dstaddr;
6.private String nexthop;
7.private String input;
8.private String output;
9.private String dpkts;
10.private String doctets;
11.private String sstart;
12.
13.private String dstport;
14.private String prot;
15.private String tos;
16.private String srcas;
17.private String dstas;
18.private String pduversion;
19.
20.
21./** default constructor */
22.public Cachetable() {
23.}
24.
25.
26./** full constructor */
27.public Cachetable(int recnum, String devIp, String srcaddr, String dstaddr, String nexthop, String input, String output, String dpkts, String doctets, String sstart, String dstport, String prot, String tos, String srcas, String dstas,String pduversion) {
28. this.recnum = recnum;
29. this.devIp = devIp;
30. this.srcaddr = srcaddr;
31. this.dstaddr = dstaddr;
32. this.nexthop = nexthop;
33. this.input = input;
34. this.output = output;
35. this.dpkts = dpkts;
36. this.doctets = doctets;
37. this.sstart = sstart;
38. this.dstport = dstport;
39. this.prot = prot;
40. this.tos = tos;
41. this.srcas = srcas;
42. this.dstas = dstas;
43. this.pduversion = pduversion;
44.
45.}
46.
47.
48.public int getRecnum() {
49. return this.recnum;
50.}
51.
52.public void setRecnum(int recnum) {
53. this.recnum= recnum;
54.}
55.
56.public String getDevIp() {
57. return this.devIp;
58.}
59.
60.public void setDevIp(String devIp) {
61. this.devIp = devIp;
62.}
63.
64.
65.public String getSrcaddr() {
66. return this.srcaddr;
67.}
68.
69.public void setSrcaddr(String srcaddr) {
70. this.srcaddr = srcaddr;
71.}
72.
73.
74.public String getDstaddr() {
75. return this.dstaddr;
76.}
77.
78.public void setDstaddr(String dstaddr) {
79. this.dstaddr = dstaddr;
80.}
81.
82.
83.public String getNexthop() {
84. return this.nexthop;
85.}
86.
87.public void setNexthop(String nexthop) {
88. this.nexthop = nexthop;
89.}
90.
91.
92.public String getInput() {
93. return this.input;
94.}
95.
96.public void setInput(String input) {
97. this.input = input;
98.}
99.
100.
101.public String getOutput() {
102. return this.output;
103.}
104.
105.public void setOutput(String output) {
106. this.output = output;
107.}
108.
109.public String getDpkts() {
110. return this.dpkts;
111.}
112.
113.public void setDpkts(String dpkts) {
114. this.dpkts = dpkts;
115.}
116.
117.
118.public String getDoctets() {
119. return this.doctets;
120.}
121.
122.public void setDoctets(String doctets) {
123. this.doctets = doctets;
124.}
125.
126.
127.public String getSstart() {
128. return this.sstart;
129.}
130.
131.public void setSstart(String sstart) {
132. this.sstart = sstart;
133.}
134.
135.public String getDstport() {
136. return this.dstport;
137.}
138.
139.public void setDstport(String dstport) {
140. this.dstport = dstport;
141.}
142.
143.public String getProt() {
144. return this.prot;
145.}
146.
147.public void setProt(String prot) {
148. this.prot = prot;
149.}
150.
151.
152.public String getTos() {
153. return this.tos;
154.}
155.
156.public void setTos(String tos) {
157. this.tos = tos;
158.}
159.
160.public String getSrcas() {
161. return this.srcas;
162.}
163.
164.public void setSrcas(String srcas) {
165. this.srcas = srcas;
166.}
167.
168.
169.public String getDstas() {
170. return this.dstas;
171.}
172.
173.public void setDstas(String dstas) {
174. this.dstas = dstas;
175.}
176.
177.public String getPduversion() {
178. return this.pduversion;
179.}
180.
181.public void setPduversion(String pduversion) {
182. this.pduversion = pduversion;
183.}
184.
185.
//Fields
privateintrecnum;
privateStringdevIp;
privateStringsrcaddr;
privateStringdstaddr;
privateStringnexthop;
privateStringinput;
privateStringoutput;
privateStringdpkts;
privateStringdoctets;
privateStringsstart;
privateStringdstport;
privateStringprot;
privateStringtos;
privateStringsrcas;
privateStringdstas;
privateStringpduversion;
/**defaultconstructor*/
publicCachetable(){
}
/**fullconstructor*/
publicCachetable(intrecnum,StringdevIp,Stringsrcaddr,Stringdstaddr,Stringnexthop,Stringinput,Stringoutput,Stringdpkts,Stringdoctets,Stringsstart,Stringdstport,Stringprot,Stringtos,Stringsrcas,Stringdstas,Stringpduversion){
this.recnum=recnum;
this.devIp=devIp;
this.srcaddr=srcaddr;
this.dstaddr=dstaddr;
this.nexthop=nexthop;
this.input=input;
this.output=output;
this.dpkts=dpkts;
this.doctets=doctets;
this.sstart=sstart;
this.dstport=dstport;
this.prot=prot;
this.tos=tos;
this.srcas=srcas;
this.dstas=dstas;
this.pduversion=pduversion;
}
publicintgetRecnum(){
returnthis.recnum;
}
publicvoidsetRecnum(intrecnum){
this.recnum=recnum;
}
publicStringgetDevIp(){
returnthis.devIp;
}
publicvoidsetDevIp(StringdevIp){
this.devIp=devIp;
}
publicStringgetSrcaddr(){
returnthis.srcaddr;
}
publicvoidsetSrcaddr(Stringsrcaddr){
this.srcaddr=srcaddr;
}
publicStringgetDstaddr(){
returnthis.dstaddr;
}
publicvoidsetDstaddr(Stringdstaddr){
this.dstaddr=dstaddr;
}
publicStringgetNexthop(){
returnthis.nexthop;
}
publicvoidsetNexthop(Stringnexthop){
this.nexthop=nexthop;
}
publicStringgetInput(){
returnthis.input;
}
publicvoidsetInput(Stringinput){
this.input=input;
}
publicStringgetOutput(){
returnthis.output;
}
publicvoidsetOutput(Stringoutput){
this.output=output;
}
publicStringgetDpkts(){
returnthis.dpkts;
}
publicvoidsetDpkts(Stringdpkts){
this.dpkts=dpkts;
}
publicStringgetDoctets(){
returnthis.doctets;
}
publicvoidsetDoctets(Stringdoctets){
this.doctets=doctets;
}
publicStringgetSstart(){
returnthis.sstart;
}
publicvoidsetSstart(Stringsstart){
this.sstart=sstart;
}
publicStringgetDstport(){
returnthis.dstport;
}
publicvoidsetDstport(Stringdstport){
this.dstport=dstport;
}
publicStringgetProt(){
returnthis.prot;
}
publicvoidsetProt(Stringprot){
this.prot=prot;
}
publicStringgetTos(){
returnthis.tos;
}
publicvoidsetTos(Stringtos){
this.tos=tos;
}
publicStringgetSrcas(){
returnthis.srcas;
}
publicvoidsetSrcas(Stringsrcas){
this.srcas=srcas;
}
publicStringgetDstas(){
returnthis.dstas;
}
publicvoidsetDstas(Stringdstas){
this.dstas=dstas;
}
publicStringgetPduversion(){
returnthis.pduversion;
}
publicvoidsetPduversion(Stringpduversion){
this.pduversion=pduversion;
}
}
第二部分:
具体的POI操作生成EXCEL类
【我这里只是个示例,没连数据库,直接运行即可,如果想连,稍微变动一点即可】
Java代码
1.package com.zkyy.flow.excel;
2.
3.import java.io.FileOutputStream;
4.import java.io.IOException;
5.import java.io.OutputStream;
6.import java.sql.SQLException;
7.import java.util.ArrayList;
8.import java.util.List;
9.
10.import javax.swing.JOptionPane;
11.
12.import org.apache.poi.hssf.usermodel.HSSFCell;
13.import org.apache.poi.hssf.usermodel.HSSFCellStyle;
14.import org.apache.poi.hssf.usermodel.HSSFDataFormat;
15.import org.apache.poi.hssf.usermodel.HSSFFooter;
16.import org.apache.poi.hssf.usermodel.HSSFHeader;
17.import org.apache.poi.hssf.usermodel.HSSFRow;
18.import org.apache.poi.hssf.usermodel.HSSFSheet;
19.import org.apache.poi.hssf.usermodel.HSSFWorkbook;
20.
21.import com.kk.flow.webapp.util.Cachetable;
22.
23.public class ExcelOut {
24.
25. //表头
26. public static final String[] tableHeader = {"序号","版本","接收时刻","设备","入接口","出接口",
27. "源IP","目的IP","下一跳","协议","端口","对端端口","TOS","源AS","目的AS","TCP_FLAG","pad1","pad2"};
28. //创建工作本 TOS
29. public static HSSFWorkbook demoWorkBook = new HSSFWorkbook();
30. //创建表
31. public static HSSFSheet demoSheet = demoWorkBook.createSheet("The World's 500 Enterprises");
32. //表头的单元格个数目
33. public static final short cellNumber = (short)tableHeader.length;
34. //数据库表的列数
35. public static final int columNumber = 1;
36. /**
37. * 创建表头
38. * @return
39. */
40. public static void creat