2013-03-11 179 views
-2

我有表用戶數據導出到Excel

-record(person, {id, firstname, lastname}). 

此表包含此值:

1 francoi  mocci  
    2 test  tes 

我的目標是我怎麼可以從函數mnesia導出這些數據到excel

我知道反向的意思是將數據從excel傳輸到mnesia

解決方案在這個cas e是交談的高強csv.file然後使用此種代碼來解析csv文件:

%%% --- csv parser in Erlang. ------ 
%%% To help process large csv files without loading them into 
%%% memory. Similar to the xml parsing technique of SAX 

-module(csv). 
-compile(export_all). 

parse(FilePath,ForEachLine,Opaque)-> 
    case file:open(FilePath,[read]) of 
     {_,S} -> 
      start_parsing(S,ForEachLine,Opaque); 
     Error -> Error 
    end. 

start_parsing(S,ForEachLine,Opaque)-> 
    Line = io:get_line(S,''), 
    case Line of 
     eof -> {ok,Opaque}; 
     "\n" -> start_parsing(S,ForEachLine,Opaque); 
     "\r\n" -> start_parsing(S,ForEachLine,Opaque); 
     _ -> 
      NewOpaque = ForEachLine(scanner(clean(clean(Line,10),13)),Opaque), 
      start_parsing(S,ForEachLine,NewOpaque) 
    end. 

scan(InitString,Char,[Head|Buffer]) when Head == Char -> 
    {lists:reverse(InitString),Buffer}; 
scan(InitString,Char,[Head|Buffer]) when Head =/= Char -> 
    scan([Head|InitString],Char,Buffer); 
scan(X,_,Buffer) when Buffer == [] -> {done,lists:reverse(X)}. 
scanner(Text)-> lists:reverse(traverse_text(Text,[])). 

%%traverse_text(Text,Buff)-> 
    %% case scan("",$,,Text) of 
    %% {done,SomeText}-> [SomeText|Buff]; 
%%  {Value,Rem}-> traverse_text(Rem,[Value|Buff]) 
    %% end. 


traverse_text(Text,Buff)-> 
    case scan("",$;,Text) of 
     {done,SomeText}-> [SomeText|Buff]; 
     {Value,Rem}-> traverse_text(Rem,[Value|Buff]) 
    end. 


clean(Text,Char)-> 
    string:strip(string:strip(Text,right,Char),left,Char). 

,這是插入來自csv文件數據給Mnesia的函數的示例:

test()-> 

    ForEachLine = fun(Line,Buffer)-> 
    [Id, Firstname, Lastname] = Line, 

            %% here insert each line to the table mnesia 

            Buffer end, 


InitialBuffer = [], 




csv:parse("/home/test/Desktop/testt.csv",ForEachLine,InitialBuffer). 

這個例子沒有問題

我嘗試使用此代碼:

test()-> 
    F = fun(T) -> mensia:foldl(fun(X,Acc) -> [X|Acc] end, [],T), 
{atomic,L} = mnesia:transaction(F), 
file:write_file("filename.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) || 
       #person{id = F1,firstname = F2,lastname = F3} <- L]). 

,但我有個是錯誤:

syntax error before : '.' 

這個錯誤與該行:

#person{id = F1,firstname = F2,lastname = F3} <- L]). 

我試圖糾正我的代碼:

test()-> 
    F = fun(T) -> mensia:foldl(fun(X,Acc) -> [X|Acc] end, [],T), 
{atomic,L} = mnesia:transaction(F), 
file:write_file("filename.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) || 
       #person{id = F1,firstname = F2,lastname = F3} <- L])end. 

但我現在這個錯誤:

variable 'F' is unbound 

這個錯誤與該行:

{atomic,L} = mnesia:transaction(F), 

我解決了這個問題:

test()-> 
     F = fun(T) -> mensia:foldl(fun(X,Acc) -> [X|Acc] end, [],T)end, 
{atomic,L} = mnesia:transaction(F), 
file:write_file("filename.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) || 
       #person{id = F1,firstname = F2,lastname = F3} <- L]). 

但是當我運行我的功能我有這樣的錯誤:

** exception error: no match of right hand side value {aborted,{{badarity,{#Fun<model.20.69991685>,[]}}, 
                   [{mnesia_tm,apply_fun,3}, 
                   {mnesia_tm,execute_transaction,5}, 
                   {model,test,0}, 
                   {erl_eval,do_apply,5}, 
                   {shell,exprs,6}, 
                   {shell,eval_exprs,6}, 
                   {shell,eval_loop,3}]}} 
    in function model:test/0 

我試試這個代碼:

test()-> 
     F = fun() -> mensia:foldl(fun(X,Acc) -> [X|Acc] end, [],person)end, 
{atomic,L} = mnesia:transaction(F), 
file:write_file("filename.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) || 
       #person{id = F1,firstname = F2,lastname = F3} <- L]). 

,但我也有這個錯誤:

** exception error: no match of right hand side value {aborted,{undef,[{mensia,foldl, 
                       [#Fun<model.21.662230>,[],person]}, 
                     {mnesia_tm,apply_fun,3}, 
                     {mnesia_tm,execute_transaction,5}, 
                     {model,test,0}, 
                     {erl_eval,do_apply,5}, 
                     {shell,exprs,6}, 
                     {shell,eval_exprs,6}, 
                     {shell,eval_loop,3}]}} 
    in function model:test/0 
+0

老實說,我知道逆方式意味着從Excel傳輸數據到Mnesia的,但我沒有」 t找到解決方案將數據從mnesia轉移到excel – 2013-03-11 10:37:06

+0

如果無法導出數據從mnesia到excel,我想知道是否有可能從mnesia導出數據到sql,如果有可能後,從mnesia導出數據到SQL我可以導出dat從sql到excel – 2013-03-11 11:16:10

+1

如果你可以將它導出到文本文件,你可以使用[XML Excel標準](http://en.wikipedia.org/wiki/Microsoft_Office_2003_XML_formats)來創建Excel工作表。 – 2013-03-11 11:26:58

回答

1

可以使用與foldl函數創建一個列表,然後寫這個列表保存到文件,使用任何字符作爲分隔符(空格,逗號標籤。 ..取決於你的記錄內容),最後用Excel讀取文本文件,你將會看到一個彈出菜單,幫助你控制excel解釋數據的方式。 我認爲使用中間列表會更好,因爲直接寫入文件對於數據庫事務來說可能很長。

編輯:對不起,我沒有測試線......它現在應該工作。

... 
F = fun(T) -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end, 
{atomic,L} = mnesia:transaction(F(mnesia_table)), 
file:write_file("filename.txt",[io_lib:format("~p\t~p~n",[F1,F2]) || 
       #table_record{field1 = F1,field2 = F2} <- L]), 
... 

enter image description here enter image description here enter image description here enter image description here

+0

感謝您的回覆,但我有一個與我的函數的語法有關的錯誤,我已更新我的問題 – 2013-03-11 13:19:40

+0

我沒有設置mnesia數據庫,所以我不能真正測試我發佈的代碼。我做了一些修改,現在應該可以工作。 – Pascal 2013-03-11 15:45:28

+0

謝謝你的回覆,但是當我測試我的函數時,我有這個錯誤:Erlang R13B03(erts-5.7.4)[source] [rq:1] [async-threads:0] [hipe] [kernel-poll: false] Eshell V5.7.4(放棄使用^ G) 1> model:test()。 **例外錯誤:未定義函數mensia:foldl/3 in函數模型:test/0 2> – 2013-03-11 16:18:14

1

您在第一行忘記end

F = fun(T) -> mensia:foldl(fun(X,Acc) -> [X|Acc] end, [],T) end, 
+0

謝謝,但我現在有這個錯誤:變量'F'沒有綁定,我已更新我的代碼 – 2013-03-11 14:12:21

+0

重新檢查我寫的你。在第一行「結束」!不在最後一個! – 2013-03-11 14:27:26

+0

對不起,謝謝 – 2013-03-11 14:32:43