ansi码文本文件转换成utf-8文本文件

时间:2023-01-06 13:02:20


STRING ls_file_name,ls_file_name_new
String ls_file,ls_file_new
String ls_parth_local = 'E:\鑫鑫请货数据\请货库存表\'
INTEGER li_fileNum
LONG ll_column,i,ll_status,ll_loop
DATETIME ldt_sysdate
STRING ls_columnname,ls_value,ls_header,ls_detail
STRING ls_goods_opcode,ls_goods_name,ls_goods_desc,ls_lot_no,ls_expire_date,ls_produce_date,ls_package_num,ls_onavailable
STRING ls_sysdate,ls_party_owner,ls_price1,ls_price2,ls_price3

ldt_sysdate  = OF_GET_SYSTEMDATE()

ls_file_name = '100_请货库存表_'+ STRING(ldt_sysdate,"YYYYMMDDHHMMSS") + ".TXT"
ls_file      = ls_parth_local + ls_file_name
ls_file_new  = is_parth + ls_file_name
ll_column    = LONG(dw_1.OBJECT.datawindow.column.count)

FOR i = 1 TO ll_column
 ls_columnname = dw_1.DESCRIBE( "#"+STRING(i)+".name")+"_t"
 ls_value      = dw_1.DESCRIBE(ls_columnname +".text")
 IF i = 1 THEN
  ls_header = ls_value
 ELSE
  ls_header = ls_header + "|" + ls_value
 END IF
NEXT
ll_status = FILEOPEN(ls_file,LineMode!,WRITE!,LockWrite!,REPLACE!)
FILEWRITE(ll_status,ls_header)
FILECLOSE(ll_status)
FOR ll_loop = 1 TO dw_1.ROWCOUNT()
 ls_goods_opcode = dw_1.OBJECT.goods_opcode[ll_loop]
 ls_goods_name   = dw_1.OBJECT.goods_name[ll_loop]
 ls_goods_desc   = dw_1.OBJECT.goods_desc[ll_loop]
 ls_lot_no       = dw_1.OBJECT.lot_no[ll_loop]
 ls_expire_date  = STRING(dw_1.OBJECT.expire_date[ll_loop],'YYYY-MM-DD')
 ls_produce_date = STRING(dw_1.OBJECT.produce_date[ll_loop],'YYYY-MM-DD')
 ls_package_num  = STRING(dw_1.OBJECT.package_num[ll_loop])
 ls_onavailable  = STRING(dw_1.OBJECT.onavailable[ll_loop])
 ls_sysdate      = STRING(dw_1.OBJECT.sysdate[ll_loop],'YYYY-MM-DD HH:MM:SS')
 ls_party_owner  = STRING(dw_1.OBJECT.party_owner[ll_loop])
 ls_price1       = STRING(dw_1.OBJECT.price1[ll_loop])
 ls_price2       = STRING(dw_1.OBJECT.price2[ll_loop])
 ls_price3       = STRING(dw_1.OBJECT.price3[ll_loop])
 
 if isnull(ls_goods_opcode) then ls_goods_opcode = ''
 if isnull(ls_goods_name) then ls_goods_name = ''
 if isnull(ls_goods_desc) then ls_goods_desc = ''
 if isnull(ls_lot_no) then ls_lot_no = ''
 if isnull(ls_expire_date) then ls_expire_date = ''
 if isnull(ls_produce_date) then ls_produce_date = ''
 if isnull(ls_package_num) then ls_package_num = ''
 if isnull(ls_onavailable) then ls_onavailable = ''
 if isnull(ls_sysdate) then ls_sysdate = ''
 if isnull(ls_party_owner) then ls_party_owner = ''
 if isnull(ls_price1) then ls_price1 = ''
 if isnull(ls_price2) then ls_price2 = ''
 if isnull(ls_price3) then ls_price3 = ''
  
 ls_detail = ls_goods_opcode + '|' + ls_goods_name + '|' + ls_goods_desc + '|' + ls_lot_no  + '|' + ls_expire_date  + '|' + &
  ls_produce_date + '|' + ls_package_num + '|' + ls_onavailable + '|' + ls_sysdate + '|' + ls_party_owner + '|' +  &
  ls_price1 + '|' + ls_price2 + '|' +ls_price3
 ll_status = FILEOPEN(ls_file,LineMode!,WRITE!,LockWrite!,Append!)
 FILEWRITE(ll_status,ls_detail)
 FILECLOSE(ll_status)
NEXT
//================================================================================
integer   li_rtn
string ls_str
OLEObject   ole_ADOStream //定义流

ole_ADOStream   =   CREATE   OLEObject //创建流

li_rtn   =   ole_ADOStream.ConnectToNewObject( "ADODB.Stream") //连接流
if li_rtn <> 0 then
    messagebox('提示', '无法连接ADODB.Stream')
    return
end if
//初始化流
ole_ADOStream.Mode   = 3   //具有读/写权限
ole_ADOStream.Type   =   2   //二进制数据Binary
ole_ADOStream.charset = 'GB2312'

ole_ADOStream.Open() //打开流
ole_ADOStream.LoadFromFile(ls_file) //载入文本文件
ls_str = ole_ADOStream.readtext()//读取流文本
//重新初始化流
ole_ADOStream.Position   =   0  //Position为0才能设置Charset 和 Type
ole_ADOStream.Type   =   2   //文本数据
ole_ADOStream.Charset   =   "UTF-8"

ole_ADOStream.writetext(ls_str)//写入流文本(关键步骤,不然中文会出现乱码)
ole_ADOStream.SaveToFile(ls_file,2) //保存为文本文件
ole_ADOStream.flush()//清空缓冲区数据
ole_ADOStream.Close() //关闭流
destroy   ole_ADOStream //销毁流
//================================================================================
Messagebox('提示','导出文件成功,文件路径为' + ls_file)