2013-01-16 38 views
2

我有存储在Excel文件将数据传输到二郎的Mnesia

该文件包含两列数据:姓名

,并包含几行

我的目标是将这些数据注册到我的数据库mnesia(在包含两个属性名称和姓氏的人员表中)

是否可以将数据从excel文件保存到mnesia数据库

如果无法将数据从excel文件导入mnesia,我们可以将源文件(excel文件)转换为.sql或.txt,之后将该文件导入mnesia

回答

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). 
0

您需要将Excel文件转换为.csv文件。那么你可以使用我提供的CSV Parser这里:https://stackoverflow.com/a/10810660/431620。从Excel导入数据非常简单直接。

编辑


我也忘了什么东西,我上面提供的CSV分析器可确保任何文件的大小可以被解析成任何你想要的应用程序二郎。这是通过扫描文件完成的,而不是一次将它全部读入内存。谨防编写试图一次读取文件的代码,一个大文件会使vm崩溃。 我们已经使用该程序将其他数据库(如SQL Server和Oracle)中的大型csv转储文件导入Mnesia。

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). 

,并在二郎山的控制台它displys正确的结果:

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;[email protected];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;[email protected];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}).