前言
姐姐需要把公司的一些證件審核時(shí)間錄入到手機(jī)的提醒事項(xiàng),避免時(shí)間過(guò)了忘了,于是問(wèn)我能不能弄一個(gè)出來(lái),于是便開(kāi)始搗鼓這個(gè)東西。
準(zhǔn)備工作
首先比較復(fù)雜的是excel表格的解析,因?yàn)樘O果官方不提供excel表格的解析,網(wǎng)上也找不到相應(yīng)的框架,于是便找了一些資料,進(jìn)行excel表格的解析和錄入。
樣表
序號(hào) | 姓名 | 性別 | 證號(hào) | 工種 | 領(lǐng)證日期 | 第一次復(fù)核時(shí)間 | 第二次復(fù)核時(shí)間 | 使用截止時(shí)間 |
---|---|---|---|---|---|---|---|---|
1 | 阮華珍 | 女 | 桂Dxxxxxxxxx | 建筑起重機(jī)械司機(jī)(施工升降機(jī)) | 2011年7月3日 | 2013年7月20日 | 2015年7月20日 | 2017年7月20日 |
正式開(kāi)始
-
excel的讀取
首先,excel的讀取我采取的是蘋果提供的 使用其他應(yīng)用程序打開(kāi)的功能,因?yàn)樘O果不像安卓可以數(shù)據(jù)共享,因此我采取的就是通過(guò)qq,微信或者百度云之類的把excel表格下載下來(lái),然后使用我的app打開(kāi)進(jìn)行數(shù)據(jù)的傳輸。
- 在
info.plist
添加以下代碼,讓我們的app可以使用打開(kāi)excel表格的功能
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array>
<string>MySmallIcon.png</string>
<string>MyLargeIcon.png</string>
</array>
<key>CFBundleTypeName</key>
<string>My File Format</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.powerpoint.ppt</string>
<string>public.item</string>
<string>com.microsoft.word.doc</string>
<string>com.adobe.pdf</string>
<string>com.microsoft.excel.xls</string>
<string>public.image</string>
<string>public.content</string>
<string>public.composite-content</string>
<string>public.archive</string>
<string>public.audio</string>
<string>public.movie</string>
<string>public.text</string>
<string>public.data</string>
</array>
</dict>
</array>
- 然后當(dāng)我們打開(kāi)一個(gè)excel的時(shí)候,會(huì)在appDelegate會(huì)自動(dòng)調(diào)用代理回調(diào),這里我采用的是通知進(jìn)行excel路徑的傳遞
- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options {
if (options) {
NSString *str = [NSString stringWithFormat:@"\n發(fā)送請(qǐng)求的應(yīng)用程序的 Bundle ID:%@\n\n文件的NSURL:%@", options[UIApplicationOpenURLOptionsSourceApplicationKey], url];
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:@{@"url":[url absoluteString]}];
}
return YES;
}
- 然后在相應(yīng)的地方進(jìn)行接收通知就可以拿到相應(yīng)的excel地址
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(analycsExcel:) name:@"test" object:nil];
-
excel的解析
拿到excel的路徑,我們就可以進(jìn)行excel的解析了,但是現(xiàn)在網(wǎng)上關(guān)于這塊的資料是很少的,然后我找了好久,找到一個(gè)封裝的比較好的demo進(jìn)行excel的解析。
碎月Coder的文章
這個(gè)demo的思路就是利用DHxlsReader(解讀XLS)
進(jìn)行xls的解析,因?yàn)橹皇莻€(gè)人的app,所以對(duì)于文件格式要求不嚴(yán)格,所以就沒(méi)有處理xlsx的格式,只是處理了xls的格式。
- 核心代碼
- (void)analycsExcel:(NSNotification *)noc
{
NSString *path = [noc.userInfo[@"url"] substringFromIndex:15];
DHxlsReader *reader = [DHxlsReader xlsReaderFromFile:path] ;
assert(reader);
int row = 2;
while(YES) {
CellModel *model = [CellModel new];
DHcell *blankCell = [reader cellInWorkSheetIndex:0 row:row col:2];
if (blankCell.type == cellBlank)
{
break;
}
for (int i = 2; i < 10; ++i) {
DHcell *cell = [reader cellInWorkSheetIndex:0 row:row col:i];
switch (i) {
case 2:
model.name = cell.str;
break;
case 3:
model.sex = cell.str;
break;
case 4:
model.number = cell.str;
break;
case 5:
model.tpye = cell.str;
break;
case 6:
[model changeWithGotTime:cell.str];
break;
case 7:
[model changeWithfirstTime:cell.str];
break;
case 8:
[model changeWithsecTime:cell.str];
break;
case 9:
[model changeWithfinalTime:cell.str];
break;
default:
break;
}
}
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *date = [tempFormatter stringFromDate:model.gotTime];
NSString *finalDate = [tempFormatter stringFromDate:model.finalTime];
NSString *firstTitle = [NSString stringWithFormat:@"%@ 第一次復(fù)核",model.name];
NSString *firstNotes = [NSString stringWithFormat:@"第一次復(fù)核\n%@、%@\n%@\n%@\n領(lǐng)證日期:%@\n使用截止日期:%@",model.name,model.sex,model.number,model.tpye,date,finalDate];
[[JMEventCalendar sharedEventCalendar] addReminderNotify:model.firstTime title:firstTitle notes:firstNotes];
NSString *secTitle = [NSString stringWithFormat:@"%@ 第二次復(fù)核",model.name];
NSString *secNotes = [NSString stringWithFormat:@"第二次復(fù)核\n%@、%@\n%@\n%@\n領(lǐng)證日期:%@\n使用截止日期:%@",model.name,model.sex,model.number,model.tpye,date,finalDate];
[[JMEventCalendar sharedEventCalendar] addReminderNotify:model.firstTime title:secTitle notes:secNotes];
NSString *finalTitle = [NSString stringWithFormat:@"%@ 使用截止日期",model.name];
NSString *finalNotes = [NSString stringWithFormat:@"使用截止日期\n%@、%@\n%@\n%@\n領(lǐng)證日期:%@\n使用截止日期:%@",model.name,model.sex,model.number,model.tpye,date,finalDate];
[[JMEventCalendar sharedEventCalendar] addReminderNotify:model.firstTime title:finalTitle notes:finalNotes];
[self.cellArray addObject:model];
row++;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存成功" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alert show];
}
- 單例工具類里面添加提醒事項(xiàng)的方法
- (void)addReminderNotify:(NSDate *)date title:(NSString *)title notes:(NSString *)notes
{
__weak __typeof__(self) weakSelf = self;
//申請(qǐng)?zhí)嵝褭?quán)限
[self.eventDB requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong __typeof__(weakSelf) strongSelf = weakSelf;
if (granted) {
//創(chuàng)建一個(gè)提醒功能
EKReminder *reminder = [EKReminder reminderWithEventStore:self.eventDB];
//標(biāo)題
reminder.title = title;
reminder.notes = notes;
//添加日歷
[reminder setCalendar:[self.eventDB defaultCalendarForNewReminders]];
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone systemTimeZone]];
NSInteger flags = NSCalendarUnitYear | NSCalendarUnitMonth |
NSCalendarUnitDay;
NSDateComponents* dateComp = [cal components:flags fromDate:date];
NSDateComponents* dateComp2 = [cal components:flags fromDate:[NSDate dateWithTimeInterval:88000 sinceDate:date]];
dateComp.timeZone = [NSTimeZone systemTimeZone];
reminder.startDateComponents = dateComp; //開(kāi)始時(shí)間
reminder.dueDateComponents = dateComp2; //到期時(shí)間
reminder.priority = 1; //優(yōu)先級(jí)
EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date]; //添加一個(gè)車鬧鐘
[reminder addAlarm:alarm];
NSError *err;
[strongSelf.eventDB saveReminder:reminder commit:YES error:&err];
if (err==nil) {
}
}
});
}];
}
效果圖
提醒列表
提醒詳情