码迷,mamicode.com
首页 > 编程语言 > 详细

14、java实现poi操作excel,包括读和写日期格式,并且设置字体样式

时间:2016-09-11 13:01:00      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

1、首先大家来看导出的结果

技术分享

下边就是导出的代码了

protected void testExcel() throws IOException{
        String path=getServletContext().getRealPath("/WEB-INF/Template/timeSequence.xlsx");
                System.out.println(path);
                InputStream input=new FileInputStream(path);
                XSSFWorkbook  workBook=new XSSFWorkbook(input);
                XSSFSheet hssfSheet = workBook.getSheet("Sheet1");
                XSSFRow hssfRow=null;
                XSSFCell cell=null;
                XSSFFont font=workBook.createFont();
                font.setFontName("GE Inspira");
                
                OutputStream out=new FileOutputStream(path);
                XSSFSheet hssfSheet2 = workBook.createSheet("Sheet2");
                for (int i = 0; i < hssfSheet.getLastRowNum(); i++) {
                    
                    hssfRow=hssfSheet.getRow(i);
                    XSSFRow row=hssfSheet2.createRow(i);
                    for (int j = 0; j < hssfRow.getLastCellNum(); j++) {
                        CellStyle style=workBook.createCellStyle();
                        style.setFont(font);
                        cell=hssfRow.getCell(j);
                        XSSFCell cellWrite=row.createCell(j);
                        if(cell.getCellType()==cell.CELL_TYPE_STRING){
                            //set value for strings
                            cellWrite.setCellValue(cell.getStringCellValue());
                            cellWrite.setCellStyle(style);
                        }else if(cell.getCellType()==cell.CELL_TYPE_NUMERIC){
                            //set Value for date
                            if(HSSFDateUtil.isCellDateFormatted(cell)){
                                DateFormat format=new SimpleDateFormat();
                                short df=workBook.createDataFormat().getFormat("yyyy-MM-dd"); 
                                style.setDataFormat(df);
                                cellWrite.setCellStyle(style);
                                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd");
                                String readDateValue=dateFormat.format(cell.getDateCellValue());
                                cellWrite.setCellValue(readDateValue);
                            }else{
                                //set value for numeric
                                cellWrite.setCellValue(cell.getNumericCellValue());
                                cellWrite.setCellStyle(style);
                            }
                        }else if(cell.getCellType()==cell.CELL_TYPE_BLANK){
                            //set value for blank
                            cellWrite.setCellValue("");
                            cellWrite.setCellStyle(style);
                        }else{
                            cellWrite.setCellValue(cell.getStringCellValue());
                            cellWrite.setCellStyle(style);
                        }
                    }
                }
                workBook.write(out);
    }

excel模板的存放位置

技术分享

如有非作者本人光顾的十分十分感谢

14、java实现poi操作excel,包括读和写日期格式,并且设置字体样式

标签:

原文地址:http://www.cnblogs.com/weizhen/p/5861442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!