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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POI对EXCEL的操作【重点:如何设置CELL格式为文本格式】

發(fā)布時間:2024/4/17 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POI对EXCEL的操作【重点:如何设置CELL格式为文本格式】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

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

目前的POI版本是3.7

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

?必須包只有一個:poi-3.7-20101029.jar


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

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

?? ? ? ? ? ? ? ?3)為了方便操作,頂層的cell各個名稱可以抽取出來成為一個單獨類

具體代碼

?? 第一部分:單獨的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類

    【我這里只是個示例,沒連數(shù)據(jù)庫,直接運行即可,如果想連,稍微變動一點即可】

    ?

    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?=?{"序號","版本","接收時刻","設(shè)備","入接口","出接口",??? ??
  • ????????"源IP","目的IP","下一跳","協(xié)議","端口","對端端口","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");??? ??
  • ????//表頭的單元格個數(shù)目??? ??
  • ????public?static?final?short?cellNumber?=?(short)tableHeader.length;??? ??
  • ????//數(shù)據(jù)庫表的列數(shù)??? ??
  • ????public?static?final?int?columNumber?=?1;??? ??
  • ????/**?? ?
  • ?????*?創(chuàng)建表頭?? ?
  • ?????*?@return?? ?
  • ?????*/?? ??
  • ????public?static?void?createTableHeader()??? ??
  • ????{??? ??
  • ????????HSSFHeader?header?=?demoSheet.getHeader();??? ??
  • ????????header.setCenter("世界五百強企業(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個單元格??? ??
  • ????????????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ù)據(jù)庫的到List<Cachetable>的數(shù)據(jù)就行了。?共生成 ?
  • ?????*?100條數(shù)據(jù).相當于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)建整個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:\\世界五百強企業(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)出出錯,錯誤信息?:"+e+"\n錯誤原因可能是表格已經(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 = {"序號","版本","接收時刻","設(shè)備","入接口","出接口", "源IP","目的IP","下一跳","協(xié)議","端口","對端端口","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"); //表頭的單元格個數(shù)目 public static final short cellNumber = (short)tableHeader.length; //數(shù)據(jù)庫表的列數(shù) public static final int columNumber = 1; /** * 創(chuàng)建表頭 * @return */ public static void createTableHeader() { HSSFHeader header = demoSheet.getHeader(); header.setCenter("世界五百強企業(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個單元格 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ù)據(jù)庫的到List<Cachetable>的數(shù)據(jù)就行了。 共生成* 100條數(shù)據(jù).相當于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)建整個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:\\世界五百強企業(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)出出錯,錯誤信息 :"+e+"\n錯誤原因可能是表格已經(jīng)打開。");e.printStackTrace();} finally {try {fos.close(); } catch (Exception e) { e.printStackTrace(); } } } }

    ?

    ?

    說明:

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

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



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

    ?

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

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

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

    "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

    保留為過國際化用
    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

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

    ?

    ?

    ?

    第二段:POI中Excel文件Cell的類型
    在讀取每一個Cell的值的時候,通過getCellType方法獲得當前Cell的類型,在Excel中Cell有6種類型,如下面所示。

    Cell的類型

    CellType
    說明

    CELL_TYPE_BLANK
    空值

    CELL_TYPE_BOOLEAN
    布爾型

    CELL_TYPE_ERROR
    錯誤

    CELL_TYPE_FORMULA
    公式型

    CELL_TYPE_STRING
    字符串型

    CELL_TYPE_NUMERIC
    數(shù)值型

    一般都采用CELL_TYPE_STRING和CELL_TYPE_NUMERIC類型,因為在Excel文件中只有字符串和數(shù)字。如果Cell的Type為CELL_TYPE_NUMERIC時,還需要進一步判斷該Cell的數(shù)據(jù)格式,因為它有可能是Date類型,在Excel中的Date類型也是以Double類型的數(shù)字存儲的。Excel中的Date表示當前時間與1900年1月1日相隔的天數(shù),所以需要調(diào)用HSSFDateUtil的isCellDateFormatted方法,判斷該Cell的數(shù)據(jù)格式是否是Excel Date類型。如果是,則調(diào)用getDateCellValue方法,返回一個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)出效果圖吧,點擊任何一個CELL右鍵設(shè)置單元格格式


    ?

    ?

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



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

    ?

    ?

    PS:更多的關(guān)于POI設(shè)置EXCEL單元格格式為數(shù)字、百分比、貨幣、日期等等格式的,請看我接下來要寫的文章

    ?

    總結(jié)

    以上是生活随笔為你收集整理的POI对EXCEL的操作【重点:如何设置CELL格式为文本格式】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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