iOS 根據(jù)excel表格自動(dòng)添加系統(tǒng)提醒事項(xiàng)

前言

姐姐需要把公司的一些證件審核時(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)始

  1. 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];
  1. 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) {
                }
            }
        });
    }];
}

效果圖


提醒列表
提醒詳情
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,362評(píng)論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,577評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 178,486評(píng)論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 63,852評(píng)論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,600評(píng)論 6 412
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 55,944評(píng)論 1 328
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,944評(píng)論 3 447
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 43,108評(píng)論 0 290
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,652評(píng)論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,385評(píng)論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,616評(píng)論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,111評(píng)論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,798評(píng)論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 35,205評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 36,537評(píng)論 1 295
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,334評(píng)論 3 400
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,570評(píng)論 2 379

推薦閱讀更多精彩內(nèi)容

  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,198評(píng)論 30 471
  • /**ios常見(jiàn)的幾種加密方法: 普通的加密方法是講密碼進(jìn)行加密后保存到用戶偏好設(shè)置( [NSUserDefaul...
    彬至睢陽(yáng)閱讀 2,972評(píng)論 0 7
  • 1、禁止手機(jī)睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa閱讀 1,138評(píng)論 1 6
  • 李一十八閱讀 265評(píng)論 0 0
  • 10多年前,人們都擔(dān)心80后無(wú)法從60后70后那接過(guò)責(zé)任的重?fù)?dān),但是08年抗震救災(zāi)時(shí),以80后為主的年青一代義...
    那年的夏閱讀 4,084評(píng)論 5 15