在Cocoa中创建Excel(.xlsx)文件 [英] Creating Excel (.xlsx) files in Cocoa

查看:155
本文介绍了在Cocoa中创建Excel(.xlsx)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Cocoa应用程序,我正在尝试导出到Excel XML格式(ISO / IEC 29500-1),它基本上是一个zip文件,其中包含一个XML文件,其名称为 .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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆