i am trying to write excel file through erlang. I used the following code to write the excel file
-module(excel).
-export([start/1]).
start(Val)->
case file:open("office-test.xls",[append]) of
{ok,Fd} -> io:format(" file created"),
io:fwrite(Fd,"~p\t~p\t~p~n", ["Name","Date","Number"]),
export(Fd,Val),
file:close(Fd);
{error,_} -> io:format("~nerror in creation of file")
end.
export(_,0)->
ok;
export(Fd,Val) ->
io:fwrite(Fd, "~p\t~p\t~p\t~n" ,["B123","2012/10/11 12:12:12","val"++integer_to_list(Val)]),
export(Fd,Val-1).
It was able to write successfully but when i open in LibreOffice. I got up a pop-up window asking the data seperated by. I dont want the end user to work on it.
1) Is there any way such that the office(ms office or libre office) will automatically parse it.??
2) Is there any other way to write the excel sheets through erlang..??
you have to write a
CSV , Comma delimited text file. You would have to save it with.csvfile extension. You write to this file line, by line. make sure that each line ends with\r\n. This file can be read very well from excel.You make sure that the headings appear on the first line, like this:
Also, the file encoding matters. ANSI encoding is better. You can as well process Excel files in Erlang by first converting/re-saving the file as
.csv , comma delimited fileusing excel.Then use this
csv file parser moduleHow to use this module to parse csv files from Excel. An example of our simple csv file above, in shell
C:\Windows\System32>erl Eshell V5.9 (abort with ^G) 1> ForEachLine = fun(Line,Buffer)-> io:format("Line: ~p~n",[Line]),Buffer end. #Fun<erl_eval.12.111823515> 2> InitialBuffer = []. [] 3> csv:parse("E:/erlang_projects.csv",ForEachLine,InitialBuffer). Line: ["Name","Sex","Project"] Line: ["Joe Armstrong","Male","Erlang"] Line: ["Klacke Wickstrom","Male","Yaws"] Line: ["Rusty R","Male","Nitrogen"] Line: ["Bill Gates","Male",[]] Line: ["Muzaaya Joshua","Male","ZeePay"] {ok,[]} 4> ForEachLine2 = fun(Line,Buffer)-> io:format("Line: ~p~n",[Line]),[Line|Buffer] end. #Fun<erl_eval.12.111823515> 5> csv:parse("E:/erlang_projects.csv",ForEachLine2,InitialBuffer). Line: ["Name","Sex","Project"] Line: ["Joe Armstrong","Male","Erlang"] Line: ["Klacke Wickstrom","Male","Yaws"] Line: ["Rusty R","Male","Nitrogen"] Line: ["Bill Gates","Male",[]] Line: ["Muzaaya Joshua","Male","ZeePay"] {ok,[["Muzaaya Joshua","Male","ZeePay"], ["Bill Gates","Male",[]], ["Rusty R","Male","Nitrogen"], ["Klacke Wickstrom","Male","Yaws"], ["Joe Armstrong","Male","Erlang"], ["Name","Sex","Project"]]} 6>So you can use this module later on to parse your csv files from Excel as well. Now, just learn how to write a csv file line by line, read the pragmatic Erlang Programming Book in the files chapter, or the erlang documentation.