Java如何导出Excel表格

版权声明:请勿抄袭 https://blog.csdn.net/qq_41459991/article/details/82501891

Java如何导出Excel表格

  • Markdown和扩展Markdown简洁的语法
  • 代码块高亮

- 效果图效果图界面

代码块

代码块语法遵循标准markdown代码,例如:

//<!—Servlet  界面直接调用就可以了,这里我不会转小数类型-->
    //导出 Excel
    public void getGenerateExcel (HttpServletRequest req, HttpServletResponse resp)
            throws ServletException,IOException {
        // TODO Auto-generated method stub

        String currentPageStr = req.getParameter("curPage");//当前页
        String pageSizeStr = req.getParameter("pageSize");//每一页的数据条数

        String commoName = req.getParameter("cxuserName");//获取界面的用户名称

        int currentPage = 1;
        int pageSize = 10;

        if(currentPageStr != null && Tools.isNum(currentPageStr)){
            currentPage = Integer.parseInt(currentPageStr);
        }
        if(pageSizeStr != null && Tools.isNum(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        int startIndex = (currentPage-1) * pageSize;

        CommodityService commoNameService = new CommodityServiceImpl();
        //List<commoditytb> TabCommodity = commoNameService.findAll(startIndex,pageSize,commoName);

        List<commoditytb> list = new ArrayList<commoditytb>();
        list = commoNameService.findAll(startIndex,pageSize,commoName);

        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("商品表一");
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        /*style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式*/

        HSSFCell cell = row.createCell(0);
        cell.setCellValue("商品名称");
        cell.setCellStyle(style);
        cell = row.createCell(1);
        cell.setCellValue("商品类型");
        cell.setCellStyle(style);
        cell = row.createCell(2);
        cell.setCellValue("销售价格");
        cell.setCellStyle(style);
        cell = row.createCell(3);
        cell.setCellValue("积分");
        cell.setCellStyle(style);
        cell = row.createCell(4);
        cell.setCellValue("库存数量");
        cell.setCellStyle(style);
        cell = row.createCell(5);
        cell.setCellValue("生产许可证");
        cell.setCellStyle(style);
        cell = row.createCell(6);
        cell.setCellValue("厂名");
        cell.setCellStyle(style);
        cell = row.createCell(7);
        cell.setCellValue("厂址");
        cell.setCellStyle(style);
        cell = row.createCell(8);
        cell.setCellValue("厂家联系电话");
        cell.setCellStyle(style);
        cell = row.createCell(9);
        cell.setCellValue("配料表");
        cell.setCellStyle(style);
        cell = row.createCell(10);
        cell.setCellValue("保质期");
        cell.setCellStyle(style);
        cell = row.createCell(11);
        cell.setCellValue("净含量");
        cell.setCellStyle(style);
        cell = row.createCell(12);
        cell.setCellValue("包装方式");
        cell.setCellStyle(style);
        cell = row.createCell(13);
        cell.setCellValue("品牌");
        cell.setCellStyle(style);
        cell = row.createCell(14);
        cell.setCellValue("存储方式");
        cell.setCellStyle(style);
        cell = row.createCell(15);
        cell.setCellValue("下架状态");
        cell.setCellStyle(style);
        cell = row.createCell(16);
        cell.setCellValue("商品编号");
        cell.setCellStyle(style);

        for (int i = 0; i < list.size(); i++)
        {
            row = sheet.createRow((int) i + 1);
            commoditytb stu = (commoditytb)list.get(i);
            // 第五步,创建单元格,并设置值
        /*  String getPoints = ;
            BigDecimal registerFund = new BigDecimal(stu.getSellPrice()); //Decimal类型转换
            */
            //穩
            row.createCell(0).setCellValue(stu.getCommodityName());//商品名称
            row.createCell(1).setCellValue(stu.getCommodityTypeName());//商品类型
            /*row.createCell(2).setCellValue(BigDecimal(stu.getSellPrice()));//销售价格 setSellPrice
            row.createCell(3).setCellValue(BigDecimal(stu.getGetPoints()));//所获积分
*/          row.createCell(4).setCellValue(stu.getStockNumber());//库存数量
            row.createCell(5).setCellValue(stu.getProduceNumber());//生产许可证
            row.createCell(6).setCellValue(stu.getFactoryName());//厂名
            row.createCell(7).setCellValue(stu.getFactoryAdress());//厂址
            row.createCell(8).setCellValue(stu.getFactoryConnection());//厂家联系电话
            row.createCell(9).setCellValue(stu.getChargeMixture());//配料表
            row.createCell(10).setCellValue(stu.getQualityDate());//保质期
            row.createCell(11).setCellValue(stu.getNetContent());//净含量
            row.createCell(12).setCellValue(stu.getPackWay());//包装方式
            row.createCell(13).setCellValue(stu.getBrank());//品牌
            row.createCell(14).setCellValue(stu.getLayInWay());//存储方式
            row.createCell(15).setCellValue(stu.getSoldOutBit());//下架状态
            row.createCell(16).setCellValue(stu.getProductID());//商品编号
        }
        // 第六步,将文件存到指定位置
        try
        {
            Date date = new Date();
            String str = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSSS").format(date); //需要导出带有时间的,请把时间拼接到/杠后面去
            FileOutputStream fout = new FileOutputStream("C:\\Users\\夜\\Desktop\\商品图片/Table.xls");
            wb.write(fout);
            fout.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

脚注

生成一个脚注1.

目录

离线写博客

即使用户在没有网络的情况下,也可以通过本编辑器离线写博客(直接在曾经使用过的浏览器中输入write.blog.csdn.net/mdeditor即可。Markdown编辑器使用浏览器离线存储将内容保存在本地。

用户写博客的过程中,内容实时保存在浏览器缓存中,在用户关闭浏览器或者其它异常情况下,内容不会丢失。用户再次打开浏览器时,会显示上次用户正在编辑的没有发表的内容。

博客发表后,本地缓存将被删除。 

用户可以选择 把正在写的博客保存到服务器草稿箱,即使换浏览器或者清除缓存,内容也不会丢失。

注意:虽然浏览器存储大部分时候都比较可靠,但为了您的数据安全,在联网后,请务必及时发表或者保存到服务器草稿箱

浏览器兼容

  1. 目前,本编辑器对Chrome浏览器支持最为完整。建议大家使用较新版本的Chrome。
  2. IE9以下不支持
  3. IE9,10,11存在以下问题
    1. 不支持离线功能
    2. IE9不支持文件导入导出
    3. IE10不支持拖拽文件导入


  1. 这里是 脚注内容.

猜你喜欢

转载自blog.csdn.net/qq_41459991/article/details/82501891