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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

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

目前的POI版本是3.7

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

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

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

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

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;

}

第二部分:具體的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?=?{"序號","版本","接收時刻","設備","入接口","出接口",

"源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?

{

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?cells,short?rowIndex)

{

//創(chuàng)建第rowIndex行

HSSFRow?row?=?demoSheet.createRow((short)?rowIndex);

for(int?i?=?0;i?

{

//創(chuàng)建第i個單元格

HSSFCell?cell?=?row.createCell(i);

if(cell.getCellType()!=1){

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

}

//新增的四句話,設置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的數(shù)據(jù)就行了。?共生成

*?100條數(shù)據(jù).相當于100行

*

*?@return

*/

public?List?getDate()?{

List?cacheList?=?new?ArrayList();

for?(int?j?=?0;?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?list=getDate();

for(int?j=0;j

List?listRead=new?ArrayList();

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++;

}

}

/**

*?導出表格

*?@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,?"表格已成功導出到?:?"+fileName);

}?catch?(Exception?e)?{

JOptionPane.showMessageDialog(null,?"表格導出出錯,錯誤信息?:"+e+"\n錯誤原因可能是表格已經打開。");

e.printStackTrace();

}?finally?{

try?{

fos.close();

}?catch?(Exception?e)?{

e.printStackTrace();

}

}

}

}

說明:

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

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

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

第一段:Excel的單元格格式

圖中的數(shù)據(jù)有數(shù)值、貨幣、時間、日期、文本等格式。這些數(shù)據(jù)格式在POI中的HSSFDataFormat類里都有相應的定義。

HSSFDataFormat是HSSF子項目里面定義的一個類。類HSSFDataFormat允許用戶新建數(shù)據(jù)格式類型。HSSFDataFormat類包含靜態(tài)方法static java.lang.String getBuiltinFormat(short index),它可以根據(jù)編號返回內置數(shù)據(jù)類型。另外static short getBuiltinFormat(java.lang.String format)方法則可以根據(jù)數(shù)據(jù)類型返回其編號,static java.util.List getBuiltinFormats()可以返回整個內置的數(shù)據(jù)格式列表。

在HSSFDataFormat里一共定義了49種內置的數(shù)據(jù)格式,如下面所示。

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

內置數(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

在上面表中,字符串類型所對應的是數(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ù),所以需要調用HSSFDateUtil的isCellDateFormatted方法,判斷該Cell的數(shù)據(jù)格式是否是Excel Date類型。如果是,則調用getDateCellValue方法,返回一個Java類型的Date。

好了讀完上面兩段文字我想大家關于CELL類型和格式應該清楚了,更應該清楚的是到底怎么才能將‘設置單元格格式’改成文本然后再導出

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

HSSFCellStyle cellStyle2 = demoWorkBook.createCellStyle();

HSSFDataFormat format = demoWorkBook.createDataFormat();

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

cell.setCellStyle(cellStyle2);

看最終導出效果圖吧,點擊任何一個CELL右鍵設置單元格格式

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

看到沒這就是javax.swing.JOptionPane類的有關消息輸出的好處,很方便使用

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产色网址 | 永久在线 | 黑人精品无码一区二区三区AV | 亚洲成肉网 | 国产精品毛片久久久久久 | 黑人巨大猛交丰满少妇 | 老司机午夜免费视频 | 亚洲 国产 日韩 欧美 | 男女在线观看视频 | 黄色无遮挡网站 | 男人日女人的网站 | 欧美v日本| 欧美一区 | 国产精品日韩欧美一区二区三区 | 狠狠操操 | 波多野结衣欲乱上班族 | 熟女视频一区二区三区 | aaa一区二区 | 欧美人与动牲交xxxxbbbb | 成人精品在线播放 | 最近最新中文字幕 | 日韩午夜免费 | 日韩国产在线一区 | 就爱操av | 嫩草影院国产 | 亚洲熟妇av日韩熟妇在线 | 视频一区在线播放 | 91九色pron| 国产一线二线三线在线观看 | jizzz18 | 日本韩国欧美一区二区三区 | 狠狠操综合| 日本免费不卡一区二区 | 国产精品mv| 麻豆一区二区三区精品视频 | 亚洲成人系列 | 国产在线国偷精品免费看 | 丝袜 亚洲 另类 欧美 重口 | 亚洲激情视频在线播放 | 爱福利视频一区二区 | 欧美日韩视频无码一区二区三 | 日本成人一级片 | 黄色免费一级 | 色小姐综合 | 性色视频网站 | av在线观看地址 | 天天插综合网 | 精品日本一区二区 | 91网站永久免费看nba视频 | 日韩欧美在线免费观看 | 黄色录象片 | 视频一区二区免费 | 狠狠的干狠狠的操 | 91毛片视频| 日本视频在线免费观看 | 邻居校草天天肉我h1v1 | 日韩精品五区 | 精品久操 | 国产xxxxxx| 开心激情婷婷 | 99久久精品免费看国产免费软件 | 狠狠干老司机 | 免费极品av一视觉盛宴 | 黄色aaa大片 | 欧美日韩一区二区三区四区 | 免费av软件| 国产馆在线观看 | 超碰在线资源 | 五月天中文字幕 | 中文字字幕码一二三区 | 天天干在线观看 | 国产三级免费观看 | www.黄色网址.com| 一区二区三区精品视频 | 91av不卡 | 一级色视频 | 亚洲一区 中文字幕 | 亚洲精品成人在线视频 | av在线地址 | 国产精品久久久久久久久动漫 | xxxwww黄色 | 爽爽窝窝午夜精品一区二区 | 亚洲午夜一区二区 | 天天操天天操天天操 | 亚洲精品一区二区三区不卡 | 久久久五月| 神马老子午夜 | 欧美日韩国产电影 | 国产欧美一区二区三区白浆喷水 | 亚洲一区二区色图 | 夫妻性生活黄色大片 | 就是色| 国产伦理在线观看 | 99热黄色| 93看片淫黄大片一级 | 黄色日批网站 | 午夜操一操 | 精品免费一区二区三区 | a∨色狠狠一区二区三区 |