导入 libxlsxwriter
Podfile 文件添加如下示例,终端cd到项目文件夹,执行 pod install
pod 'libxlsxwriter'
引用
导入头文件
#import <xlsxwriter/xlsxwriter.h>
创建Excel
- (void)createExcel1Random{ NSString *fileName = [[UIDevice jy_documentPath] stringByAppendingPathComponent:@"demo_k.xlsx"]; if ([[NSFileManager defaultManager] fileExistsAtPath:fileName] ) { [[NSFileManager defaultManager] removeItemAtPath:fileName error:nil]; } NSLog(@"fileName = %@",fileName); // 创建新xlsx文件,路径需要转成c字符串 lxw_workbook *workbook = workbook_new([fileName UTF8String]); // 创建一个sheet lxw_worksheet *worksheet = workbook_add_worksheet(workbook, [@"tem_data" UTF8String] ); ///设置列宽 worksheet_set_column(worksheet, COLS("A:A"), 1.5, NULL); ///序列 worksheet_set_column(worksheet, COLS("B:B"), 15, NULL); ///数据列 worksheet_set_column(worksheet, COLS("C:C"), 50, NULL); ///行高 worksheet_set_row(worksheet, 0, 10, nil); worksheet_set_row(worksheet, 1, 16.8, nil); worksheet_set_row(worksheet, 2, 16.8, nil); lxw_format *format = [self generalCellAttribute:workbook]; for (int i = 0; i < 20; i++) { NSString *ran = [[NSString jy_randomLetterAndNumber:26] uppercaseString]; uint32_t lxw_row_t = (uint32_t)(i); worksheet_write_string(worksheet, lxw_row_t + 1, 0 + 1, [[NSString stringWithFormat:@"%d",i+1] UTF8String], format); worksheet_write_string(worksheet, lxw_row_t + 1, 1 + 1, [ran UTF8String], format); } workbook_close(workbook); NSLog(@"fileName = %@",fileName); } /// 一般单元格样式 /// @param workbook lxw_format - (lxw_format *)generalCellAttribute:(lxw_workbook *)workbook{ lxw_format *format = workbook_add_format(workbook); format_set_border(format, LXW_BORDER_THIN);// 边框(四周):中宽边框 // 加粗 format_set_bold(format); //颜色 format_set_font_color(format, LXW_COLOR_BLACK); // 水平居中 format_set_align(format, LXW_ALIGN_CENTER); //垂直居中 format_set_align(format, LXW_ALIGN_VERTICAL_CENTER); //设置单元格格式 /* format_set_num_format(format, "¥#,##0.00"); */ return format; }
- (void)createExcel2Random{ NSString *fileName = [[UIDevice jy_documentPath] stringByAppendingPathComponent:@"demo_k.xlsx"]; if ([[NSFileManager defaultManager] fileExistsAtPath:fileName] ) { [[NSFileManager defaultManager] removeItemAtPath:fileName error:nil]; } NSLog(@"fileName = %@",fileName); // 创建新xlsx文件,路径需要转成c字符串 lxw_workbook *workbook = workbook_new([fileName UTF8String]); // 创建一个sheet lxw_worksheet *worksheet = workbook_add_worksheet(workbook, [@"tem_data" UTF8String] ); ///设置列宽 worksheet_set_column(worksheet, COLS("A:A"), 1.5, NULL); ///序列 worksheet_set_column(worksheet, COLS("B:B"), 15, NULL); ///数据列 worksheet_set_column(worksheet, COLS("C:C"), 50, NULL); ///行高 worksheet_set_row(worksheet, 0, 10, nil); worksheet_set_row(worksheet, 1, 16.8, nil); worksheet_set_row(worksheet, 2, 16.8, nil); /*合并4个单元格。*/ worksheet_merge_range(worksheet, 1, 1, 2, 2, [@"合并单元格" UTF8String], [self mergeCellAttribute:workbook]); lxw_format *format = [self generalCellAttribute:workbook]; for (int i = 0; i < 20; i++) { NSString *ran = [[NSString jy_randomLetterAndNumber:26] uppercaseString]; uint32_t lxw_row_t = (uint32_t)(i); worksheet_write_string(worksheet, lxw_row_t + 3, 0 + 1, [[NSString stringWithFormat:@"%d",i+1] UTF8String], format); worksheet_write_string(worksheet, lxw_row_t + 3, 1 + 1, [ran UTF8String], format); } workbook_close(workbook); NSLog(@"fileName = %@",fileName); } /// 合并单元格样式 /// @param workbook lxw_format - (lxw_format *)mergeCellAttribute:(lxw_workbook *)workbook{ lxw_format *merge_format = workbook_add_format(workbook); /* 为合并范围配置格式。*/ //合并居中(左右) format_set_align(merge_format, LXW_ALIGN_CENTER); //合并居中(上下) format_set_align(merge_format, LXW_ALIGN_VERTICAL_CENTER); //字体加粗 format_set_bold(merge_format); //字体颜色 format_set_font_color(merge_format,LXW_COLOR_BLACK); //字体大小 format_set_font_size(merge_format, 16.0); //背景颜色 format_set_bg_color(merge_format, LXW_COLOR_GRAY); //边框样式 format_set_border(merge_format, LXW_BORDER_MEDIUM); return merge_format; }
相关格式
/* 创建新工作簿。*/ lxw_workbook *workbook = workbook_new("format_font.xlsx"); /* 添加工作表。*/ lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); /* 扩大第一列,使文本更清晰。*/ worksheet_set_column(工作表,0,0,20,NULL); /* 添加一些格式。*/ lxw_format *format1 = workbook_add_format(workbook); lxw_format *format2 = workbook_add_format(workbook); lxw_format *format3 = workbook_add_format(workbook); /* 为格式1设置粗体属性。*/ format_set_bold(format1); /* 为格式2设置斜体属性。*/ format_set_italic(format2); /* 为格式3设置粗体和斜体属性。*/ format_set_bold(格式3); format_set_italic(format3); /*写一些格式化的字符串。*/ worksheet_write_string(工作表,0,0,“这是粗体”,格式1); worksheet_write_string(工作表,1,0,“这是斜体”,格式2); worksheet_write_string(工作表,2,0,“粗体和斜体”,格式3); /* 关闭工作簿,保存文件并释放任何内存。*/ workbook_close(工作簿);
导出Excel
- (void)exportExcel{ NSString *filePath = [[UIDevice jy_documentPath] stringByAppendingPathComponent:@"demo_k.xlsx"]; if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] ) { NSURL *url = [NSURL fileURLWithPath:filePath]; _documentController = [UIDocumentInteractionController interactionControllerWithURL:url]; _documentController.delegate = self; [self openDocumentMenu]; } else{ NSLog(@"没有文件"); } } - (void)openDocumentMenu{ [_documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES]; // 菜单操作 }
官方文档
内容来源于网络如有侵权请私信删除
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!