通过Erlang编写Excel文件 [英] Writing Excel FIle through Erlang
问题描述
-module(excel)。
-export([start / 1])。
start(Val) - >
案例文件:
{ok,Fd} - >的open(office-test.xls,[append]) 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 file 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)。
它能够成功写入,但是当我在LibreOffice中打开。我起床了一个弹出窗口,要求数据分开。我不希望最终用户在此处工作。
1)有没有办法让办公室(ms office或libre office)自动解析。
2)有没有其他办法通过erlang编写excel表???
你必须写一个 CSV,逗号分隔的文本文件
。您必须使用 .csv
文件扩展名保存。你按行写入这个文件行。请确保每行以 \r\\\
结尾。这个文件可以从excel中读取得很好。
确保标题出现在第一行,如下所示:
Name,Sex, Project \\\\
Joe Armstrong,男,Erlang\\\\
Klacke Wickstrom,男,Yaws\r\\\
生锈R,男,氮\\ r \\\
比尔·盖茨,男,\\\\
Muzaaya约书亚,男,ZeePay \\\\
此外,文件编码也很重要。 ANSI编码更好。您也可以通过首先使用excel将文件转换为/ code> .csv,逗号分隔的文件来处理Erlang中的Excel文件。
然后在Erlang中使用此 csv文件解析器模块
%%% --- csv解析器。 ------
%%%帮助处理大型csv文件,而无需将它们加载到
%%%内存中。类似于SAX的xml解析技术
-module(csv)。
-compile(export_all)。
parse(FilePath,ForEachLine,Opaque) - >
案例文件:open(FilePath,[read])
{_,S} - >
start_parsing(S,ForEachLine,不透明);
错误 - >错误
结束。
start_parsing(S,ForEachLine,不透明) - >
Line = io:get_line(S,''),
case行
eof - > 【OK,不透明};
\\\
- > start_parsing(S,ForEachLine,不透明);
\r\\\
- > start_parsing(S,ForEachLine,不透明);
_ - >
NewOpaque = ForEachLine(扫描器(clean(clean(Line,10),13)),Opaque),
start_parsing(S,ForEachLine,NewOpaque)
end。
$ b $当Head = Char - >时,b扫描(InitString,Char,[Head | Buffer])
{lists:reverse(InitString),Buffer};
scan(InitString,Char,[Head | Buffer])当Head = / = Char - >
扫描([Head | InitString],Char,Buffer);
scan(X,_,Buffer)当Buffer == [] - > {完成后,列表:反向(X)}。
扫描仪(文本) - >列表:reverse(traverse_text(Text,[]))。
traverse_text(Text,Buff) - >
案例扫描(,$ ,,文本)
{done,SomeText} - > [SomeText |巴夫];
{Value,Rem} - > traverse_text(Rem,[Value | Buff])
end。
clean(Text,Char) - >
string:strip(string:strip(Text,right,Char),left,Char)。
如何使用此模块从Excel解析csv文件。以上我们简单的csv文件的例子,在shell
C:\Windows\System32> erl
Eshell V5。 9(中止与^ G)
1> ForEachLine = fun(Line,Buffer) - > io:format(Line:〜p〜n,[Line]),缓冲区结束。
#Fun< erl_eval.12.111823515>
2> InitialBuffer = []。
[]
3> CSV:解析( E:/erlang_projects.csv,ForEachLine,InitialBuffer)。
行:[Name,Sex,Project]
行:[Joe Armstrong,Male,Erlang]
行:[Klacke Wickstrom ,男,Yaws]
行:[生锈R,男,氮]
行:[比尔·盖茨,男,[]]
行:[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:解析( E:/erlang_projects.csv,ForEachLine2,InitialBuffer)。
行:[Name,Sex,Project]
行:[Joe Armstrong,Male,Erlang]
行:[Klacke Wickstrom ,男,Yaws]
行:[生锈R,男,氮]
行:[比尔·盖茨,男,[]]
线:[Muzaaya Joshua,男,ZeePay]
{ok,[[Muzaaya Joshua,男,ZeePay],
[比尔·盖茨 ,男,[]],
[Rusty R,Male,Nitrogen],
[Klacke Wickstrom,Male,Yaws b [Joe Armstrong,Male,Erlang],
[Name,Sex,Project]]}
6>
所以你可以稍后使用这个模块从Excel中解析你的csv文件。现在,只需学习如何一行编写一个csv文件,在文件一章或erlang文档中阅读实用的Erlang编程书。
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 .csv
file 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:
Name,Sex,Project\r\n Joe Armstrong,Male,Erlang\r\n Klacke Wickstrom,Male,Yaws\r\n Rusty R,Male,Nitrogen\r\n Bill Gates,Male,\r\n Muzaaya Joshua,Male,ZeePay\r\n
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 file
using excel.
Then use this csv file parser module
%%% --- 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).
How 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.
这篇关于通过Erlang编写Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!