需求是这样的:要将EXCEL中的数据转为txt和json,由于泰文直接转出会乱码再手动转为utf-8就没用了,所以需要在VBA中直接转变为utf-8,但是由于文本量过大,所以现在运行一次宏要好久,求各位大大帮忙可以优化一下,怎么能变得快一些

代码如下:

Const adTypeBinary = 1

Const adTypeText = 2

Const adSaveCreateNotExist = 1

Const adSaveCreateOverWrite = 2

Sub exportJosn()

Dim s As String

Dim fullName As String

Dim Data1 As String

Dim rng As Range

Dim xLen As Long

Dim yLen As Long

Dim r1 As Long

Dim c1 As Long

fullName = Replace(ThisWorkbook.fullName, ".xlsm", ".json.txt")

fullName = Replace(fullName, "公告", "Attributes")

xLen = Range("a1").CurrentRegion.Columns.Count

yLen = Range("a1").CurrentRegion.Rows.Count

' Open fullName For Output As #1

' Print #1, "{"

tempStr = "{" & VBA.Constants.vbCrLf

For r1 = 2 To yLen

s = ""

For c1 = 1 To xLen

If (Application.IsNumber(Cells(r1, c1).Value)) Then

s = s & Chr(34) & Cells(1, c1).Value & Chr(34) & " : " & Cells(r1, c1).Value

Else

s = s & Chr(34) & Cells(1, c1).Value & Chr(34) & " : " & Chr(34) & Cells(r1, c1).Value & Chr(34)

End If

If c1 < xLen Then

s = s & ", "

End If

Next

If r1 < yLen Then

' Print #1, Chr(34) & Cells(r1, 1).Value & Chr(34) & " : {" & s & "}, "

tempStr = tempStr & Chr(34) & Cells(r1, 1).Value & Chr(34) & " : {" & s & "}, " & VBA.Constants.vbCrLf

Else

' Print #1, Chr(34) & Cells(r1, 1).Value & Chr(34) & " : {" & s & "}"

tempStr = tempStr & Chr(34) & Cells(r1, 1).Value & Chr(34) & " : {" & s & "}" & VBA.Constants.vbCrLf

End If

Next

' Print #1, "}"

tempStr = tempStr & "}" & VBA.Constants.vbCrLf

'  Close #1

Set Stream = CreateObject("adodb.stream")

Stream.Open

Stream.Type = adTypeText

Stream.Charset = "UTF-8"

Stream.writetext tempStr

Stream.flush

Stream.savetofile fullName, adSaveCreateOverWrite

Stream.Close

Set Stream = Nothing

fullName = Replace(ThisWorkbook.fullName, ".xlsm", ".json")

fullName = Replace(fullName, "公告", "服务器用表")

xLen = Range("a1").CurrentRegion.Columns.Count

yLen = Range("a1").CurrentRegion.Rows.Count

'  Open fullName For Output As #1

'  Print #1, "{"

tempStr = "{" & VBA.Constants.vbCrLf

For r1 = 2 To yLen

s = ""

For c1 = 1 To xLen

If (Application.IsNumber(Cells(r1, c1).Value)) Then

s = s & Chr(34) & Cells(1, c1).Value & Chr(34) & " : " & Cells(r1, c1).Value

Else

s = s & Chr(34) & Cells(1, c1).Value & Chr(34) & " : " & Chr(34) & Cells(r1, c1).Value & Chr(34)

End If

If c1 < xLen Then

s = s & ", "

End If

Next

If r1 < yLen Then

' Print #1, Chr(34) & Cells(r1, 1).Value & Chr(34) & " : {" & s & "}, "

tempStr = tempStr & Chr(34) & Cells(r1, 1).Value & Chr(34) & " : {" & s & "}, " & VBA.Constants.vbCrLf

Else

' Print #1, Chr(34) & Cells(r1, 1).Value & Chr(34) & " : {" & s & "}"

tempStr = tempStr & Chr(34) & Cells(r1, 1).Value & Chr(34) & " : {" & s & "}" & VBA.Constants.vbCrLf

End If

Next

'Print #1, "}"

tempStr = tempStr & "}" & VBA.Constants.vbCrLf

' Close #1

Set Stream = CreateObject("adodb.stream")

Stream.Open

Stream.Type = adTypeText

Stream.Charset = "UTF-8"

Stream.writetext tempStr

Stream.flush

Stream.savetofile fullName, adSaveCreateOverWrite

Stream.Close

Set Stream = Nothing

MsgBox ("ok!")

End Sub

adodb执行查询oracle慢,adodb.stream逐行读写过慢,求优化VBA相关推荐

  1. c# 利用ADODB连接ORACLE数据库

    连接字符串 string constr=@  "Driver={Microsoft ODBC for Oracle};Server=test;Uid=test;Pwd=test;&quo ...

  2. python逐行写入文件_python逐行读写txt文件的实例讲解

    实例如下所示: # -*-coding:utf-8-*- import os file_obj = open("test2.txt") all_lines = file_obj.r ...

  3. adodb/adodb.inc.php,php adodb连接不同数据库

    php adodb连接不同数据库的实现代码. include("adodb/adodb.inc.php"); //包含adodb类库文件 $conn = NewADOConnect ...

  4. adodb/adodb.inc.php,php adodb连接不同数据库分享

    代码如下: 代码如下: include("adodb/adodb.inc.php"); //包含adodb类库文件 $conn = NewADOConnection(' 代码如下: ...

  5. Java8的Stream中的Collectors操作求double类型和的坑

    Java8的Stream中的Collectors操作求double类型和的坑 无敌踩坑王的我又双叒叕来了!!!!!! 例子: @Testpublic void testSumDouble() {dou ...

  6. Oracle执行计划filter下多个节点的优化

    Oracle执行计划filter下多个节点的优化 FILTER操作是执行计划中常见的操作,这种操作有两种情况: l 只有一个子节点,那么就是简单过滤操作. l有多个子节点,那么就是类似NESTED L ...

  7. oracle应付创建会计科目,ORACLE创建会计科目出现的系统警告,求牛人指点迷津!该怎么处理...

    当前位置:我的异常网» Oracle管理 » ORACLE创建会计科目出现的系统警告,求牛人指点迷津 ORACLE创建会计科目出现的系统警告,求牛人指点迷津!该怎么处理 www.myexception ...

  8. JAVA使用stream对数据分组后求每组的最大值

    JAVA stream对数据分组后求每组的最大值 具体需求 对industryCode分组,然后通过取出每组的num的最大值 // 实体类 @Data @AllArgsConstructor publ ...

  9. 【Oracle】看懂执行计划之基于规则的优化器(RBO)

    [Oracle]看懂执行计划之基于规则的优化器(RBO) 简介   基于规则的优化器(RBO)是通过编码在 Oracle 数据库中的一系列固定的规则,来决定目标 SQL 的执行计划.Oracle 事先 ...

最新文章

  1. 机器学习中的数学(1)-回归(regression)、梯度下降(gradient descent)
  2. API 类和面向对象简介
  3. iis负载均衡与文件同步[网摘]
  4. C# 线程问题之争用条件
  5. 工具 每分钟_black: 一个不妥协的Python代码格式化工具
  6. NS2相关学习——完成一个新协议(3)
  7. matlab中如何定义数组,matlab中如何定义一个数组为全局变量?
  8. enum ordinal java_Spring 3.x自动将枚举的ordinal值转换为枚举类型
  9. 华硕天选笔记本电脑能上网但浏览器打不开
  10. ANSNP中线安防 安科瑞 时丽花
  11. Markdown 并排显示图片
  12. 怎样成为公司喜欢的人?小技巧
  13. 计算机总线拓扑和环形拓扑,网络拓扑结构总线型环形星型,各自的优缺点是什么..._网络编辑_帮考网...
  14. 简要介绍SIGMOD2021接收的区块链相关论文
  15. 使用bind+dlz+mysql实现动态配置DNS
  16. FAST上网设置服务器未响应,fast路由器不能上网的解决方法
  17. CPU峰值性能计算方法
  18. 这个650万科学大奖来了!今年颁给谁?
  19. PCB关于EMIEMC布线
  20. python matplotlib坐标轴设置的方法

热门文章

  1. Hexo NexT主题添加友链
  2. 修改Windows hosts文件的解决方案
  3. 因变量顺序类别的回归(R语言)
  4. html+js 网页调用网络摄像头 点击拍照 并且......
  5. 分享几个国家标准下载网站
  6. 【解决】Mac终端无法用code .命令打开vscode
  7. 01.java支付之微信支付
  8. 2015年传智播客java_2015年的Java –重大事件
  9. Oracle常用日期计算(年初、月初、本周第一天、当年第几周、当月第几周)
  10. spring找不到application.yml文件