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

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模板的存放位置

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

转载于:https://www.cnblogs.com/weizhen/p/5861442.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值