java 生成bdf

这篇博客介绍了如何使用Java从数据库中获取数据并生成DBF文件。首先定义了一个基础类`JBCode`来存储字段信息,然后在前端通过点击按钮触发数据上报。控制层代码中,`dogjsexplore`方法负责处理请求,设置DBF文件标题,构建字段信息,并执行SQL查询获取数据。最后,`makeDbf`方法用于将数据写入到DBF文件中。整个过程涉及数据库操作、文件头定义和数据写入步骤。
摘要由CSDN通过智能技术生成

java从数据库中获取数据生成bdf

1.首先需要一个采集信息字段的基本类

public class JBCode {
 private String ksid;

    public String getKsid() {
        return ksid;
    }

    public void setKsid(String ksid) {
        this.ksid = ksid;
    }
    }

放一个字段用来演示

2.前端页面

 <td align="left">
                <input type="button" class="form-buttun" value="数据上报(DBF)" onclick="exportDBF4()">
            </td>


 function exportDBF4(){
            if(confirm("确定导出系统数据吗?")){
                document.getElementById("form").action="<%=request.getContextPath() %>/bc/dogjsexplore.htm";
                document.getElementById("form").submit();
            }
        }

一个按钮外加一个方法,仅把主体代码复制过来了,其他的东西太多了影响观感就没放

3.控制层代码
dogjsexplore方法

 @RequestMapping("/dogjsexplore")
    private void dogjsexplore( HttpServletResponse res){
			//bdf文件标题名设置为ksname,可自行修改
        String bt=rq1+"年"+ksname1;
        String ksname = bt;

        List<JBCode> jbCodelist = new ArrayList<JBCode>();
        String structName = "";
        //当前状态
        String filename = ksname ;
//下面为bdf标题,如需添加按code1等往下复制即可
        
        JBCode code0 = new JBCode();
        //数据库中的表的,列名称
        code0.setName("ksid");
        //bdf列名称
        code0.setTitle("考试id");
        code0.setLength("20");
        jbCodelist.add(code0);
         int countColname = jbCodelist.size();
        String[] showStrutName = new String [countColname];// def文件头名称数组(显示用)
        String[] strutName = new String[countColname]; // def文件头名称数组(根据该字段获取相应数据)
        byte[] strutType = new byte[countColname];
        int[] strutLength = new int[countColname];
        for (int s = 0; s < countColname; s++) {
            JBCode jbcode = jbCodelist.get(s);
            String title = jbcode.getTitle();


                Pattern pWord = Pattern.compile("[\u4e00-\u9fa5]");// 校验中文的正则表达式
                if (pWord.matcher(title).find() && title.length() > 5) {// 如果是中文,就只要前5个字,原因是dbf文件的列名只支持最多10个字符
                    title = title.substring(0, 5);
                } else {
                    title = title.length() > 10 ? title.substring(0,10) : title ;
                }

            showStrutName[s] = title;
            strutName[s] = jbcode.getName();
            strutType[s] = DBFField.FIELD_TYPE_C;
            strutLength[s] = Integer.valueOf(jbcode.getLength());
        }

//为bdf提供数据的表,其中查询的字段要输code中字段对应
 String sql="select ksid from exam_612_bk";
        System.out.println(sql);
        List list = commonJdbcdao.qeryList(sql,null);
  // 导出dbf
            this.makeDbf(res, filename, strutName, showStrutName, strutType, strutLength, list);


    }

makeDbf方法,bdf数据的写入

public void makeDbf(HttpServletResponse response,
                        String dbfName, String[] strutName,String[] showStrutName, byte[] strutType,
                        int[] strutLength, Collection<Object> dataset) {

        OutputStream fos = null;
        try {
            dbfName = new String(dbfName.getBytes("gbk"), "ISO-8859-1");
         //  response.setContentType("applicationnd.ms-excel");
            response.setHeader("Content-disposition", "attachment; filename="
                    + dbfName + ".dbf");
            fos = response.getOutputStream();
            int fieldCount = strutName.length;
            DBFField[] fields = new DBFField[fieldCount];
            for (int i = 0; i < fieldCount; i++) {
                fields[i] = new DBFField();
                fields[i].setName(showStrutName[i]);
                fields[i].setDataType(strutType[i]);
                // Date类型不能设置字段长度,这里没有处理其它没有字段长度的类型
                if (strutType[i] != DBFField.FIELD_TYPE_D) {
                    fields[i].setFieldLength(strutLength[i]);
                }
            }

            DBFWriter writer = new DBFWriter();
            writer.setFields(fields);
            writer.setCharactersetName("GBK");
            Iterator<Object> it = dataset.iterator();
            Object[][] data = new Object[dataset.size()][fieldCount];

            int index=0;
            while (it.hasNext()) {
                Map t = (Map) it.next();
                for (short i = 0; i < strutName.length; i++) {
                    String fieldName = strutName[i];
                    String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                    try {
                        Object value = t.get(fieldName.toUpperCase());
                        value = (value==null) ? "":value;
                        // 判断值的类型后进行强制类型转换
                        String textValue = "";
                        textValue = value.toString();

                        data[index][i]=textValue;
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        // 清理资源
                    }
                }
                index++;

            }


            for (int i = 0; i < dataset.size(); i++) {
                writer.addRecord(data[i]);
            }
            writer.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值