日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

POI 单元格格式

發(fā)布時(shí)間:2024/4/17 编程问答 64 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POI 单元格格式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?http://snk.blogbus.com/logs/4351059.html

poi 輸出 excel 時(shí) ,單引號(hào)被顯示出來(lái)的問(wèn)題,還不知道如何解決,搜索到最相近的記錄如下。
把cell改為text屬性:CellStyle.setDataFormart(HSSFDataFormat.getBuiltinFormat("text"));
HSSFDataFormat 的格式有:

public class HSSFDataFormatextends java.lang.Object

Utility to identify builtin formats. Now can handle user defined data formats also. The following is a list of the formats as returned by this class.

?

0, "General"
1, "0"
2, "0.00"
3, "#,##0"
4, "#,##0.00"
5, "($#,##0_);($#,##0)"
6, "($#,##0_);[Red]($#,##0)"
7, "($#,##0.00);($#,##0.00)"
8, "($#,##0.00_);[Red]($#,##0.00)"
9, "0%"
0xa, "0.00%"
0xb, "0.00E+00"
0xc, "# ?/?"
0xd, "# ??/??"
0xe, "m/d/yy"
0xf, "d-mmm-yy"
0x10, "d-mmm"
0x11, "mmm-yy"
0x12, "h:mm AM/PM"
0x13, "h:mm:ss AM/PM"
0x14, "h:mm"
0x15, "h:mm:ss"
0x16, "m/d/yy h:mm"

// 0x17 - 0x24 reserved for international and undocumented 0x25, "(#,##0_);(#,##0)"

0x26, "(#,##0_);[Red](#,##0)"

0x27, "(#,##0.00_);(#,##0.00)"

0x28, "(#,##0.00_);[Red](#,##0.00)"

0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"

0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"

0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"

0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"

0x2d, "mm:ss"

0x2e, "[h]:mm:ss"

0x2f, "mm:ss.0"

0x30, "##0.0E+0"

0x31, "@" - This is text format.

0x31 "text" - Alias for "@"

?

?

=====================

http://javacrazyer.iteye.com/blog/894850

再讀本篇文章之前,請(qǐng)先看我的前一篇文章,前一篇文章中有重點(diǎn)講到POI設(shè)置EXCEL單元格格式為文本格式,剩下的設(shè)置小數(shù)、百分比、貨幣、日期、科學(xué)計(jì)數(shù)法和中文大寫這些將在下面一一寫出

以下將要介紹的每一種都會(huì)用到這三行中的變量

?

?? ? ? ? ? ?HSSFWorkbook demoWorkBook = new HSSFWorkbook(); ??

?? ? ? ? ? ?HSSFSheet demoSheet = demoWorkBook.createSheet("The World's 500 Enterprises"); ??

?? ? ? ? ? ?HSSFCell cell = demoSheet.createRow(0).createCell(0);

?

第一種:日期格式

?

?? ? ? ? ? ?cell.setCellValue(new Date(2008,5,5));

?? ? ? ? ? ?//set date format

?? ? ? ? ? ?HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

?? ? ? ? ? ?HSSFDataFormat format= demoWorkBook.createDataFormat();

?? ? ? ? ? ?cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));

?? ? ? ? ? ?cell.setCellStyle(cellStyle);

?

第二種:保留兩位小數(shù)格式

?? ? ? ? ???cell.setCellValue(1.2);

?? ? ? ? ? ?HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

?? ? ? ? ? ?cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));

?? ? ? ? ? ?cell.setCellStyle(cellStyle);

?

這里與上面有所不同,用的是HSSFDataFormat.getBuiltinFormat()方法,之所以用這個(gè),是因?yàn)?.00是Excel內(nèi)嵌的格式,完整的Excel內(nèi)嵌格式列表大家可以看這個(gè)窗口中的自定義列表:



?這里就不一一列出了

?

第三種:貨幣格式

?

?? ? ? ? ? ?cell.setCellValue(20000);

?? ? ? ? ? ?HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

?? ? ? ? ? ?HSSFDataFormat format= demoWorkBook.createDataFormat();

?? ? ? ? ? ?cellStyle.setDataFormat(format.getFormat("¥#,##0"));

?? ? ? ? ? ?cell.setCellStyle(cellStyle);

?

第四種:百分比格式

?

?? ? ? ? ? ?cell.setCellValue(20);

?? ? ? ? ? ?HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

?? ? ? ? ? ?cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));

?? ? ? ? ? ?cell.setCellStyle(cellStyle);

??此種情況跟第二種一樣

?

第五種:中文大寫格式

?

?? ? ? ? ? ?cell.setCellValue(20000);

?? ? ? ? ? ?HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

?? ? ? ? ? ?HSSFDataFormat format= demoWorkBook.createDataFormat();

?? ? ? ? ? ?cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));

?? ? ? ? ? ?cell.setCellStyle(cellStyle);

?

第六種:科學(xué)計(jì)數(shù)法格式

?

?? ? ? ? ? ?cell.setCellValue(20000);

?? ? ? ? ? ?HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

?? ? ? ? ? ?cellStyle.setDataFormat( HSSFDataFormat.getBuiltinFormat("0.00E+00"));

?? ? ? ? ? ?cell.setCellStyle(cellStyle);

此種情況也與第二種情況一樣

==============

http://javacrazyer.iteye.com/blog/894758

實(shí)際開發(fā)過(guò)程中通常用到的就是從數(shù)據(jù)庫(kù)導(dǎo)出EXCEL表格了,JXL可以這樣做,其實(shí)POI也可以(關(guān)于JXL與POI的異同可訪問(wèn)我之前總結(jié)的文章),之前寫過(guò)POI對(duì)七種文檔(當(dāng)然也包括EXCEL)的內(nèi)容讀取操作的文章,這次要寫的就非常重要了,就是開發(fā)中經(jīng)常會(huì)用到的POI讀取數(shù)據(jù)庫(kù)導(dǎo)出EXCEL的操作,所謂導(dǎo)出EXCEL也就是生成帶數(shù)據(jù)內(nèi)容的新的EXCEL文件

目前的POI版本是3.7

下載地址:http://poi.apache.org/download.html#POI-3.7

?必須包只有一個(gè):poi-3.7-20101029.jar


?整理思路:1)數(shù)據(jù)庫(kù)中的字段對(duì)應(yīng)EXCEL的最頂層一行各個(gè)CELL名稱[也就是上面圖片中序號(hào)版本...的]

?? ? ? ? ? ? ? ?2)將每個(gè)數(shù)據(jù)一次插入到對(duì)應(yīng)名稱CELL的對(duì)應(yīng)記錄位置

?? ? ? ? ? ? ? ?3)為了方便操作,頂層的cell各個(gè)名稱可以抽取出來(lái)成為一個(gè)單獨(dú)類

具體代碼

?? 第一部分:單獨(dú)的EXCEL表頭類

?public class Cachetable {

Java代碼 ?
  • //?Fields ??
  • private?int?recnum; ??
  • private?String?devIp; ??
  • private?String?srcaddr; ??
  • private?String?dstaddr; ??
  • private?String?nexthop; ??
  • private?String?input; ??
  • private?String?output; ??
  • private?String?dpkts; ??
  • private?String?doctets; ??
  • private?String?sstart; ??
  • ??
  • private?String?dstport; ??
  • private?String?prot; ??
  • private?String?tos; ??
  • private?String?srcas; ??
  • private?String?dstas; ??
  • private?String?pduversion; ??
  • ??
  • ??
  • /**?default?constructor?*/??
  • public?Cachetable()?{ ??
  • } ??
  • ??
  • ??
  • /**?full?constructor?*/??
  • 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)?{ ??
  • ????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; ??
  • ??
  • } ??
  • ??
  • ??
  • public?int?getRecnum()?{ ??
  • ????return?this.recnum; ??
  • } ??
  • ??
  • public?void?setRecnum(int?recnum)?{ ??
  • ????this.recnum=?recnum; ??
  • } ??
  • ??
  • public?String?getDevIp()?{ ??
  • ????return?this.devIp; ??
  • } ??
  • ??
  • public?void?setDevIp(String?devIp)?{ ??
  • ????this.devIp?=?devIp; ??
  • } ??
  • ??
  • ??
  • public?String?getSrcaddr()?{ ??
  • ????return?this.srcaddr; ??
  • } ??
  • ??
  • public?void?setSrcaddr(String?srcaddr)?{ ??
  • ????this.srcaddr?=?srcaddr; ??
  • } ??
  • ??
  • ??
  • public?String?getDstaddr()?{ ??
  • ????return?this.dstaddr; ??
  • } ??
  • ??
  • public?void?setDstaddr(String?dstaddr)?{ ??
  • ????this.dstaddr?=?dstaddr; ??
  • } ??
  • ??
  • ??
  • public?String?getNexthop()?{ ??
  • ????return?this.nexthop; ??
  • } ??
  • ??
  • public?void?setNexthop(String?nexthop)?{ ??
  • ????this.nexthop?=?nexthop; ??
  • } ??
  • ??
  • ??
  • public?String?getInput()?{ ??
  • ????return?this.input; ??
  • } ??
  • ??
  • public?void?setInput(String?input)?{ ??
  • ????this.input?=?input; ??
  • } ??
  • ??
  • ??
  • public?String?getOutput()?{ ??
  • ????return?this.output; ??
  • } ??
  • ??
  • public?void?setOutput(String?output)?{ ??
  • ????this.output?=?output; ??
  • } ??
  • ??
  • public?String?getDpkts()?{ ??
  • ????return?this.dpkts; ??
  • } ??
  • ??
  • public?void?setDpkts(String?dpkts)?{ ??
  • ????this.dpkts?=?dpkts; ??
  • } ??
  • ??
  • ??
  • public?String?getDoctets()?{ ??
  • ????return?this.doctets; ??
  • } ??
  • ??
  • public?void?setDoctets(String?doctets)?{ ??
  • ????this.doctets?=?doctets; ??
  • } ??
  • ??
  • ??
  • public?String?getSstart()?{ ??
  • ????return?this.sstart; ??
  • } ??
  • ??
  • public?void?setSstart(String?sstart)?{ ??
  • ????this.sstart?=?sstart; ??
  • } ??
  • ??
  • public?String?getDstport()?{ ??
  • ????return?this.dstport; ??
  • } ??
  • ??
  • public?void?setDstport(String?dstport)?{ ??
  • ????this.dstport?=?dstport; ??
  • } ??
  • ??
  • public?String?getProt()?{ ??
  • ????return?this.prot; ??
  • } ??
  • ??
  • public?void?setProt(String?prot)?{ ??
  • ????this.prot?=?prot; ??
  • } ??
  • ??
  • ??
  • public?String?getTos()?{ ??
  • ????return?this.tos; ??
  • } ??
  • ??
  • public?void?setTos(String?tos)?{ ??
  • ????this.tos?=?tos; ??
  • } ??
  • ??
  • public?String?getSrcas()?{ ??
  • ????return?this.srcas; ??
  • } ??
  • ??
  • public?void?setSrcas(String?srcas)?{ ??
  • ????this.srcas?=?srcas; ??
  • } ??
  • ??
  • ??
  • public?String?getDstas()?{ ??
  • ????return?this.dstas; ??
  • } ??
  • ??
  • public?void?setDstas(String?dstas)?{ ??
  • ????this.dstas?=?dstas; ??
  • } ??
  • ??
  • public?String?getPduversion()?{ ??
  • ????return?this.pduversion; ??
  • } ??
  • ??
  • public?void?setPduversion(String?pduversion)?{ ??
  • ????this.pduversion?=?pduversion; ??
  • } ??
  • ??
  • ??
  • // Fieldsprivate int recnum;private String devIp;private String srcaddr;private String dstaddr;private String nexthop;private String input;private String output;private String dpkts;private String doctets;private String sstart;private String dstport;private String prot;private String tos;private String srcas;private String dstas;private String pduversion;/** default constructor */public Cachetable() {}/** full constructor */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) {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;}public int getRecnum() {return this.recnum;}public void setRecnum(int recnum) {this.recnum= recnum;}public String getDevIp() {return this.devIp;}public void setDevIp(String devIp) {this.devIp = devIp;}public String getSrcaddr() {return this.srcaddr;}public void setSrcaddr(String srcaddr) {this.srcaddr = srcaddr;}public String getDstaddr() {return this.dstaddr;}public void setDstaddr(String dstaddr) {this.dstaddr = dstaddr;}public String getNexthop() {return this.nexthop;}public void setNexthop(String nexthop) {this.nexthop = nexthop;}public String getInput() {return this.input;}public void setInput(String input) {this.input = input;}public String getOutput() {return this.output;}public void setOutput(String output) {this.output = output;}public String getDpkts() {return this.dpkts;}public void setDpkts(String dpkts) {this.dpkts = dpkts;}public String getDoctets() {return this.doctets;}public void setDoctets(String doctets) {this.doctets = doctets;}public String getSstart() {return this.sstart;}public void setSstart(String sstart) {this.sstart = sstart;}public String getDstport() {return this.dstport;}public void setDstport(String dstport) {this.dstport = dstport;}public String getProt() {return this.prot;}public void setProt(String prot) {this.prot = prot;}public String getTos() {return this.tos;}public void setTos(String tos) {this.tos = tos;}public String getSrcas() {return this.srcas;}public void setSrcas(String srcas) {this.srcas = srcas;}public String getDstas() {return this.dstas;}public void setDstas(String dstas) {this.dstas = dstas;}public String getPduversion() {return this.pduversion;}public void setPduversion(String pduversion) {this.pduversion = pduversion;}}

    ?

    第二部分:具體的POI操作生成EXCEL類

    【我這里只是個(gè)示例,沒連數(shù)據(jù)庫(kù),直接運(yùn)行即可,如果想連,稍微變動(dòng)一點(diǎn)即可】

    ?

    Java代碼 ?
  • package?com.zkyy.flow.excel; ??
  • ??
  • import?java.io.FileOutputStream; ??
  • import?java.io.IOException; ??
  • import?java.io.OutputStream; ??
  • import?java.sql.SQLException; ??
  • import?java.util.ArrayList; ??
  • import?java.util.List; ??
  • ??
  • import?javax.swing.JOptionPane; ??
  • ??
  • import?org.apache.poi.hssf.usermodel.HSSFCell; ??
  • import?org.apache.poi.hssf.usermodel.HSSFCellStyle; ??
  • import?org.apache.poi.hssf.usermodel.HSSFDataFormat; ??
  • import?org.apache.poi.hssf.usermodel.HSSFFooter; ??
  • import?org.apache.poi.hssf.usermodel.HSSFHeader; ??
  • import?org.apache.poi.hssf.usermodel.HSSFRow; ??
  • import?org.apache.poi.hssf.usermodel.HSSFSheet; ??
  • import?org.apache.poi.hssf.usermodel.HSSFWorkbook; ??
  • ??
  • import?com.kk.flow.webapp.util.Cachetable; ??
  • ?? ??
  • public?class?ExcelOut?{??? ??
  • ?? ??
  • ????//表頭??? ??
  • ????public?static?final?String[]?tableHeader?=?{"序號(hào)","版本","接收時(shí)刻","設(shè)備","入接口","出接口",??? ??
  • ????????"源IP","目的IP","下一跳","協(xié)議","端口","對(duì)端端口","TOS","源AS","目的AS","TCP_FLAG","pad1","pad2"};??? ??
  • ????//創(chuàng)建工作本???TOS ??
  • ????public?static?HSSFWorkbook?demoWorkBook?=?new?HSSFWorkbook();??? ??
  • ????//創(chuàng)建表??? ??
  • ????public?static?HSSFSheet?demoSheet?=?demoWorkBook.createSheet("The?World's?500?Enterprises");??? ??
  • ????//表頭的單元格個(gè)數(shù)目??? ??
  • ????public?static?final?short?cellNumber?=?(short)tableHeader.length;??? ??
  • ????//數(shù)據(jù)庫(kù)表的列數(shù)??? ??
  • ????public?static?final?int?columNumber?=?1;??? ??
  • ????/**?? ?
  • ?????*?創(chuàng)建表頭?? ?
  • ?????*?@return?? ?
  • ?????*/?? ??
  • ????public?static?void?createTableHeader()??? ??
  • ????{??? ??
  • ????????HSSFHeader?header?=?demoSheet.getHeader();??? ??
  • ????????header.setCenter("世界五百?gòu)?qiáng)企業(yè)名次表");??? ??
  • ????????HSSFRow?headerRow?=?demoSheet.createRow((short)?0);??? ??
  • ????????for(int?i?=?0;i?<?cellNumber;i++)??? ??
  • ????????{??? ??
  • ????????????HSSFCell?headerCell?=?headerRow.createCell((short)?i);?? ??
  • ????????????headerCell.setCellType(HSSFCell.CELL_TYPE_STRING); ??
  • ????????????headerCell.setCellValue(tableHeader[i]);??? ??
  • ????????}??? ??
  • ????}??? ??
  • ????/**?? ?
  • ?????*?創(chuàng)建行?? ?
  • ?????*?@param?cells?? ?
  • ?????*?@param?rowIndex?? ?
  • ?????*/?? ??
  • ????public?static?void?createTableRow(List<String>?cells,short?rowIndex)??? ??
  • ????{??? ??
  • ????????//創(chuàng)建第rowIndex行??? ??
  • ????????HSSFRow?row?=?demoSheet.createRow((short)?rowIndex);??? ??
  • ????????for(int?i?=?0;i?<?cells.size();i++)??? ??
  • ????????{??? ??
  • ????????????//創(chuàng)建第i個(gè)單元格??? ??
  • ????????????HSSFCell?cell?=?row.createCell(i);? ??
  • ????????????if(cell.getCellType()!=1){ ??
  • ????????????????cell.setCellType(HSSFCell.CELL_TYPE_STRING);?? ??
  • ????????????} ??
  • ???????????? ??
  • ????????????//新增的四句話,設(shè)置CELL格式為文本格式 ??
  • ????????????HSSFCellStyle?cellStyle2?=?demoWorkBook.createCellStyle(); ??
  • ????????????HSSFDataFormat?format?=?demoWorkBook.createDataFormat(); ??
  • ????????????cellStyle2.setDataFormat(format.getFormat("@")); ??
  • ????????????cell.setCellStyle(cellStyle2); ??
  • ???????? ??
  • ????????????cell.setCellValue(cells.get(i));? ??
  • ????????????cell.setCellType(HSSFCell.CELL_TYPE_STRING); ??
  • ????????}?? ??
  • ????}??? ??
  • ???? ??
  • ????/** ?
  • ?????*?USE:用于獲取Cachetable的數(shù)據(jù)。。。假數(shù)據(jù)。到時(shí)候:你連接數(shù)據(jù)庫(kù)的到List<Cachetable>的數(shù)據(jù)就行了。?共生成 ?
  • ?????*?100條數(shù)據(jù).相當(dāng)于100行 ?
  • ?????*? ?
  • ?????*?@return ?
  • ?????*/??
  • ????public?List<Cachetable>?getDate()?{ ??
  • ????????List<Cachetable>?cacheList?=?new?ArrayList<Cachetable>(); ??
  • ????????for?(int?j?=?0;?j?<?300;?j++)?{ ??
  • ????????????Cachetable?tb?=?new?Cachetable(); ??
  • ????????????tb.setRecnum(j?+?1); ??
  • ????????????tb.setDevIp("JavaCrazyer"); ??
  • ????????????tb.setSrcaddr("北京"); ??
  • ????????????tb.setDstaddr("xxx"); ??
  • ????????????tb.setNexthop("yy"); ??
  • ????????????tb.setInput("123"); ??
  • ????????????tb.setOutput("127.0.0.1"); ??
  • ????????????tb.setDpkts("what?are?you?doing?"); ??
  • ????????????tb.setDoctets("who?are?you?"); ??
  • ????????????tb.setSstart("Oh??sure!"); ??
  • ????????????tb.setProt("One"); ??
  • ????????????tb.setTos("two"); ??
  • ????????????tb.setSrcas("three"); ??
  • ????????????tb.setDstas("four"); ??
  • ????????????tb.setPduversion("不知道"); ??
  • ????????????cacheList.add(tb); ??
  • ????????} ??
  • ????????return?cacheList; ??
  • ????} ??
  • ??????? ??
  • ????/**?? ?
  • ?????*?創(chuàng)建整個(gè)Excel表?? ?
  • ?????*?@throws?SQLException??? ?
  • ?????*?? ?
  • ?????*/?? ??
  • ????public??void?createExcelSheet()?throws?SQLException{ ??
  • ????????createTableHeader();??? ??
  • ????????int?rowIndex=1; ??
  • ???????? ??
  • ????????List<Cachetable>?list=getDate(); ??
  • ???????? ??
  • ????????for(int?j=0;j<list.size();j++){ ??
  • ????????????List<String>?listRead=new?ArrayList<String>(); ??
  • ????????for(int?i=1;i<=columNumber;i++){ ??
  • ??????????listRead.add(list.get(i).getDevIp()); ??
  • ??????????listRead.add(list.get(i).getSrcaddr()); ??
  • ??????????listRead.add(list.get(i).getDstaddr()); ??
  • ??????????listRead.add(list.get(i).getNexthop()); ??
  • ??????????listRead.add(list.get(i).getInput()); ??
  • ??????????listRead.add(list.get(i).getOutput()); ??
  • ??????????listRead.add(list.get(i).getDpkts()); ??
  • ??????????listRead.add(list.get(i).getDoctets()); ??
  • ??????????listRead.add(list.get(i).getSstart()); ??
  • ??????????listRead.add(list.get(i).getProt()); ??
  • ??????????listRead.add(list.get(i).getTos()); ??
  • ??????????listRead.add(list.get(i).getSrcas()); ??
  • ??????????listRead.add(list.get(i).getDstas()); ??
  • ??????????listRead.add(list.get(i).getPduversion()); ??
  • ??????????listRead.add(rowIndex+""); ??
  • ????????} ??
  • ?????????createTableRow(listRead,(short)rowIndex);??? ??
  • ?????????rowIndex++;??? ??
  • ????????} ??
  • ????} ??
  • ??? ??
  • ????/**?? ?
  • ?????*?導(dǎo)出表格?? ?
  • ?????*?@param?sheet?? ?
  • ?????*?@param?os?? ?
  • ?????*?@throws?IOException?? ?
  • ?????*/?? ??
  • ????public?void?exportExcel(HSSFSheet?sheet,OutputStream?os)?throws?IOException??? ??
  • ????{??? ??
  • ????????sheet.setGridsPrinted(true);??? ??
  • ????????HSSFFooter?footer?=?sheet.getFooter();??? ??
  • ????????footer.setRight("Page?"?+?HSSFFooter.page()?+?"?of?"?+??? ??
  • ????????HSSFFooter.numPages());??? ??
  • ????????demoWorkBook.write(os);??? ??
  • ????}??? ??
  • ??????? ??
  • ????public?static?void?main(String[]?args)?{??? ??
  • ????????String?fileName?=?"f:\\世界五百?gòu)?qiáng)企業(yè)名次表.xls";??? ??
  • ?????????FileOutputStream?fos?=?null;??? ??
  • ????????????try?{ ??
  • ????????????????ExcelOut?pd?=?new?ExcelOut(); ??
  • ????????????????pd.createExcelSheet(); ??
  • ????????????????fos?=?new?FileOutputStream(fileName);?? ??
  • ????????????????pd.exportExcel(demoSheet,fos); ??
  • ????????????????JOptionPane.showMessageDialog(null,?"表格已成功導(dǎo)出到?:?"+fileName); ??
  • ????????????}?catch?(Exception?e)?{ ??
  • ????????????????JOptionPane.showMessageDialog(null,?"表格導(dǎo)出出錯(cuò),錯(cuò)誤信息?:"+e+"\n錯(cuò)誤原因可能是表格已經(jīng)打開。"); ??
  • ????????????????e.printStackTrace(); ??
  • ????????????}?finally?{ ??
  • ????????????????try?{ ??
  • ????????????????????fos.close();??? ??
  • ????????????????}?catch?(Exception?e)?{??? ??
  • ????????????????????e.printStackTrace();??? ??
  • ????????????????}??? ??
  • ????????????}??? ??
  • ????}??? ??
  • }????
  • package com.zkyy.flow.excel;import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;import javax.swing.JOptionPane;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFFooter; import org.apache.poi.hssf.usermodel.HSSFHeader; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook;import com.kk.flow.webapp.util.Cachetable;public class ExcelOut { //表頭 public static final String[] tableHeader = {"序號(hào)","版本","接收時(shí)刻","設(shè)備","入接口","出接口", "源IP","目的IP","下一跳","協(xié)議","端口","對(duì)端端口","TOS","源AS","目的AS","TCP_FLAG","pad1","pad2"}; //創(chuàng)建工作本 TOSpublic static HSSFWorkbook demoWorkBook = new HSSFWorkbook(); //創(chuàng)建表 public static HSSFSheet demoSheet = demoWorkBook.createSheet("The World's 500 Enterprises"); //表頭的單元格個(gè)數(shù)目 public static final short cellNumber = (short)tableHeader.length; //數(shù)據(jù)庫(kù)表的列數(shù) public static final int columNumber = 1; /** * 創(chuàng)建表頭 * @return */ public static void createTableHeader() { HSSFHeader header = demoSheet.getHeader(); header.setCenter("世界五百?gòu)?qiáng)企業(yè)名次表"); HSSFRow headerRow = demoSheet.createRow((short) 0); for(int i = 0;i < cellNumber;i++) { HSSFCell headerCell = headerRow.createCell((short) i); headerCell.setCellType(HSSFCell.CELL_TYPE_STRING);headerCell.setCellValue(tableHeader[i]); } } /** * 創(chuàng)建行 * @param cells * @param rowIndex */ public static void createTableRow(List<String> cells,short rowIndex) { //創(chuàng)建第rowIndex行 HSSFRow row = demoSheet.createRow((short) rowIndex); for(int i = 0;i < cells.size();i++) { //創(chuàng)建第i個(gè)單元格 HSSFCell cell = row.createCell(i); if(cell.getCellType()!=1){cell.setCellType(HSSFCell.CELL_TYPE_STRING); }//新增的四句話,設(shè)置CELL格式為文本格式HSSFCellStyle cellStyle2 = demoWorkBook.createCellStyle();HSSFDataFormat format = demoWorkBook.createDataFormat();cellStyle2.setDataFormat(format.getFormat("@"));cell.setCellStyle(cellStyle2);cell.setCellValue(cells.get(i)); cell.setCellType(HSSFCell.CELL_TYPE_STRING);} } /*** USE:用于獲取Cachetable的數(shù)據(jù)。。。假數(shù)據(jù)。到時(shí)候:你連接數(shù)據(jù)庫(kù)的到List<Cachetable>的數(shù)據(jù)就行了。 共生成* 100條數(shù)據(jù).相當(dāng)于100行* * @return*/public List<Cachetable> getDate() {List<Cachetable> cacheList = new ArrayList<Cachetable>();for (int j = 0; j < 300; j++) {Cachetable tb = new Cachetable();tb.setRecnum(j + 1);tb.setDevIp("JavaCrazyer");tb.setSrcaddr("北京");tb.setDstaddr("xxx");tb.setNexthop("yy");tb.setInput("123");tb.setOutput("127.0.0.1");tb.setDpkts("what are you doing?");tb.setDoctets("who are you?");tb.setSstart("Oh sure!");tb.setProt("One");tb.setTos("two");tb.setSrcas("three");tb.setDstas("four");tb.setPduversion("不知道");cacheList.add(tb);}return cacheList;}/** * 創(chuàng)建整個(gè)Excel表 * @throws SQLException * */ public void createExcelSheet() throws SQLException{createTableHeader(); int rowIndex=1;List<Cachetable> list=getDate();for(int j=0;j<list.size();j++){List<String> listRead=new ArrayList<String>();for(int i=1;i<=columNumber;i++){listRead.add(list.get(i).getDevIp());listRead.add(list.get(i).getSrcaddr());listRead.add(list.get(i).getDstaddr());listRead.add(list.get(i).getNexthop());listRead.add(list.get(i).getInput());listRead.add(list.get(i).getOutput());listRead.add(list.get(i).getDpkts());listRead.add(list.get(i).getDoctets());listRead.add(list.get(i).getSstart());listRead.add(list.get(i).getProt());listRead.add(list.get(i).getTos());listRead.add(list.get(i).getSrcas());listRead.add(list.get(i).getDstas());listRead.add(list.get(i).getPduversion());listRead.add(rowIndex+"");}createTableRow(listRead,(short)rowIndex); rowIndex++; }}/** * 導(dǎo)出表格 * @param sheet * @param os * @throws IOException */ public void exportExcel(HSSFSheet sheet,OutputStream os) throws IOException { sheet.setGridsPrinted(true); HSSFFooter footer = sheet.getFooter(); footer.setRight("Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages()); demoWorkBook.write(os); } public static void main(String[] args) { String fileName = "f:\\世界五百?gòu)?qiáng)企業(yè)名次表.xls"; FileOutputStream fos = null; try {ExcelOut pd = new ExcelOut();pd.createExcelSheet();fos = new FileOutputStream(fileName); pd.exportExcel(demoSheet,fos);JOptionPane.showMessageDialog(null, "表格已成功導(dǎo)出到 : "+fileName);} catch (Exception e) {JOptionPane.showMessageDialog(null, "表格導(dǎo)出出錯(cuò),錯(cuò)誤信息 :"+e+"\n錯(cuò)誤原因可能是表格已經(jīng)打開。");e.printStackTrace();} finally {try {fos.close(); } catch (Exception e) { e.printStackTrace(); } } } }

    ?

    ?

    說(shuō)明:

    ?? 1)有關(guān)數(shù)據(jù)庫(kù)連接,如果操作到數(shù)據(jù)庫(kù)的話,在遍歷數(shù)據(jù)庫(kù)時(shí)用getDate這個(gè)方法遍歷就可以啦,那么插入的數(shù)據(jù)就不是定值了,而是數(shù)據(jù)庫(kù)中的值哦,具體操作數(shù)據(jù)庫(kù)的步驟,我不用說(shuō),你懂得

    ?? 2)有關(guān)涉及更改EXCEL的CELL格式為字符串,如圖一般情況下大家導(dǎo)出的EXCEL表格CELL格式通常是常規(guī)的



    ?? 這個(gè)問(wèn)題,估計(jì)已經(jīng)不止一兩個(gè)朋友在網(wǎng)上問(wèn)過(guò),我至今沒有看到一個(gè)滿意的答案,通常大家都是想到既然是設(shè)置CELL格式肯定是通過(guò)cell.setCellType(HSSFCell.CELL_TYPE_STRING)然后插入數(shù)據(jù)再導(dǎo)出,誠(chéng)然這種想法是對(duì)的,實(shí)際上不能起到任何作用,因?yàn)檫@個(gè)方法就是EXCEL默認(rèn)的格式,寫不寫都一樣(好多同學(xué)都不知道吧),再寫出我的解決方案之前請(qǐng)大家參考下一段文字

    ?

    第一段:Excel的單元格格式
    圖中的數(shù)據(jù)有數(shù)值、貨幣、時(shí)間、日期、文本等格式。這些數(shù)據(jù)格式在POI中的HSSFDataFormat類里都有相應(yīng)的定義。
    HSSFDataFormat是HSSF子項(xiàng)目里面定義的一個(gè)類。類HSSFDataFormat允許用戶新建數(shù)據(jù)格式類型。HSSFDataFormat類包含靜態(tài)方法static java.lang.String getBuiltinFormat(short index),它可以根據(jù)編號(hào)返回內(nèi)置數(shù)據(jù)類型。另外static short getBuiltinFormat(java.lang.String format)方法則可以根據(jù)數(shù)據(jù)類型返回其編號(hào),static java.util.List getBuiltinFormats()可以返回整個(gè)內(nèi)置的數(shù)據(jù)格式列表。
    在HSSFDataFormat里一共定義了49種內(nèi)置的數(shù)據(jù)格式,如下面所示。

    ?HSSFDataFormat的數(shù)據(jù)格式

    內(nèi)置數(shù)據(jù)類型
    編號(hào)

    "General"
    0

    "0"
    1

    "0.00"
    2

    "#,##0"
    3

    "#,##0.00"
    4

    "($#,##0_);($#,##0)"
    5

    "($#,##0_);[Red]($#,##0)"
    6

    "($#,##0.00);($#,##0.00)"
    7

    "($#,##0.00_);[Red]($#,##0.00)"
    8

    "0%"
    9

    "0.00%"
    0xa

    "0.00E+00"
    0xb

    "# ?/?"
    0xc

    "# ??/??"
    0xd

    "m/d/yy"
    0xe

    "d-mmm-yy"
    0xf

    "d-mmm"
    0x10

    "mmm-yy"
    0x11

    "h:mm AM/PM"
    0x12

    "h:mm:ss AM/PM"
    0x13

    "h:mm"
    0x14

    "h:mm:ss"
    0x15

    "m/d/yy h:mm"
    0x16

    保留為過(guò)國(guó)際化用
    0x17 - 0x24

    "(#,##0_);(#,##0)"
    0x25

    "(#,##0_);[Red](#,##0)"
    0x26

    "(#,##0.00_);(#,##0.00)"
    0x27

    "(#,##0.00_);[Red](#,##0.00)"
    0x28

    "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"
    0x29

    "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"
    0x2a

    "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"
    0x2b

    "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"
    0x2c

    "mm:ss"
    0x2d

    "[h]:mm:ss"
    0x2e

    "mm:ss.0"
    0x2f

    "##0.0E+0"
    0x30

    "@" - This is text format
    0x31

    在上面表中,字符串類型所對(duì)應(yīng)的是數(shù)據(jù)格式為"@"(最后一行),也就是HSSFDataFormat中定義的值為0x31(49)的那行。Date類型的值的范圍是0xe-0x11,本例子中的Date格式為""m/d/yy"",在HSSFDataFormat定義的值為0xe(14)。 ?

    ?

    ?

    ?

    第二段:POI中Excel文件Cell的類型
    在讀取每一個(gè)Cell的值的時(shí)候,通過(guò)getCellType方法獲得當(dāng)前Cell的類型,在Excel中Cell有6種類型,如下面所示。

    Cell的類型

    CellType
    說(shuō)明

    CELL_TYPE_BLANK
    空值

    CELL_TYPE_BOOLEAN
    布爾型

    CELL_TYPE_ERROR
    錯(cuò)誤

    CELL_TYPE_FORMULA
    公式型

    CELL_TYPE_STRING
    字符串型

    CELL_TYPE_NUMERIC
    數(shù)值型

    一般都采用CELL_TYPE_STRING和CELL_TYPE_NUMERIC類型,因?yàn)樵贓xcel文件中只有字符串和數(shù)字。如果Cell的Type為CELL_TYPE_NUMERIC時(shí),還需要進(jìn)一步判斷該Cell的數(shù)據(jù)格式,因?yàn)樗锌赡苁荄ate類型,在Excel中的Date類型也是以Double類型的數(shù)字存儲(chǔ)的。Excel中的Date表示當(dāng)前時(shí)間與1900年1月1日相隔的天數(shù),所以需要調(diào)用HSSFDateUtil的isCellDateFormatted方法,判斷該Cell的數(shù)據(jù)格式是否是Excel Date類型。如果是,則調(diào)用getDateCellValue方法,返回一個(gè)Java類型的Date。

    ?

    ?

    好了讀完上面兩段文字我想大家關(guān)于CELL類型和格式應(yīng)該清楚了,更應(yīng)該清楚的是到底怎么才能將‘設(shè)置單元格格式’改成文本然后再導(dǎo)出

    解決方案:就是上面代碼中的ExcelOut類里面createTableRow方法中的一段代碼

    ?

    ?? ? ? ? ? ?HSSFCellStyle cellStyle2 = demoWorkBook.createCellStyle();

    ?? ? ? ? ? ?HSSFDataFormat format = demoWorkBook.createDataFormat();

    ?? ? ? ? ? ?cellStyle2.setDataFormat(format.getFormat("@"));

    ?? ? ? ? ? ?cell.setCellStyle(cellStyle2);

    看最終導(dǎo)出效果圖吧,點(diǎn)擊任何一個(gè)CELL右鍵設(shè)置單元格格式


    ?

    ?

    ?3)??JOptionPane.showMessageDialog(null, "表格已成功導(dǎo)出到 : "+fileName);這句話有點(diǎn)意思



    ?看到?jīng)]這就是javax.swing.JOptionPane類的有關(guān)消息輸出的好處,很方便使用

    ?

    總結(jié)

    以上是生活随笔為你收集整理的POI 单元格格式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。