POI导出excel日期格式
參考帖子:
【1】http://www.ithao123.cn/content-2028409.html
【2】http://javacrazyer.iteye.com/blog/894850
再讀本篇文章之前,請先看我的前一篇文章,前一篇文章中有重點講到POI設置EXCEL單元格格式為文本格式,剩下的設置小數、百分比、貨幣、日期、科學計數法和中文大寫這些將在下面一一寫出
以下將要介紹的每一種都會用到這三行中的變量
?
?? ? ? ? ? ?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);
?
第二種:保留兩位小數格式
?? ? ? ? ???cell.setCellValue(1.2);
?? ? ? ? ? ?HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
?? ? ? ? ? ?cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
?? ? ? ? ? ?cell.setCellStyle(cellStyle);
?
這里與上面有所不同,用的是HSSFDataFormat.getBuiltinFormat()方法,之所以用這個,是因為0.00是Excel內嵌的格式,完整的Excel內嵌格式列表大家可以看這個窗口中的自定義列表:
?這里就不一一列出了
?
第三種:貨幣格式
?
?? ? ? ? ? ?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);
?
第六種:科學計數法格式
?
?? ? ? ? ? ?cell.setCellValue(20000);
?? ? ? ? ? ?HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
?? ? ? ? ? ?cellStyle.setDataFormat( HSSFDataFormat.getBuiltinFormat("0.00E+00"));
?? ? ? ? ? ?cell.setCellStyle(cellStyle);
此種情況也與第二種情況一樣
from:?http://www.cnblogs.com/abc8023/p/5843230.html
總結
以上是生活随笔為你收集整理的POI导出excel日期格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VS2013新特性
- 下一篇: 让你用 Chrome 上网快到想哭:Vi