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

实际开发过程中通常用到的就是从数据库导出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表头类

public class Cachetable {

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. private String dstport;
  13. private String prot;
  14. private String tos;
  15. private String srcas;
  16. private String dstas;
  17. private String pduversion;
  18. /** default constructor */
  19. public Cachetable() {
  20. }
  21. /** full constructor */
  22. 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) {
  23. this.recnum = recnum;
  24. this.devIp = devIp;
  25. this.srcaddr = srcaddr;
  26. this.dstaddr = dstaddr;
  27. this.nexthop = nexthop;
  28. this.input = input;
  29. this.output = output;
  30. this.dpkts = dpkts;
  31. this.doctets = doctets;
  32. this.sstart = sstart;
  33. this.dstport = dstport;
  34. this.prot = prot;
  35. this.tos = tos;
  36. this.srcas = srcas;
  37. this.dstas = dstas;
  38. this.pduversion = pduversion;
  39. }
  40. public int getRecnum() {
  41. return this.recnum;
  42. }
  43. public void setRecnum(int recnum) {
  44. this.recnum= recnum;
  45. }
  46. public String getDevIp() {
  47. return this.devIp;
  48. }
  49. public void setDevIp(String devIp) {
  50. this.devIp = devIp;
  51. }
  52. public String getSrcaddr() {
  53. return this.srcaddr;
  54. }
  55. public void setSrcaddr(String srcaddr) {
  56. this.srcaddr = srcaddr;
  57. }
  58. public String getDstaddr() {
  59. return this.dstaddr;
  60. }
  61. public void setDstaddr(String dstaddr) {
  62. this.dstaddr = dstaddr;
  63. }
  64. public String getNexthop() {
  65. return this.nexthop;
  66. }
  67. public void setNexthop(String nexthop) {
  68. this.nexthop = nexthop;
  69. }
  70. public String getInput() {
  71. return this.input;
  72. }
  73. public void setInput(String input) {
  74. this.input = input;
  75. }
  76. public String getOutput() {
  77. return this.output;
  78. }
  79. public void setOutput(String output) {
  80. this.output = output;
  81. }
  82. public String getDpkts() {
  83. return this.dpkts;
  84. }
  85. public void setDpkts(String dpkts) {
  86. this.dpkts = dpkts;
  87. }
  88. public String getDoctets() {
  89. return this.doctets;
  90. }
  91. public void setDoctets(String doctets) {
  92. this.doctets = doctets;
  93. }
  94. public String getSstart() {
  95. return this.sstart;
  96. }
  97. public void setSstart(String sstart) {
  98. this.sstart = sstart;
  99. }
  100. public String getDstport() {
  101. return this.dstport;
  102. }
  103. public void setDstport(String dstport) {
  104. this.dstport = dstport;
  105. }
  106. public String getProt() {
  107. return this.prot;
  108. }
  109. public void setProt(String prot) {
  110. this.prot = prot;
  111. }
  112. public String getTos() {
  113. return this.tos;
  114. }
  115. public void setTos(String tos) {
  116. this.tos = tos;
  117. }
  118. public String getSrcas() {
  119. return this.srcas;
  120. }
  121. public void setSrcas(String srcas) {
  122. this.srcas = srcas;
  123. }
  124. public String getDstas() {
  125. return this.dstas;
  126. }
  127. public void setDstas(String dstas) {
  128. this.dstas = dstas;
  129. }
  130. public String getPduversion() {
  131. return this.pduversion;
  132. }
  133. public void setPduversion(String pduversion) {
  134. this.pduversion = pduversion;
  135. }
    // 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类

【我这里只是个示例,没连数据库,直接运行即可,如果想连,稍微变动一点即可】

Java代码  
  1. package com.zkyy.flow.excel;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import javax.swing.JOptionPane;
  9. import org.apache.poi.hssf.usermodel.HSSFCell;
  10. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  11. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  12. import org.apache.poi.hssf.usermodel.HSSFFooter;
  13. import org.apache.poi.hssf.usermodel.HSSFHeader;
  14. import org.apache.poi.hssf.usermodel.HSSFRow;
  15. import org.apache.poi.hssf.usermodel.HSSFSheet;
  16. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  17. import com.kk.flow.webapp.util.Cachetable;
  18. public class ExcelOut {
  19. //表头
  20. public static final String[] tableHeader = {"序号","版本","接收时刻","设备","入接口","出接口",
  21. "源IP","目的IP","下一跳","协议","端口","对端端口","TOS","源AS","目的AS","TCP_FLAG","pad1","pad2"};
  22. //创建工作本   TOS
  23. public static HSSFWorkbook demoWorkBook = new HSSFWorkbook();
  24. //创建表
  25. public static HSSFSheet demoSheet = demoWorkBook.createSheet("The World's 500 Enterprises");
  26. //表头的单元格个数目
  27. public static final short cellNumber = (short)tableHeader.length;
  28. //数据库表的列数
  29. public static final int columNumber = 1;
  30. /**
  31. * 创建表头
  32. * @return
  33. */
  34. public static void createTableHeader()
  35. {
  36. HSSFHeader header = demoSheet.getHeader();
  37. header.setCenter("世界五百强企业名次表");
  38. HSSFRow headerRow = demoSheet.createRow((short) 0);
  39. for(int i = 0;i < cellNumber;i++)
  40. {
  41. HSSFCell headerCell = headerRow.createCell((short) i);
  42. headerCell.setCellType(HSSFCell.CELL_TYPE_STRING);
  43. headerCell.setCellValue(tableHeader[i]);
  44. }
  45. }
  46. /**
  47. * 创建行
  48. * @param cells
  49. * @param rowIndex
  50. */
  51. public static void createTableRow(List<String> cells,short rowIndex)
  52. {
  53. //创建第rowIndex行
  54. HSSFRow row = demoSheet.createRow((short) rowIndex);
  55. for(int i = 0;i < cells.size();i++)
  56. {
  57. //创建第i个单元格
  58. HSSFCell cell = row.createCell(i);
  59. if(cell.getCellType()!=1){
  60. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  61. }
  62. //新增的四句话,设置CELL格式为文本格式
  63. HSSFCellStyle cellStyle2 = demoWorkBook.createCellStyle();
  64. HSSFDataFormat format = demoWorkBook.createDataFormat();
  65. cellStyle2.setDataFormat(format.getFormat("@"));
  66. cell.setCellStyle(cellStyle2);
  67. cell.setCellValue(cells.get(i));
  68. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  69. }
  70. }
  71. /**
  72. * USE:用于获取Cachetable的数据。。。假数据。到时候:你连接数据库的到List<Cachetable>的数据就行了。 共生成
  73. * 100条数据.相当于100行
  74. *
  75. * @return
  76. */
  77. public List<Cachetable> getDate() {
  78. List<Cachetable> cacheList = new ArrayList<Cachetable>();
  79. for (int j = 0; j < 300; j++) {
  80. Cachetable tb = new Cachetable();
  81. tb.setRecnum(j + 1);
  82. tb.setDevIp("JavaCrazyer");
  83. tb.setSrcaddr("北京");
  84. tb.setDstaddr("xxx");
  85. tb.setNexthop("yy");
  86. tb.setInput("123");
  87. tb.setOutput("127.0.0.1");
  88. tb.setDpkts("what are you doing?");
  89. tb.setDoctets("who are you?");
  90. tb.setSstart("Oh  sure!");
  91. tb.setProt("One");
  92. tb.setTos("two");
  93. tb.setSrcas("three");
  94. tb.setDstas("four");
  95. tb.setPduversion("不知道");
  96. cacheList.add(tb);
  97. }
  98. return cacheList;
  99. }
  100. /**
  101. * 创建整个Excel表
  102. * @throws SQLException
  103. *
  104. */
  105. public  void createExcelSheet() throws SQLException{
  106. createTableHeader();
  107. int rowIndex=1;
  108. List<Cachetable> list=getDate();
  109. for(int j=0;j<list.size();j++){
  110. List<String> listRead=new ArrayList<String>();
  111. for(int i=1;i<=columNumber;i++){
  112. listRead.add(list.get(i).getDevIp());
  113. listRead.add(list.get(i).getSrcaddr());
  114. listRead.add(list.get(i).getDstaddr());
  115. listRead.add(list.get(i).getNexthop());
  116. listRead.add(list.get(i).getInput());
  117. listRead.add(list.get(i).getOutput());
  118. listRead.add(list.get(i).getDpkts());
  119. listRead.add(list.get(i).getDoctets());
  120. listRead.add(list.get(i).getSstart());
  121. listRead.add(list.get(i).getProt());
  122. listRead.add(list.get(i).getTos());
  123. listRead.add(list.get(i).getSrcas());
  124. listRead.add(list.get(i).getDstas());
  125. listRead.add(list.get(i).getPduversion());
  126. listRead.add(rowIndex+"");
  127. }
  128. createTableRow(listRead,(short)rowIndex);
  129. rowIndex++;
  130. }
  131. }
  132. /**
  133. * 导出表格
  134. * @param sheet
  135. * @param os
  136. * @throws IOException
  137. */
  138. public void exportExcel(HSSFSheet sheet,OutputStream os) throws IOException
  139. {
  140. sheet.setGridsPrinted(true);
  141. HSSFFooter footer = sheet.getFooter();
  142. footer.setRight("Page " + HSSFFooter.page() + " of " +
  143. HSSFFooter.numPages());
  144. demoWorkBook.write(os);
  145. }
  146. public static void main(String[] args) {
  147. String fileName = "f:\\世界五百强企业名次表.xls";
  148. FileOutputStream fos = null;
  149. try {
  150. ExcelOut pd = new ExcelOut();
  151. pd.createExcelSheet();
  152. fos = new FileOutputStream(fileName);
  153. pd.exportExcel(demoSheet,fos);
  154. JOptionPane.showMessageDialog(null, "表格已成功导出到 : "+fileName);
  155. } catch (Exception e) {
  156. JOptionPane.showMessageDialog(null, "表格导出出错,错误信息 :"+e+"\n错误原因可能是表格已经打开。");
  157. e.printStackTrace();
  158. } finally {
  159. try {
  160. fos.close();
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. }
  164. }
  165. }
  166. }
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","下一跳","协议","端口","对端端口","TOS","源AS","目的AS","TCP_FLAG","pad1","pad2"};
//创建工作本   TOS
public static HSSFWorkbook demoWorkBook = new HSSFWorkbook();
//创建表
public static HSSFSheet demoSheet = demoWorkBook.createSheet("The World's 500 Enterprises");
//表头的单元格个数目
public static final short cellNumber = (short)tableHeader.length;
//数据库表的列数
public static final int columNumber = 1;
/**
* 创建表头
* @return
*/
public static void createTableHeader()
{
HSSFHeader header = demoSheet.getHeader();
header.setCenter("世界五百强企业名次表");
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]);
}
}
/**
* 创建行
* @param cells
* @param rowIndex
*/
public static void createTableRow(List<String> cells,short rowIndex)
{
//创建第rowIndex行
HSSFRow row = demoSheet.createRow((short) rowIndex);
for(int i = 0;i < cells.size();i++)
{
//创建第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的数据。。。假数据。到时候:你连接数据库的到List<Cachetable>的数据就行了。 共生成
* 100条数据.相当于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;
}
/**
* 创建整个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++;
}
}
/**
* 导出表格
* @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:\\世界五百强企业名次表.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)有关数据库连接,如果操作到数据库的话,在遍历数据库时用getDate这个方法遍历就可以啦,那么插入的数据就不是定值了,而是数据库中的值哦,具体操作数据库的步骤,我不用说,你懂得

2)有关涉及更改EXCEL的CELL格式为字符串,如图一般情况下大家导出的EXCEL表格CELL格式通常是常规的


   这个问题,估计已经不止一两个朋友在网上问过,我至今没有看到一个满意的答案,通常大家都是想到既然是设置CELL格式肯定是通过cell.setCellType(HSSFCell.CELL_TYPE_STRING)然后插入数据再导出,诚然这种想法是对的,实际上不能起到任何作用,因为这个方法就是EXCEL默认的格式,写不写都一样(好多同学都不知道吧),再写出我的解决方案之前请大家参考下一段文字

第一段:Excel的单元格格式
图中的数据有数值、货币、时间、日期、文本等格式。这些数据格式在POI中的HSSFDataFormat类里都有相应的定义。
HSSFDataFormat是HSSF子项目里面定义的一个类。类HSSFDataFormat允许用户新建数据格式类型。HSSFDataFormat类包含静态方法static java.lang.String getBuiltinFormat(short index),它可以根据编号返回内置数据类型。另外static short getBuiltinFormat(java.lang.String format)方法则可以根据数据类型返回其编号,static java.util.List getBuiltinFormats()可以返回整个内置的数据格式列表。
在HSSFDataFormat里一共定义了49种内置的数据格式,如下面所示。

HSSFDataFormat的数据格式

内置数据类型
编号

"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

在上面表中,字符串类型所对应的是数据格式为"@"(最后一行),也就是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
数值型

一般都采用CELL_TYPE_STRING和CELL_TYPE_NUMERIC类型,因为在Excel文件中只有字符串和数字。如果Cell的Type为CELL_TYPE_NUMERIC时,还需要进一步判断该Cell的数据格式,因为它有可能是Date类型,在Excel中的Date类型也是以Double类型的数字存储的。Excel中的Date表示当前时间与1900年1月1日相隔的天数,所以需要调用HSSFDateUtil的isCellDateFormatted方法,判断该Cell的数据格式是否是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类的有关消息输出的好处,很方便使用

PS:更多的关于POI设置EXCEL单元格格式为数字、百分比、货币、日期等等格式的,请看我接下来要写的文章

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

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

    实际开发过程中通常用到的就是从数据库导出EXCEL表格了,JXL可以这样做,其实POI也可以(关于JXL与POI的异同可访问我之前总结的文章),之前写过POI对七种文档(当然也包括EXCEL)的内容读 ...

  2. java excel 操作 poi_Java使用apache poi进行excel相关操作

    一.基本介绍 1.1.Apache POI介绍 Apache POI是一个可以进行微软的文档进行开源库,可以操作的文档类型包括word.ppt.excel.visio.outlook.... 本文主要 ...

  3. java-EasyExcel导出excel设置单元格为文本格式(含代码)

    java-EasyExcel导出excel设置单元格为文本格式(含代码) 在使用EasyExcel导出excel模板时.我们会发现导出的日期和大长度数字都会自动更换格式,不是文本格式.并且在空白单元格 ...

  4. EXCEL中如何将日期格式转换为文本格式

    日期格式的单元格内容应当如何转换为文本格式呢? 本文将针对日期格式和文本格式的转换进行简要讲解. 工具/原料 EXCEL 方法/步骤 如本例,A列中列示的内容为日期格式,要求将A列中的日期格式转换为文 ...

  5. Excel中的数字格式与文本格式进行转换

    Excel中的数字格式与文本格式进行转换 有时候因为在excel中的单元格中要输入比较多的数字例如身份证号或者银行账户号等,excel中的单元格就会出现类似下面图片中的"乱文". ...

  6. Excel批量把数字格式变成文本格式且不用双击出现左上绿标

    Excel批量把数字格式变成文本格式且不用双击出现左上绿标 解决方案 问题描述: excel里面有大量数字,并且小数点后几位都不显示, 复制到txt文本里面都是缩略的数据, 于是设置成文本格式,复制过 ...

  7. 修改样式_Word小技巧:如何设置样式 快速修改文本格式

    设置样式 找到菜单栏-开始下的样式模块, 将光标停留在需要设置样式的文本任意处,或者直接选定需要设置样式的文本,点击样式模块中需要的格式即可. 备注:点击带小横线的向下三角符号,可以显示出更多的格式. ...

  8. Springboot+POI通用Excel表格导出表头样式设置方法

    private void setSheetHeader(XSSFWorkbook xWorkbook, Sheet sh,String[] title) { // 设置单元格格式为文本格式XSSFDa ...

  9. 【Excel设置任意列为默认文本格式】

    //工作薄对象private Workbook wb;//工作表对象private Sheet sheet;this.wb = new SXSSFWorkbook(500);this.sheet = ...

最新文章

  1. SpringMVC源码系列:HandlerMapping
  2. 模块的四种形式 模块的调用 循环导入问题 模块的搜索路径 py文件的两种用途 编译python文件 包...
  3. 070_获取日期方法
  4. springboot1.5.10兼容高版本6.1.1elasticsearch
  5. JZOJ 5405. 【NOIP2017提高A组模拟10.10】Permutation
  6. 助力小白常见JS逆向乱杀喂饭教程——Url加密
  7. webplugin 没有画面_[问题记录] webpack devServer HtmlWebpackPlugin 没有加载 js、css
  8. 网络安全08-虚拟机运行架构(寄居架构+原生架构)、虚拟机产品简单介绍、windows操作系统--屏蔽系统自动更新
  9. Springboot后台接收前端Date类型
  10. 移动开发(C#、VB.NET)Smobiler开发平台——GifView控件的使用方式
  11. [转载] Java 中字符串转整型和整型转字符串
  12. System Center Virtual Machine Manager 2012 安装
  13. 将一个项目打成一个jar包,导入到另一个项目中并调用
  14. git commit之后后面的操作步骤
  15. win10 看计算机配置要求,win10怎么看电脑配置
  16. JAVA求数组的平均数,众数,中位数
  17. 气门组的结构组成有哪些_挖机破碎钳子,液压粉碎钳都有哪些结构组成,你知道吗...
  18. PE详解之IMAGE_NT_HEADERS结构定义即各个属性的作用(PE详解02)
  19. jspx框架使用总结-页面开发
  20. 计算机A级学科排名,“计算机科学与技术”学科排名出炉,上交大无缘A+吉大表现亮眼...

热门文章

  1. 百练 1363.Rails
  2. 2013\National _C_C++_B\1.猜灯谜
  3. 【STM32】DMA详解
  4. 【Linux系统编程】进程同步与互斥:POSIX有名信号量
  5. android 约束布局的坑,android - 使用android约束布局2.0.0 Flow将项目放置一行 - 堆栈内存溢出...
  6. 服务器存储系统技术方案,服务器存储技术方案.pdf
  7. 每天一道LeetCode-----对表达式添加括号并求值,返回所有可能的计算结果
  8. java里的主线程和子线程以及finally不会执行的特殊情况
  9. hdoop(2)——hdfs一些常用的配置文件
  10. Linux操作(3)—— 重定向操作