9

電子メールで送信できるように、Excel 形式でレポートを作成しようとしています。これまでのところ、最良かつ最も簡単な方法は、次のように xml ドキュメントを作成し、それを xls として保存することであることがわかりました。

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> 
    <Worksheet ss:Name="Sheet1"> 
        <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1" x:FullRows="1"> 
            <Row> 
                <Cell><Data ss:Type="String">Name</Data></Cell>
                <Cell><Data ss:Type="String">Example</Data></Cell>
            </Row>
            <Row>
                <Cell><Data ss:Type="String">Value</Data></Cell>
                <Cell><Data ss:Type="Number">123</Data></Cell>
            </Row> 
        </Table>
    </Worksheet> 
</Workbook>

次に、次を使用してこのドキュメントを保存できます

NSArray *documentDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [NSString stringWithFormat:@"%@/Report.xls", [documentDirectoryPath objectAtIndex:0]];
        [xmlString writeToFile:docDir atomically:YES encoding:NSUTF8StringEncoding error:NULL];
        [serializedData writeToFile:docDir atomically:YES];

ただし、メールを送信して xls ファイルを開こうとすると、代わりに xml がスプレッドシートに表示されます。このxlsファイルを作成するための正しい方向に私を導いてください。

4

4 に答える 4

1

OPと同じことをやってみましたが、あきらめました。私は最終的に libxls を使用して xls ファイルを開いて読み取り、csv を使用してファイルを書き出しました。(libxls は xls を書き込むことはできません。読み取るだけです)。

http://libxls.sourceforge.net

試したことはありませんが、xlslib は Excel ファイルを書き込むと主張しています。2014 年 1 月 6 日に、iOS で動作するという更新があります。

http://sourceforge.net/projects/xlslib/files/

また、なぜExcelファイルを書くのがとても難しいのかを読んで ください.

于 2015-01-04T16:14:41.363 に答える
0

これはXLSファイルを作成するためのコードです

 NSMutableString *stringToWrite = [[NSMutableString alloc] init];
 [stringToWrite appendString:[NSString stringWithFormat:@"First Name /t Last Name /t Full Name /tPhone Number /tEmail /t Job/t organizationName /tNote\n\n"]];
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      for(int i = 0 ;i<[Contact count];i++)     {
           [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"firstName"] ]];
           [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"lastName"] ]];
           [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact valueForKey:@"userName"] objectAtIndex:i]]];
           [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"phoneNumber"] ]];
           [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"emailAddress"] ]];
           [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact objectAtIndex:i] valueForKey:@"jobTitle"] ]];
           [stringToWrite appendString:[NSString stringWithFormat:@"%@/t",[[Contact  objectAtIndex:i] valueForKey:@"organizationName"] ]];
           [stringToWrite appendString:[NSString stringWithFormat:@"%@\n",[[Contact objectAtIndex:i] valueForKey:@"note"] ]];
      }
      dispatch_async(dispatch_get_main_queue(), ^(void) {
           NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
           NSString *documentDirectory=[paths objectAtIndex:0];
           NSString *strBackupFileLocation = [NSString stringWithFormat:@"%@/%@", documentDirectory,@"ContactList.xls"];
           [stringToWrite writeToFile:strBackupFileLocation atomically:YES encoding:NSUTF8StringEncoding error:nil];
      });
 });
于 2016-12-29T12:52:22.423 に答える
0

ドキュメントの上部に次の特別なタグを追加してみてください。

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
[...]

XML でこのタグを試してみたところ、私の Windows7 マシンで動作しました。ドキュメントを「test.excel.xml」として保存しました。この<?mso...>タグは、Windows が形式を Excel として認識するのに十分でした。

ここにも例があります: http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats

于 2014-02-17T21:27:28.187 に答える
-4

CSVファイル形式を作成するには、それが最善の方法です

于 2013-02-12T09:12:02.443 に答える