package sys.sc.opplugin.utils; import com.grapecity.documents.excel.Q; import kd.bos.algo.DataSet; import kd.bos.algo.Row; import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType; import kd.bos.db.DB; import kd.bos.db.DBRoute; import kd.bos.entity.EntityMetadataCache; import kd.bos.orm.query.QCP; import kd.bos.orm.query.QFilter; import kd.bos.orm.query.multi.QFilterFtlikeTransFunction; import kd.bos.servicehelper.BusinessDataServiceHelper; import kd.scm.bid.formplugin.report.biddetailquery.QingFiledContent; import java.io.*; import java.util.ArrayList; import java.util.List; /** * @author cjz * @date 2024/9/18 16:30 * @description:生成dat文件方法封装 */ public class DatFileCreatUtils { //表名 private String nckdentry; //业务编码 private String bizappnum; //生成表的字段list private List selector; //生成文件位置 private String filePath; //分隔符 private String spiltsign; //结尾符 private String endsign; public DatFileCreatUtils(String nckdentry,String bizappnum,List selector,String filePath,String spiltsign,String endsign) { this.nckdentry=nckdentry; this.bizappnum=bizappnum; this.selector=selector; this.filePath=filePath; this.spiltsign=spiltsign; this.endsign=endsign; } public void creatDatFile() { QFilter filtercol=new QFilter("number",QCP.equals,bizappnum); //业务云 DynamicObject bizappcloudDy=BusinessDataServiceHelper .loadSingle("bos_devportal_bizapp","id,bizcloud.number",new QFilter[]{filtercol}); //业务云编码 String bizcloudnum=bizappcloudDy.getString("bizcloud.number"); String sql = " /*dialect*/ select "+String.join(",", selector)+" from "+nckdentry; DataSet dataSet = DB.queryDataSet(this.getClass().getName(),DBRoute.of(bizcloudnum),sql,null); try { File file = new File(filePath); //文件路径不存在则创建文件 if (!file.exists()) { File parentDir = file.getParentFile(); if (parentDir != null && !parentDir.exists()) { parentDir.mkdirs(); } file.createNewFile(); } try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "GBK"))) { while(dataSet.hasNext()) { Row data = dataSet.next(); StringBuilder line = new StringBuilder(); for (String field : selector) { //分割符 line.append(data.get(field) != null ? data.get(field).toString() : "").append(spiltsign); } line.setLength(line.length() - 1); //结尾符 line.append(endsign); writer.write(line.toString()); writer.newLine(); } } } catch (IOException ex) { ex.printStackTrace(); } } }