http://aobdo.com/2015/08/27/exporting TableView data to Excel/?plg_nld=1&plg_uin=1&plg_auth=1&plg_nld=1&plg_ Usr=1&plg_vkey=1&plg_dev=1
1. Now get to the point, first download Microsoft Excel library file http://www.libxl.com/download.html, unzip
2. There is a example, you can refer to the Libxl.framework to import their own projects, set Bitcode for No, and linker also to be changed to-lstdc++
3. On the code, import the header file, set the agent
1
#include "libxl/libxl.h"
1
@interface Jgdetailcontroller () <uitableviewdatasource,uitableviewdelegate,jgpeopleviewdelegate, Uidocumentinteractioncontrollerdelegate>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-(void) clickbarbutton{
NSLog (@ "Createexcel");
Bookhandle book = Xlcreatebook (); Use Xlcreatexmlbook () for working with xlsx files
Sheethandle sheet = xlbookaddsheet (book, "Sheet1", NULL);
The first parameter represents which table is inserted, the second is the number of rows (default starting from 0), and the third is the number of columns (default starting from 0)
XLSHEETWRITESTR (sheet, 1, 0, "name", 0);
XLSHEETWRITESTR (sheet, 1, 1, "Gender", 0);
XLSHEETWRITESTR (sheet, 1, 2, "school", 0);
XLSHEETWRITESTR (sheet, 1, 3, "phone", 0);
for (int i = 0; i < Self.nameArray.count; i++) {
Const Charchar *name_c = [self.namearray[i] cstringusingencoding:nsutf8stringencoding]; Here is the conversion of the NSString string to the C language string
XLSHEETWRITESTR (sheet, i+2, 0,name_c, 0);
}
for (int i = 0; i < Self.sexArray.count; i++) {
Const Charchar *sex_c = [self.sexarray[i] cstringusingencoding:nsutf8stringencoding];
XLSHEETWRITESTR (sheet, i+2, 1,sex_c, 0);
}
for (int i = 0; i < Self.schoolArray.count; i++) {
Const Charchar *school_c = [self.schoolarray[i] cstringusingencoding:nsutf8stringencoding];
XLSHEETWRITESTR (sheet, i+2, 2,school_c, 0);
}
for (int i = 0; i < Self.phoneArray.count; i++) {
Const Charchar *phone_c = [self.phonearray[i] cstringusingencoding:nsutf8stringencoding];
XLSHEETWRITESTR (sheet, i+2, 3,phone_c, 0);
}
NSString *documentpath =
[Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES) objectatindex:0];
NSString *filename = [documentpath stringbyappendingpathcomponent:@ "Out.xls"];
NSLog (@ "filepath--%@", filename);
Xlbooksave (book, [filename utf8string]);
Xlbookrelease (book);
Exporting XLS files
Uidocumentinteractioncontroller *DOCU = [Uidocumentinteractioncontroller interactioncontrollerwithurl:[nsurl Fileurlwithpath:filename]];
Docu.delegate = self;
CGRect rect = CGRectMake (0, 0, 320, 300); It doesn't feel right here.
[Docu Presentopeninmenufromrect:rect InView:self.view Animated:yes]; Do not write can preview directly
[Docu Presentpreviewanimated:yes]; This is a more pit-father. If you do not write this sentence, only the above sentence will pop up select the support XLS file app.
}
1
2
3
4
5
6
Doucumentdelegate method (must be implemented, there are several display selection system comes with print or what do not say, you can study it yourself)
-(Uiviewcontroller *) Documentinteractioncontrollerviewcontrollerforpreview: ( Uidocumentinteractioncontroller *) interactioncontroller{
return self;
}
4. Take the data inside the model TableView and put it in the array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Self.namearray = [Nsmutablearray array];
Self.sexarray = [Nsmutablearray array];
Self.schoolarray = [Nsmutablearray array];
Self.phonearray = [Nsmutablearray array];
For (Jgdetail *de in self.people) {
[Self.namearray AddObject:de.name];
if (De.sex = = 0) {
[Self.sexarray addobject:@ "male"];
}else if (de.sex = = 1) {
[Self.sexarray addobject:@ "female"];
}
[Self.schoolarray AddObject:de.school];
[Self.phonearray AddObject:de.tel];
}
NSLog (@ "NameArray---%@", self.sexarray);
5. This allows you to select QQ to open the generated XLS file. The effect is as follows, because this is a charge, so ignore the first line:
Generate an Excel table or Word file in your project