在Cocoa中创建Excel(.xlsx)文件 [英] Creating Excel (.xlsx) files in Cocoa
问题描述
.xlsx
扩展名。 我尝试生成AppleScript并使用 NSAppleScript
创建Excel文件,它工作起来很慢,用户必须在他们的机器上安装Excel。
我能够创建一个简单的.xlsx Excel通过将XML写入Objective C中的文件并将其压缩并将其重命名为 .xlsx
。 Excel可以打开该文件,但是Numbers打错了我创建的文件。似乎Excel必须实现ISO / IEC规范的所有(或大部分),而数字只能打开Excel风格版本。
如何使用Cocoa和Objective-c?
如何在Excel文件中封装HTML表格?我已经检查这个工作,我可以用Excel打开它。
NSURL * documentsDirectory = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains :NSUserDomainMask] .lastObject;
NSURL * file = [documentsDirectory URLByAppendingPathComponent:@contacts.xls];
NSString * string = @< table>< tr>< td> FOO< / td>< td> BAR< / td>< / tr>< / table>
[string writeToFile:file.path atomically:YES encoding:NSUTF8StringEncoding error:nil];
I'm writing a Cocoa application and I'm trying to export to the Excel XML format (ISO/IEC 29500-1) which is basically a zip file with a bunch of XML files in it named with a .xlsx
extension.
I've tried generating AppleScript and using NSAppleScript
to create the Excel file, which works, but is slow and the user has to have Excel installed on their machine.
I was able to create a simple .xlsx Excel file by writing the XML to files in Objective C and zipping them up and renaming the zip to .xlsx
. Excel could open the file, but Numbers threw an error trying to open the file I created. It seems Excel must have implemented all (or most of) the ISO/IEC spec, while Numbers may only open the Excel flavored version.
How to create working Excel files using Cocoa and Objective-c?
How about encapsulating HTML table in Excel file? I have checked that this works and I can open it with Excel.
NSURL * documentsDirectory = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].lastObject;
NSURL *file = [documentsDirectory URLByAppendingPathComponent:@"contacts.xls"];
NSString* string = @"<table><tr><td>FOO</td><td>BAR</td></tr></table>";
[string writeToFile:file.path atomically:YES encoding:NSUTF8StringEncoding error:nil];
这篇关于在Cocoa中创建Excel(.xlsx)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!