【web开发】☆★之利用POI操作Excel表格系列教程【4】设置时间单元格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package  com.xiaoye.demo;
import  java.io.FileOutputStream;
import  java.util.Calendar;
import  java.util.Date;
import  org.apache.poi.hssf.usermodel.HSSFWorkbook;
import  org.apache.poi.ss.usermodel.Cell;
import  org.apache.poi.ss.usermodel.CellStyle;
import  org.apache.poi.ss.usermodel.CreationHelper;
import  org.apache.poi.ss.usermodel.Row;
import  org.apache.poi.ss.usermodel.Sheet;
import  org.apache.poi.ss.usermodel.Workbook;
/**
  *
  * @author 小夜的传说
  * 2014-2-21 上午9:47:17
  * 处理时间格式单元格
  */
public  class  ProTime {
     public  static  void  main(String[] args)  throws  Exception {
         Workbook wb= new  HSSFWorkbook();
         Sheet sheet=wb.createSheet( "第一个Sheet页" );
         Row row=sheet.createRow( 0 );
         row.createCell( 0 ).setCellValue( new  Date()); //第一列添加时间
                        
         CreationHelper creationHelper=wb.getCreationHelper();
         CellStyle cellStyle=wb.createCellStyle(); //设置样式
         cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat( "yyyy-mm-dd hh:mm:ss" ));
         Cell cell=row.createCell( 1 );
         cell.setCellValue( new  Date()); //第2列添加时间
         cell.setCellStyle(cellStyle); //将
                        
         //也可以这样获取时间 日历的方式
         cell=row.createCell( 2 );
         cell.setCellValue(Calendar.getInstance()); //第3列添加时间
         cell.setCellStyle(cellStyle);
                        
                    
         FileOutputStream out= new  FileOutputStream( "d://设置时间单元格.xls" );
         wb.write(out);
         out.close();
         System.out.println( "end" );
     }
}


综上可知,时间日期必须处理之后才可以在Excel中显示    

效果图:

wKiom1MIexKiFkWnAACtDqe6894464.jpg










本文转自 小夜的传说 51CTO博客,原文链接:http://blog.51cto.com/1936625305/1362147,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值