2013-03-11 180 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