資料格式轉置

2022-07-20 11:36:18 字數 2396 閱讀 6795

方案一:資料少時省事

private sub customtransform1()

dim wb as workbook, sht as worksheet

dim newsht as worksheet, dic as object

dim endrow as long, irow

set dic = createobject("scripting.dictionary")

set sht = wb.worksheets("原始資料")

'新建一張工作表,若之前已經存在同名工作表,直接刪除

set newsht = wb.worksheets.add(after:=wb.worksheets(wb.worksheets.count))

on error resume next

wb.worksheets("轉置結果").delete

on error goto 0

newsht.name = "轉置結果"

newsht.cells.clearcontents

with sht

endrow = .cells(.cells.rows.count, 1).end(xlup).row

for i = 1 to endrow

key = .cells(i, 1).value

'當a列的某個資訊第一次出現時,為它編排乙個序號,同時作為轉置後的行號

if dic.exists(key) = false then

dic(key) = dic.count + 1

end if

irow = dic(key) '輸出的行號

newsht.cells(irow, "a").value = key

newsht.cells(irow, "iv").end(xltoleft).offset(0, 1).value = .cells(i, 2).value

next i

end with

'釋放物件

set dic = nothing: set wb = nothing

set sht = nothing: set newsht = nothing

end sub

方案二:資料多時效率相對高點

private sub customtransform2()

dim wb as workbook, sht as worksheet

dim newsht as worksheet, dic as object

dim arr(), ar as variant

dim endrow as long, endcol as long

set dic = createobject("scripting.dictionary")

set sht = wb.worksheets("原始資料")

'新建一張工作表,若之前已經存在同名工作表,直接刪除

set newsht = wb.worksheets.add(after:=wb.worksheets(wb.worksheets.count))

on error resume next

wb.worksheets("轉置結果").delete

on error goto 0

newsht.name = "轉置結果"

newsht.cells.clearcontents

with sht

endrow = .cells(.cells.rows.count, 1).end(xlup).row

set rng = .range("a1:b" & endrow)

ar = rng.value

r = 0

redim arr(1 to endrow, 1 to 20) '構造二維陣列

for i = lbound(ar) to ubound(ar)

key = cstr(ar(i, 1))

if dic.exists(key) = false then

dic(key) = 1

else

dic(key) = dic(key) + 1

end if

r = dic.count: c = dic(key)

arr(r, 1) = r: arr(r, c + 1) = ar(i, 2)

next i

end with

'快速輸出結果

newsht.range("a1").resize(ubound(arr), ubound(arr, 2)).value = arr

'釋放物件

set dic = nothing: set wb = nothing

set sht = nothing: set newsht = nothing

end sub

EasyUI List轉tree資料格式

using system using system.collections using system.collections.generic using system.linq using system.linq.expressions using system.reflection namespa...

Json資料格式

在web 系統開發中,經常會碰到客戶端和伺服器端互動的問題,比如說客戶端傳送乙個 ajax 請求,然後在伺服器端進行計算,計算後返回結果,客戶端接收到這個響應結果並對它進行處理。那麼這個結果以一種什麼資料結構返回,客戶端才能比較容易和較好的處理呢?通過幾個專案的實踐,我發現 json 格式的資料是一...

JSON資料格式

下面這段文字,摘錄自留作備忘 21世紀初,douglas crockford尋找一種簡便的資料交換格式,能夠在伺服器之間交換資料。當時通用的資料交換語言是xml,但是douglas crockford覺得xml的生成和解析都太麻煩,所以他提出了一種簡化格式,也就是json。json的規格非常簡單,只...