后端代码实现Excel文件生成

引入依赖包

<!--        start 打印文件,文件生成-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <!--        end 打印文件,文件生成-->
 //订单文件生成
    private DownLoadDto ordersXLS(OrdersDto ordersDto) {
   
   
        boolean flag = true;
        String name = null;
        try {
   
   
            //设置输出到本地的excel文件的名字和路径
            name = "F:\\" + ordersDto.getUserName() + "-" + ordersDto.getOrdersNumber() + ".xls";
            FileOutputStream out = new FileOutputStream(name);
            //生成excel文档对象
            HSSFWorkbook workBook = new HSSFWorkbook();
            //创建工作簿
            HSSFSheet mySheet = workBook.createSheet();
            //设置工作簿的名字
            workBook.setSheetName(0, "我的工作簿1");
            //创建第一行,标题行
            int rowNomber = -1;
            HSSFRow myRow = mySheet.createRow(++rowNomber);
            HSSFCellStyle style = workBook.createCellStyle();
            //设置字体样式
            HSSFFont font = workBook.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 10);
            style.setFont(font);

            //设置标题行,每一列的标题
            HSSFCell cell = myRow.createCell((short) 0);
            cell.setCellStyle(style);
            cell.setCellValue("菜名");
            cell = myRow.createCell((short) 1);
            cell.setCellStyle(style);
            cell.setCellValue("单价");
            cell = myRow.createCell((short) 2);
            cell.setCellStyle(style);
            cell.setCellValue("数量");
            cell = myRow.createCell((short) 3);
            cell.setCellStyle(style);
            cell.setCellValue("订单编号");
            cell = myRow.createCell((short) 4);
            cell.setCellStyle(style);
            cell.setCellValue("用户id");
            cell = myRow.createCell((short) 5);
            cell.setCellStyle(style);
            cell.setCellValue("用户姓名");
            cell = myRow.createCell((short) 6);
            cell.setCellStyle(style);
            cell.setCellValue("订单总金额");
            cell = myRow.createCell((short) 7);
            cell.setCellStyle(style);
            cell.setCellValue("评价信息");
            cell = myRow.createCell((short) 8);
            cell.setCellStyle(style);
            cell.setCellValue("联系电话");
            cell = myRow.createCell((short) 9);
            cell.setCellStyle(style);
            cell.setCellValue("备注");
            cell = myRow.createCell((short) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值