Java 导出生成Excel 报表 有表头,表内容,表统计功能,完美。。。。。

本文介绍了一种将库存数据从数据库导出到Excel的方法。通过Java编程实现,包括设置Excel表格格式、从数据库查询数据并填充到Excel表格中等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    /******************************* 9 Start  reportinventoryToExcel***************************************/

    public String reportinventoryToExcel()

    {

        // TODO Auto-generated method stub
        DataBase db = new DataBase();
        ResultSet rs = null;
        String strSQL = null;
        String path = new String();
        // 获取时间
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddhhmmss");
        Date d = new Date();
        String number = sf.format(d);
        String filename = number + ".xls";
        System.out.println(number);

        path = filename;
        File file = new File(path);

        try {
            jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(file);
            // 创建表"研究院"
            jxl.write.WritableSheet ws = wwb.createSheet("InboundDetail", 0);

            // 设置默认字体
            jxl.write.WritableFont wfc = new jxl.write.WritableFont(
                    WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,
                    UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
            // 设置title字体
            jxl.write.WritableFont titleFont = new WritableFont(
                    WritableFont.ARIAL, 14, WritableFont.NO_BOLD, false,
                    UnderlineStyle.SINGLE, jxl.format.Colour.BLACK);
            // 设置时间字体
            jxl.write.WritableFont datewfc = new jxl.write.WritableFont(
                    WritableFont.ARIAL, 10, WritableFont.BOLD, false,
                    UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
            // 设置落款字体
            jxl.write.WritableFont endFont = new WritableFont(
                    WritableFont.ARIAL, 10, WritableFont.BOLD);

            // ***************************Excel表格字体格式Start***********************//
            int charTitle = 10;// 标题字体大小
            int charNormal = 10;// 标题字体大小
            // 添加带有字型Formatting的对象

            // *******************************表头格式部分
            jxl.write.WritableFont titleHead = new jxl.write.WritableFont(
                    WritableFont.createFont("宋体"), charTitle, WritableFont.BOLD);

            jxl.write.WritableCellFormat titleHeadFormat = new jxl.write.WritableCellFormat(
                    titleHead);
            titleHeadFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
            titleHeadFormat.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
            titleHeadFormat.setAlignment(Alignment.CENTRE); // 水平对齐
            titleHeadFormat.setWrap(true); // 是否换行

            // *******************************表体数据内容格式部分

            jxl.write.WritableFont DataFontCotent = new jxl.write.WritableFont(
                    WritableFont.createFont("ARIA"), 9);

            jxl.write.WritableCellFormat DataFontCotentFormat = new jxl.write.WritableCellFormat(
                    DataFontCotent);
            DataFontCotentFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
            DataFontCotentFormat.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
            DataFontCotentFormat.setAlignment(Alignment.LEFT); // 水平对齐
            DataFontCotentFormat.setWrap(true); // 是否换行

            // 用于带有formatting的Number对象
            WritableFont nf = new WritableFont(
                    WritableFont.createFont("Times New Roman"), charNormal);
            jxl.write.NumberFormat nf1 = new jxl.write.NumberFormat("#.##");
            jxl.write.WritableCellFormat wcfN = new jxl.write.WritableCellFormat(
                    nf1);
            wcfN.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
            wcfN.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
            wcfN.setAlignment(Alignment.CENTRE);// 水平对齐
            wcfN.setWrap(true); // 是否换行

            // ***************************Excel表格字体格式End***********************//

            // 设置左右上下边距
            ws.getSettings().setLeftMargin(0.6);
            ws.getSettings().setRightMargin(0.6);
            ws.getSettings().setTopMargin(0.3);
            ws.getSettings().setBottomMargin(0.3);

            WritableCellFormat detFormat = new WritableCellFormat(wfc);
            // 用于Number的格式

            WritableCellFormat firstFormat = new WritableCellFormat();
            WritableCellFormat dataFormat = new WritableCellFormat();
            WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
            WritableCellFormat dateFormat = new WritableCellFormat(datewfc);// 时间格式
            WritableCellFormat endFormat = new WritableCellFormat(endFont);// 落款样式
            // WritableCellFormat backFormat = new WritableCellFormat(); // 背景色

            // 标题行具体格式
            firstFormat.setAlignment(jxl.format.Alignment.CENTRE);
            firstFormat.setBackground(Colour.GRAY_25); // 背静色
            firstFormat.setBorder(jxl.format.Border.ALL,
                    jxl.format.BorderLineStyle.DOUBLE,
                    jxl.format.Colour.GRAY_50);
            // 内容行具体格式
            dataFormat.setBorder(jxl.format.Border.ALL,
                    jxl.format.BorderLineStyle.DOUBLE,
                    jxl.format.Colour.GRAY_50);
            dataFormat.setBackground(Colour.LIGHT_GREEN); // 背静色
            dataFormat.setAlignment(Alignment.CENTRE); // 设置对齐方式
            dataFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 设置对齐方式

            // **** Excel表头部分 ****
            ws.addCell(new Label(0, 0, "Inbound Ref"));
            ws.setColumnView(0, 15, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(1, 0, "Location"));
            ws.setColumnView(1, 25, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(2, 0, "Pallet"));
            ws.setColumnView(2, 15, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(3, 0, "CARTON"));
            ws.setColumnView(3, 18, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(4, 0, "China PO"));
            ws.setColumnView(4, 18, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(5, 0, "Kaga PO"));
            ws.setColumnView(5, 18, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(6, 0, "PRODUCTION NAME"));
            ws.setColumnView(6, 25, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(7, 0, "LOT NO"));
            ws.setColumnView(7, 18, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(8, 0, "JP PARTNO"));
            ws.setColumnView(8, 25, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(9, 0, "CH NPARTNO"));
            ws.setColumnView(9, 20, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(10, 0, "ORIGIN"));
            ws.setColumnView(10, 8, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(11, 0, "QTY"));
            ws.setColumnView(11, 8, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(12, 0, "INVGW"));
            ws.setColumnView(12, 8, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(13, 0, "UNIT PRICE"));
            ws.setColumnView(13, 20, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(14, 0, "Length"));
            ws.setColumnView(14, 8, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(15, 0, "Width"));
            ws.setColumnView(15, 8, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(16, 0, "Height"));
            ws.setColumnView(16, 8, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(17, 0, "CBM"));
            ws.setColumnView(17, 10, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(18, 0, "DESCRIPTION"));
            ws.setColumnView(18, 40, titleHeadFormat);// 设置宽度

            ws.addCell(new Label(19, 0, "REMARKS"));
            ws.setColumnView(19, 40, titleHeadFormat);// 设置宽度

            // **** End Excel表头部分 ****

            // 数据库连接

            strSQL = "select ref_no,inbound_id,location,pallet_no,inventory_no,lot_no,partno_jp,partno_cn,partno_kaga,prod_name,part_desc1,part_desc2,part_desc3,pono_china,pono_kaga,qty_order,qty_recvd,qty_upcoming,inv_gw,ctn_gw,dime_l,dime_w,dime_h,carton,curr,price_orig,exch_rate,price_base,origin,maker,rmks,createdon,createdby,partsn from kg_inbounditem";
            rs = db.executeQuery(strSQL);
            int y = 1;
            int sum = 0;
            while (rs.next()) {

                // 循环从数据表表中写入数据到EXCEL单元格中
                jxl.write.Label labelc0 = new jxl.write.Label(0, y,
                        rs.getString("ref_no"), DataFontCotentFormat);
                ws.addCell(labelc0);

                jxl.write.Label labelc1 = new jxl.write.Label(1, y,
                        rs.getString("location"), DataFontCotentFormat);
                ws.addCell(labelc1);

                jxl.write.Label labelc2 = new jxl.write.Label(2, y,
                        rs.getString("pallet_no"), DataFontCotentFormat);
                ws.addCell(labelc2);

                jxl.write.Label labelc3 = new jxl.write.Label(3, y,
                        rs.getString("carton"), DataFontCotentFormat);
                ws.addCell(labelc3);

                jxl.write.Label labelc4 = new jxl.write.Label(4, y,
                        rs.getString("pono_china"), DataFontCotentFormat);
                ws.addCell(labelc4);

                jxl.write.Label labelc5 = new jxl.write.Label(5, y,
                        rs.getString("partno_kaga"), DataFontCotentFormat);
                ws.addCell(labelc5);

                jxl.write.Label labelc6 = new jxl.write.Label(6, y,
                        rs.getString("prod_name"), DataFontCotentFormat);
                ws.addCell(labelc6);

                jxl.write.Label lot_no = new jxl.write.Label(7, y,
                        rs.getString("lot_no"), DataFontCotentFormat);
                ws.addCell(lot_no);

                jxl.write.Label partno_jp = new jxl.write.Label(8, y,
                        rs.getString("partno_jp"), DataFontCotentFormat);
                ws.addCell(partno_jp);

                jxl.write.Label partno_cn = new jxl.write.Label(9, y,
                        rs.getString("partno_cn"), DataFontCotentFormat);
                ws.addCell(partno_cn);

                jxl.write.Label origin = new jxl.write.Label(10, y,
                        rs.getString("origin"), DataFontCotentFormat);
                ws.addCell(origin);
                jxl.write.Label qty_order = new jxl.write.Label(11, y,
                        rs.getString("qty_order"), DataFontCotentFormat);
                ws.addCell(qty_order);

                jxl.write.Label inv_gw = new jxl.write.Label(12, y,
                        rs.getString("inv_gw"), DataFontCotentFormat);
                ws.addCell(inv_gw);

                jxl.write.Label price_base = new jxl.write.Label(13, y,
                        rs.getString("price_base"), DataFontCotentFormat);
                ws.addCell(price_base);

                jxl.write.Label dime_l = new jxl.write.Label(14, y,
                        rs.getString("dime_l"), DataFontCotentFormat);
                ws.addCell(dime_l);

                jxl.write.Label dime_w = new jxl.write.Label(15, y,
                        rs.getString("dime_w"), DataFontCotentFormat);
                ws.addCell(dime_w);

                jxl.write.Label dime_h = new jxl.write.Label(16, y,
                        rs.getString("dime_h"), DataFontCotentFormat);
                ws.addCell(dime_h);

                jxl.write.Label cbm = new jxl.write.Label(17, y,
                        rs.getString("dime_h"), DataFontCotentFormat);
                ws.addCell(cbm);

                jxl.write.Label part_desc1 = new jxl.write.Label(18, y,
                        rs.getString("part_desc1"), DataFontCotentFormat);
                ws.addCell(part_desc1);

                jxl.write.Label rmks = new jxl.write.Label(19, y,
                        rs.getString("rmks"), DataFontCotentFormat);
                ws.addCell(rmks);

                sum = sum;
                y++;

            }
            // 统计
            jxl.write.Label labelsum = new jxl.write.Label(17, y, "统计",
                    endFormat);
            ws.addCell(labelsum);
            int a=5000;
            jxl.write.Number f = new jxl.write.Number(18, y,a,DataFontCotentFormat );
            ws.addCell(f);
            
            jxl.write.Label labelsum1 = new jxl.write.Label(15, y, "合计",
                    endFormat);
            ws.addCell(labelsum1);
            int a1=5000;
            jxl.write.Number f1 = new jxl.write.Number(16, y,a,DataFontCotentFormat );
            ws.addCell(f1);
            
            wwb.write();
            wwb.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "reportinventoryToExcel";

    }

    /******************************* 9 End ***************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值