当前位置: 首页 > 工具软件 > BDF3 > 使用案例 >

java 生成bdf

米裕
2023-12-01

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();
            }
        }

    }
 类似资料: