思路
将数据源拼成字符串,每组(行)数据用\n隔开,每组数据中每个数据之间用\t隔开,然后采用utf16的编码格式生成NSData类型的数据,存储为.xls后缀的文件。
姓名\t 年龄 \t身高\t体重\n
xxx1\t 19 \t 180\t56 \n
xxx2\t 20 \t 175\t78 \n
代码实现
#import <Foundation/Foundation.h>
#import "HJXLSTitleModle.h"
@interface HJXLSManager : NSObject
/**
* @Author A, 2017-3-17 9:00:00
*
* 生成Excel文件
*
* @param titles HJXLSTitleModle类型的数组,
* HJXLSTitleModle用于存储标题,和对应模型数据中的key
*
* @param datas 数据模型数组
*
* @return BOOL 生成是否成功
*/
+(BOOL)createXLSFileTitles:(NSArray <HJXLSTitleModle *>*)titles datas:(NSArray *)datas fileName:(NSString *)name;
/**
* @Author A, 2017-3-17 9:00:00
*
* 通过名字获取ecel文件
*
*
* @return excel文件data数据
*/
+(NSData *)XLSFileWithFileName:(NSString *)Name;
+(NSString *)xlsFilePath;
@end
#import "HJBasicModle.h"
@interface HJXLSTitleModle : HJBasicModle
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *key;
-(instancetype)initWithName:(NSString *)name key:(NSString *)key;
@end
titles也可以字典,字典key为模型的属性名,字典value为标题名称。因为字典是无序的,所以标题名称也是无序的,所以这里我采用数组存储这个标题名称。
#import "HJXLSManager.h"
@implementation HJXLSManager
+(NSString *)xlsFilePath{
NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *xlsDirPath = [documentPath stringByAppendingPathComponent:@"xlsFile"];
if (![[NSFileManager defaultManager] fileExistsAtPath:xlsDirPath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:xlsDirPath withIntermediateDirectories:YES attributes:nil error:nil];
}
return xlsDirPath;
}
+(BOOL)createXLSFileTitles:(NSArray <HJXLSTitleModle *>*)titles datas:(NSArray *)datas fileName:(NSString *)name{
if (!titles) {
return NO;
}
NSString *fileName = name?[name stringByAppendingString:@".xls"]:@"excel.xls";
NSMutableArray *xlsDataMuArr = [NSMutableArray array];
//插入标题
for (HJXLSTitleModle *modle in titles) {
[xlsDataMuArr addObject:modle.name];
//插入制表符
[xlsDataMuArr addObject:@"\t"];
}
//插入数据
[xlsDataMuArr replaceObjectAtIndex:xlsDataMuArr.count-1 withObject:@"\n"];//换行
for (id subData in datas) {
for (HJXLSTitleModle *modle in titles) {
NSString *objectString = [subData valueForKey:modle.key];
[xlsDataMuArr addObject:[self XLSString:objectString]];
[xlsDataMuArr addObject:@"\t"];
}
[xlsDataMuArr replaceObjectAtIndex:xlsDataMuArr.count-1 withObject:@"\n"];
}
NSString *xlsString = [xlsDataMuArr componentsJoinedByString:@""];
NSData *xlsData = [xlsString dataUsingEncoding:NSUTF16StringEncoding];
NSFileManager *fileManager = [NSFileManager defaultManager];
return [fileManager createFileAtPath:[[self xlsFilePath] stringByAppendingPathComponent:fileName] contents:xlsData attributes:nil];
}
+(NSString *)XLSString:(NSString *)string{
return string?:@"null";
}
+(NSData *)XLSFileWithFileName:(NSString *)Name{
NSString *filePath = [[self xlsFilePath] stringByAppendingPathComponent:[Name stringByAppendingString:@".xls"]];
return [[NSData alloc] initWithContentsOfFile:filePath];
}
@end
使用
HJXLSTitleModle *modle1 = [[HJXLSTitleModle alloc] initWithName:@"名字" key:@"name"];
HJXLSTitleModle *modle2 = [[HJXLSTitleModle alloc] initWithName:@"年龄" key:@"age"];
HJXLSTitleModle *modle3 = [[HJXLSTitleModle alloc] initWithName:@"身高" key:@"height"];
HJXLSTitleModle *modle4 = [[HJXLSTitleModle alloc] initWithName:@"体重" key:@"weight"];
HJCeShiModle *dataModle1 = [[HJCeShiModle alloc] initWithName:@"xxx1" age:@"19" height:@"180" weight:@"56"];
HJCeShiModle *dataModle2 = [[HJCeShiModle alloc] initWithName:@"xxx2" age:@"20" height:@"175" weight:@"78"];
BOOL flag = [HJXLSManager createXLSFileTitles:@[modle1,modle2,modle3,modle4] datas:@[dataModle1,dataModle2] fileName:@"资料"];
生成的Excel文件如下:
最后附上代码
https://github.com/jswsjian/createExcel