1.现在开始进入正题,要先下载微软的excel库文件http://www.libxl.com/download.html,解压
2.里面有example,可以参考一下、把LibXL.framework导入自己的项目当中,设置bitcode为no,以及linker也要改为-lstdc++
3.上代码,导入头文件,设置代理
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
21
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);
//第一个参数代表插入哪个表,第二个是第几行(默认从0开始),第三个是第几列(默认从0开始)
xlSheetWriteStr(sheet, 1, 0, "姓名", 0);
xlSheetWriteStr(sheet, 1, 1, "性别", 0);
xlSheetWriteStr(sheet, 1, 2, "学校", 0);
xlSheetWriteStr(sheet, 1, 3, "电话", 0);
for (int i = 0; i < self.nameArray.count; i++) {
const charchar *name_c = [self.nameArray[i] cStringUsingEncoding:NSUTF8StringEncoding]; //这里是将NSString字符串转为C语言字符串
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);
//导出xls文件
UIDocumentInteractionController *docu = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filename]];
docu.delegate = self;
CGRect rect = CGRectMake(0, 0, 320, 300); //这里感觉没什么用
[docu presentOpenInMenuFromRect:rect inView:self.view animated:YES]; //不写可以直接预览
[docu presentPreviewAnimated:YES]; //这句比较坑爹。如果不写这句,只写上面那句会弹出选择支持xls文件的APP。
}
1
2
3
4
5
6
//doucumentDelegate方法(必须实现,还有几个展示选择系统自带print还是啥的就不说了,可以自己研究下)
- ( UIViewController *)documentInteractionControllerViewControllerForPreview:( UIDocumentInteractionController *)interactionController{
return self;
}
4.把模型tableView里面的数据取出来放在数组里:
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:@"男"];
}else if(de.sex == 1){
[self.sexArray addObject:@"女"];
}
[self.schoolArray addObject:de.school];
[self.phoneArray addObject:de.tel];
}
NSLog(@"nameArray---%@",self.sexArray);
5.这样就可以选择QQ打开生成的xls文件。效果如下,因为这个是要收费的,所以忽略第一行: