我有表用戶數據導出到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
老實說,我知道逆方式意味着從Excel傳輸數據到Mnesia的,但我沒有」 t找到解決方案將數據從mnesia轉移到excel – 2013-03-11 10:37:06
如果無法導出數據從mnesia到excel,我想知道是否有可能從mnesia導出數據到sql,如果有可能後,從mnesia導出數據到SQL我可以導出dat從sql到excel – 2013-03-11 11:16:10
如果你可以將它導出到文本文件,你可以使用[XML Excel標準](http://en.wikipedia.org/wiki/Microsoft_Office_2003_XML_formats)來創建Excel工作表。 – 2013-03-11 11:26:58