`
u010214413
  • 浏览: 41537 次
社区版块
存档分类
最新评论

使用poi 创建Excel 保存到本地并下载

阅读更多

项目中需要一个提供Excel数据展示下载功能,用于并不是list数据展示,无法通过for来实现。只能通过硬编码实现每一个cell。该Excel数据展示一共用到了四种样式,所以创建了4中style。

最终实现的效果如下:



 
        //查询payment,数据源。用于绑定数据到Excel
        RfPayment rfPayment=rfFinanceManager.findRfPaymentById(shopCode, paymentId);
        XSSFWorkbook workbook = null;
        byte[] bytes=null;
        try{
            workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("sheet名称");
            //设置字体
            XSSFFont font = workbook.createFont();
            //字体大小
            font.setFontHeightInPoints((short) 14);
            //字体
            font.setFontName("宋体");
            //加粗
            font.setBold(true);
            //颜色
            font.setColor(IndexedColors.BLACK.index);

            /** 标题样式 */
            XSSFCellStyle styleTitle = workbook.createCellStyle();
            styleTitle.setFont(font);
            //文字居中
            styleTitle.setAlignment(HorizontalAlignment.CENTER);

            /** 通用样式 */
            XSSFCellStyle styleAuto = workbook.createCellStyle();
            styleAuto.setFont(font);
            //文字左对齐
            styleAuto.setAlignment(HorizontalAlignment.LEFT);

            /** 通用样式 带下划线 */
            XSSFCellStyle styleAutoMedium = workbook.createCellStyle();
            styleAutoMedium.setFont(font);
            //文字左对齐
            styleAutoMedium.setAlignment(HorizontalAlignment.LEFT);
            //粗下划线
            styleAutoMedium.setBorderBottom(BorderStyle.MEDIUM);

            /** 通用样式 边框黑色 */
            XSSFCellStyle styleAutoThin = workbook.createCellStyle();
            styleAutoThin.setFont(font);
            //文字左对齐
            styleAutoThin.setAlignment(HorizontalAlignment.LEFT);
            //粗下划线
            styleAutoThin.setBorderTop(BorderStyle.THIN);
            styleAutoThin.setBorderBottom(BorderStyle.THIN);
            styleAutoThin.setBorderLeft(BorderStyle.THIN);
            styleAutoThin.setBorderRight(BorderStyle.THIN);

            /** 店铺名称样式 */
            XSSFCellStyle styleShop = workbook.createCellStyle();
            styleShop.setFont(font);
            //文字左对齐
            styleShop.setAlignment(HorizontalAlignment.LEFT);
            //背景黄色
            styleShop.setFillForegroundColor(IndexedColors.YELLOW.index);//前景颜色
            styleShop.setFillPattern(FillPatternType.SOLID_FOREGROUND);//填充方式,前色填充

            //第一行第一列为标题
            //第一行第一列到第四列合并
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("网银退款申请书");
            cell.setCellStyle(styleTitle);

            row = sheet.createRow(1);
            cell = row.createCell(0);
            cell.setCellValue("部门:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(1);
            // 店铺名称
            SalesEntity salesEntityParam = new SalesEntity();
            salesEntityParam.setCode(rfPayment.getShopCode());
            List<SalesEntity> salesEntities = salesEntityManager.findListByParam(salesEntityParam);
            cell.setCellValue(!CollectionUtils.isEmpty(salesEntities) ? salesEntities.iterator().next().getName() : rfPayment.getShopCode());
            cell.setCellStyle(styleShop);

            cell = row.createCell(2);
            cell.setCellValue("费用代码:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(3);
            cell.setCellValue("");
            cell.setCellStyle(styleAutoMedium);

            row = sheet.createRow(2);
            cell = row.createCell(0);
            cell.setCellValue("退款单创建日期:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(1);
            cell.setCellValue(rfPayment.getRefundCreateTimeString().substring(0, 10));
            cell.setCellStyle(styleAutoMedium);

            row = sheet.createRow(3);
            cell = row.createCell(0);
            cell.setCellValue("导出日期:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(1);
            cell.setCellValue(DateFormatHelper.dateToStr(new Date(), "yyyy-MM-dd"));
            cell.setCellStyle(styleAutoMedium);

            row = sheet.createRow(4);
            cell = row.createCell(0);
            cell.setCellValue("姓名:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(1);
            cell.setCellValue(userName);//
            cell.setCellStyle(styleAutoMedium);
            cell = row.createCell(2);
            cell.setCellValue("员工代码:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(3);
            cell.setCellValue(jobNumber);//
            cell.setCellStyle(styleAutoMedium);

            row = sheet.createRow(6);
            cell = row.createCell(0);
            cell.setCellValue("付款对象:");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellValue(rfPayment.getPayee());
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(7);
            cell = row.createCell(0);
            cell.setCellValue("退款申请编码:");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellValue(rfPayment.getRfDocNo());
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellValue("付款原由:");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellValue(sysConfigInit.getSysConfigValue(SysConfigConstants.RF_TYPE, rfPayment.getRefundCategory()));
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(8);
            cell = row.createCell(0);
            cell.setCellValue("OMS订单号:");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellValue(rfPayment.getOrderDocNo());
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellValue("相关退货单号:");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellValue(rfPayment.getRefundCategory().equals(Constants.STRING_NUMBER_3)?"":rfPayment.getRoDocNo());
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(9);
            cell = row.createCell(0);
            cell.setCellValue("平台订单编号:");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellValue(rfPayment.getPfDocNo());
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(10);
            cell = row.createCell(1);
            cell.setCellValue("支付金额(元)");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellValue("备注");
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(11);
            cell = row.createCell(0);
            cell.setCellValue("退款单总金额");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellValue(MoneryTransition.number2CNMontrayUnit(rfPayment.getPayTotal()));
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellValue("¥"+rfPayment.getPayTotal().setScale(2, BigDecimal.ROUND_FLOOR).toString());
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellValue("");
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(12);
            cell = row.createCell(0);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(13);
            cell = row.createCell(0);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(14);
            cell = row.createCell(0);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(15);
            cell = row.createCell(0);
            cell.setCellValue("银行转帐(户名)");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellValue(rfPayment.getPayee());
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(16);
            cell = row.createCell(0);
            cell.setCellValue("(银行帐号)");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellValue(rfPayment.getAccount());
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(17);
            cell = row.createCell(0);
            cell.setCellValue("(银行名称)");
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(1);
            cell.setCellValue(rfPayment.getBank());
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(2);
            cell.setCellStyle(styleAutoThin);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoThin);

            row = sheet.createRow(19);
            cell = row.createCell(0);
            cell.setCellValue("申请人:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(1);
            cell.setCellValue(rfPayment.getRefundCreateUser().equals(Long.valueOf("-666"))?"系统创建":omsUserManager.findUserById(Long.valueOf(rfPayment.getRefundCreateUser())).getUserName());
            cell.setCellStyle(styleAutoMedium);
            cell = row.createCell(2);
            cell.setCellValue("日期:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(3);
            cell.setCellValue(rfPayment.getRefundCreateTimeString().substring(0, 10));
            cell.setCellStyle(styleAutoMedium);

            row = sheet.createRow(20);
            cell = row.createCell(0);
            cell.setCellValue("部门主管:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(1);
            cell.setCellStyle(styleAutoMedium);
            cell = row.createCell(2);
            cell.setCellValue("日期:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoMedium);

            row = sheet.createRow(21);
            cell = row.createCell(0);
            cell.setCellValue("财务总监:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(1);
            cell.setCellStyle(styleAutoMedium);
            cell = row.createCell(2);
            cell.setCellValue("日期:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoMedium);

            row = sheet.createRow(22);
            cell = row.createCell(0);
            cell.setCellValue("总经理:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(1);
            cell.setCellStyle(styleAutoMedium);
            cell = row.createCell(2);
            cell.setCellValue("日期:");
            cell.setCellStyle(styleAuto);
            cell = row.createCell(3);
            cell.setCellStyle(styleAutoMedium);

            for (int i = 0; i < 4; i++){
                //宽度自适应
                sheet.autoSizeColumn(i);
            }
           
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            workbook.write(os);
            bytes = os.toByteArray();
            //保存文件到服务器 ,由于不需要保存到本地,所以注释这段了
            //        File file = new File("D://" + System.currentTimeMillis() + ".xlsx");
            //        FileOutputStream out = new FileOutputStream(file);
            //        workbook.write(out);
            //        out.close();
        }catch (EncryptedDocumentException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
            String uuid = UUID.randomUUID().toString();
            logger.error(String.format("导出退款申请书", uuid), e.getMessage());
            throw new RuntimeException(String.format("退款支出明细编辑", uuid), e);
        }finally {
            try{
                workbook.close();
            }catch (IOException e){
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //记录导出次数
        rfPaymentExportSchemesManager.insertExportNum(rfPayment);

        //下载文件
        String fileNmae = System.currentTimeMillis() + ".xlsx";

        HttpHeaders headers = new HttpHeaders();

        headers.setContentDispositionFormData("attachment", new String(fileNmae.getBytes("UTF-8"), "iso-8859-1"));

        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);

        return entity;

   poi 的jar包使用的是。3.12版本

 

  • 大小: 26 KB
0
0
分享到:
评论

相关推荐

    POI导出Excel文件

    Apache POI是一个流行的Java库,用于读取和写入Microsoft Office格式的文件,尤其是Excel(.xlsx和.xls)...理解如何使用POI库可以帮助你更好地集成Excel功能到你的Java项目中,无论是数据导出、报告生成还是数据分析。

    使用POI,实现excel文件导出,图片url导出文件,图片和excel文件导出压缩包

    本文将深入探讨如何使用POI库来实现Excel文件的导出,以及如何将图片URL转换为图片文件并与其他文件一起打包成压缩包。 首先,让我们了解一下Apache POI。POI是Java开发者的开源API,它允许程序创建、修改和显示...

    使用POI将数据写入到EXCEL

    通过阅读提供的示例代码"14、POI将数据写入到EXCEL并保存到本地-示例",你可以更深入地理解这个过程。此代码实例将演示如何将数据结构化地写入Excel,这对于数据分析、报表生成和数据导出场景非常有用。

    Java 利用poi输出Excel文件

    以下是一个简单的示例代码,演示了如何使用Apache POI创建一个Excel文件并写入数据: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io....

    Springboot+Poi实现Excel的导入导出

    1. **导出Excel**:首先,使用MyBatis执行SQL查询获取数据,然后通过POI创建一个Workbook对象,代表Excel工作簿。接着,创建Sheet对象表示工作表,并在其中添加Row和Cell来填充数据。最后,将Workbook写入到本地文件...

    Excel POI 工具类

    2. **写入Excel文件**:创建新的工作簿,添加工作表,设置单元格的值,然后保存到文件。这可以用于生成报告或导出数据。 3. **处理样式和格式**:工具类可能会提供方法来设置单元格的字体、颜色、边框、对齐方式等...

    jxls-poi导出excel示例代码文件

    你可以选择保存到本地或者直接通过HTTP响应发送给用户下载。 具体代码示例可能如下: ```java import org.jxls.JxlsHelper; import org.jxls.common.Context; import org.json.JSONObject; // 加载模板 ...

    java版poi导出excel文件

    本教程将详细讲解如何使用Java和Apache POI库来创建并导出Excel文件。 首先,我们需要在项目中引入Apache POI的相关依赖。在Maven项目中,可以在pom.xml文件中添加以下依赖: ```xml &lt;groupId&gt;org.apache.poi ...

    poi +jsp 实现excel的上传下载的小例子

    开发者可以解压这个文件,然后在本地环境中运行,以理解并学习如何使用POI和JSP实现Excel的上传下载功能。 在实际的应用场景中,这样的功能可能被用于数据导入导出,比如用户可以通过上传Excel文件将大量数据快速...

    java-poi导出导入excel文件到数据库

    4. **保存到本地**:使用`FileOutputStream`将内存中的`Workbook`写入到本地文件。 ```java FileOutputStream fos = new FileOutputStream("output.xlsx"); workbook.write(fos); fos.close(); ``` 通过以上步骤,...

    POI把图片写到excel表中

    7. **添加图片到Sheet**:使用`drawing`的`createPicture`方法,用图片ID和锚点创建图片,并添加到Sheet中。 ```java Picture pict = drawing.createPicture(anchor, pictureIdx); pict.resize(); // 自动调整图片...

    spring mvc easyui-POI导出excel封装源码

    这里我们将深入探讨这些技术,并结合标题和描述,重点讲述如何使用Spring MVC和EasyUI结合POI库来实现Excel数据导出功能。 首先,Spring MVC是Spring框架的一部分,它是一个用于构建Web应用程序的模型-视图-控制器...

    poi导出excel

    - **保存到本地**:使用`FileOutputStream`将Excel文件保存到指定路径。 - **通过HTTP响应输出**:通过设置响应头和使用输出流将Excel文件发送给客户端。 #### 四、代码示例 以下是工具类中的部分关键代码段: ``...

    java poi 读写Excel jar包

    Java POI 是一个开源项目,专门用于处理Microsoft Office格式的文件,尤其是Excel(.xls 和 .xlsx)文件...如果你遵循上述步骤,并结合提供的链接中的详细教程,你将能够熟练地使用Java POI库进行Excel文件的读写操作。

    Java操作Excel之Poi模板导入导出

    6. **导出文件**: 将Workbook写入到OutputStream或File,用户可以选择下载或保存到本地。 在提供的压缩包文件"www.java1234.com"-Java操作Excel之Poi视频教程-第六讲源码中,你可以找到具体的示例代码来帮助理解和...

    poi实现生成excel,文件导出(备份)

    4. 使用Apache POI创建一个新的Excel工作簿,然后在工作簿中添加工作表,为每个数据行创建一行记录,并填充数据。 5. 将生成的Excel文件设置为HTTP响应的附件,指定文件名,例如"backup_data.xlsx",并设置Content-...

    Java使用poi jar包读取以及web下载excle文件

    本教程将详细介绍如何使用POI库在Java中读取Excel文件,并通过Web服务器提供给用户下载。 首先,我们需要在项目中引入Apache POI的jar包。你可以通过Maven或Gradle来管理依赖。如果是Maven,可以在pom.xml文件中...

    java导出excel(写入到excel后再下载)示例

    本示例资源提供了一个RAR压缩包,包含了实现这一功能的源代码,主要讲解了两个核心部分:一是如何将数据写入Excel文件并保存在本地,二是如何在Web环境中将数据库中的数据导出为Excel并允许用户下载。 首先,我们要...

    Java根据自定义模版导出各种需求的Excel(使用POI)

    数据填充完成后,使用`write()`方法将工作簿写入OutputStream,然后可以通过响应流返回给客户端,或者保存到本地文件系统。 9. **异常处理** 在整个过程中,务必做好异常处理,如文件不存在、权限不足、内存溢出...

Global site tag (gtag.js) - Google Analytics