我有存储在Excel文件中的数据
此文件包含两列:名称和姓氏
并包含几行
我的目标是在我的数据库mnesia中注册此数据(在包含两个属性的person表中:Name和Surname)
是否可以将数据从excel文件保存到mnesia数据库
如果无法将数据从excel文件导入mnesia,我们可以将源文件(excel文件)转换为.sql或.txt,然后将此文件导入mnesia
答案 0 :(得分:2)
我认为最简单的方法是将excel数据导出/保存到csv文件,然后使用这样的代码从erlang中解析它。
{ok, Data} = file:read_file("test.csv"),
ParsedData = lists:map(
fun(Str) -> string:tokens(Str, ",") end,
string:tokens(binary_to_list(Data), "\n")
),
lists:foreach(fun([K,V]) -> mnesia:write(K, V) end, ParsedData).
答案 1 :(得分:0)
您需要将Excel文件转换为.csv
文件。然后,您可以使用此处提供的CSV Parser
我: https://stackoverflow.com/a/10810660/431620。从Excel导入数据非常简单直接。
编辑
答案 2 :(得分:0)
我创建了这个文件 csv.erl :
%%% --- 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.
clean(Text,Char)->
string:strip(string:strip(Text,right,Char),left,Char).
我创建了一个函数,以便从csv文件中提取数据:
test()->
ForEachLine = fun(Line,Buffer)-> io:format("Line: ~p~n",[Line]),Buffer end,
InitialBuffer = [],
csv:parse("/home/include/person.csv",ForEachLine,InitialBuffer).
并且在erlang的控制台中显示正确的结果:
1> operation:test().
Line: ["50;97919861;5ab190537df;9898abb6e;efb65a0ad1;1",
"35427;1",
"3542;30-11-2012;testU2;testU6;undefined;m.;enabled;jjj;A999997;97979797;CIN;testU@gmail.com;b1088b51297846e;0;user;0;{{2012",
"11","30}","{1","59",
"26}};gggg;kkkk;ssss;standard;{2012","11",
"30};hhhh;mmm;undefined;18;yyyy;nnnn;false;{{2012",
"11","30}","{1","59","26}};1989;1;1"]
ok
现在我的目标是在mnesia数据库中注册这些数据(在表格中)
表人有49个属性
所以注册这一行
Line: ["50;97919861;5ab190537df;9898abb6e;efb65a0ad1;1",
"35427;1",
"3542;30-11-2012;testU2;testU6;undefined;m.;enabled;jjj;A999997;97979797;CIN;testU@gmail.com;b1088b51297846e;0;user;0;{{2012",
"11","30}","{1","59",
"26}};gggg;kkkk;ssss;standard;{2012","11",
"30};hhhh;mmm;undefined;18;yyyy;nnnn;false;{{2012",
"11","30}","{1","59","26}};1989;1;1"]
在具有这种结构的表格中:
-record(person, {id, token, password, pin, key, salt, pin_salt, subscription_date, first_name, last_name, alias, gender, status,
taxid, formid, idcard, id_type, email, activation_code, email_confirmation, role,
trying, last_access, last_activity, last_activity_ip, last_activity_op_code, group, last_mod, last_login,
agent_code, agency_code, parent_id,pass_changes, pin_changes, is_merchant, created_at, birth_year, birth_month, birth_date}).