E004 如何把Word表格數據提取寫入Excel

E004 如何把Word表格數據提取寫入Excel

Hi,How are you doing?

我是職場編碼(CodeVoc)。

在E000中,我們介紹了Node.js、Ruby、Electron等工具下載安裝。

這期,給你演示一下由Electron聯合Ruby製作的小工具。

播放

暫停

進入全屏

退出全屏

00:00

00:00

重播

刷新

試試

藉助Electron官方Demo,我們很容易製作一個工具展示平臺。

E004 如何把Word表格數據提取寫入Excel

點擊“View Demo”會彈出我們的工具界面。

E004 如何把Word表格數據提取寫入Excel

一、項目需求

這個工具的主要目的是為了批量把Word表格數據提取寫入Excel。

它除了可以批量選擇源文件,還選擇目標文件。

點擊“寫入”按鈕,就可以按照我們的思路,批量把表格數據一次性寫入Excel。

E004 如何把Word表格數據提取寫入Excel

二、界面設計

【html】

生成表單容器:

生成佈局標籤:

生成行內標籤:

生成單行輸入框:

生成文件選擇按鈕:

<code><

input

type

=

"file"

id=

"goal_file"

> <

input

type

=

"file"

id=

"source_file"

> /<code>

生成普通按鈕:

【css】

關注四點前白後綠氣泡某詞平臺,搜索“職場編碼”查看源碼。

【javascript】

根據ID,選中source_file按鈕

<code>

var

source_line=

document

.getElementById(

'source_line'

) /<code>

給source_file按鈕,添加"change"事件

<code>

source_line.value

=document.getElementById(

'source_file'

).files[

0

].path /<code>

根據ID,選中goal_file按鈕,

<code>

var

goal_line=

document

.getElementById(

'goal_line'

) /<code>

給goal_file按鈕,添加"change"事件

<code>

goal_file.value

=document.getElementById(

'goal_file'

).files[

0

].path /<code>

execute按鈕添加單擊事件

<code>

execute

.addEventListener(

"click"

,

function

()

{獲取參數

1

,獲取參數

2

,調用}) /<code>

獲取參數1

<code>

var

source_line=document.getElementById(

'source_line'

).

value

/<code>

獲取參數2

<code>

var

goal_line=document.getElementById(

'goal_line'

).

value

/<code>

調用Ruby腳本

<code>

const

{ spawn } =

require

(

'child_process'

)

const

ls = spawn(

'ruby'

, [

'Ruby腳本完整路徑'

,參數

1

,參數

2

]) /<code>

三、邏輯梳理

=> 基礎語法

引用Ruby標準庫

<code>

require

"win32ole"

/<code>

創建雙參數入口方法

<code>

def

Tables_to_excel

(pth_source,pth_goal)

/<code>

接收控制檯傳雙參

<code>

Tables_to_excel

(

ARGV

[0]

,

ARGV

[1]

)/<code>

創建二維數組[100行]

<code>arr=Array.

new

(

100

){[

nil

]} /<code>

使用Dir.glob提取指定格式文件,使用gsub替換文件路徑‘\’為'/'

<code>

pth_source

=File.dirname(pth_source).gsub(

'\\'

,

'/'

)

filename

= Dir.glob(pth_source+

'/*.doc*'

) /<code>

設置雙循環

<code>filename.each{

|n|

(

1

..t).each{

|i|

} } /<code>

=> 對象模型

創建可視化Excel、Word應用

<code>

@eap

=

WIN32OLE

::new(

"excel.application"

);

@eap

.visible=true

@wap

=

WIN32OLE

::new(

"word.application"

);

@wap

.visible=true /<code>

打開Excel工作簿、Word文檔

<code>

ebk

[email protected](pth_goal)

wdc

[email protected](n) /<code>

表格數量計數

<code>

t

= wdc.tables.count /<code>

執行表格數據寫入數組操作(使用.chop去除表格數據前後空白符)

<code>arr[

s

][

0

] = wdc.tables(i).cell(1,2).range.text.chop arr[

s

][

1

] = wdc.tables(i).cell(1,4).range.text.chop arr[

s

][

2

] = wdc.tables(i).cell(2,2).range.text.chop arr[

s

][

3

] = wdc.tables(i).cell(2,4).range.text.chop arr[

s

][

4

] = wdc.tables(i).cell(3,2).range.text.chop /<code>

執行數組寫入單元格區域操作

<code>ebk.worksheets(

1

).range(

"a2"

).resize(s,

5

).

value

=arr/<code>

關注四點前白後綠氣泡某詞平臺,搜索“職場編碼”查看源碼。


分享到:


相關文章: