我已经生成了csv文件来存储在本地目录路径中。我试图上传csv文件到谷歌驱动器,它工作正常。从谷歌驱动器打开一个csv文件,不显示电子表格中的预览。所以我想上传csv文件(将csv转换为电子表格)到谷歌驱动器。 如何转换?
先谢谢
来自Google云端硬盘的图片
上传csv文件以供您参考的示例:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *root = [documentsDir stringByAppendingPathComponent:@"Report.csv"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:root];
if(fileExists == YES)
{
GTLUploadParameters *uploadParameters = nil;
self.driveFile = [[GTLDriveFile alloc]init];
NSData *dat = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:root]];
uploadParameters =
[GTLUploadParameters uploadParametersWithData:dat MIMEType:@"text/csv"];
self.driveFile.title = @"Report.csv";
GTLQueryDrive *query = nil;
if (self.driveFile.identifier == nil || self.driveFile.identifier.length == 0)
{
query = [GTLQueryDrive queryForFilesInsertWithObject:self.driveFile
uploadParameters:uploadParameters];
}
else
{
query = [GTLQueryDrive queryForFilesUpdateWithObject:self.driveFile
fileId:self.driveFile.identifier
uploadParameters:uploadParameters];
}
UIAlertView *alert = [DrEditUtilities showLoadingMessageWithTitle:@"While data is uploading..."
delegate:self];
[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *updatedFile,
NSError *error)
{
appDel.File_Id = updatedFile.identifier;
[alert dismissWithClickedButtonIndex:0 animated:YES];
if (error == nil)
{
self.driveFile = updatedFile;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Feel Fix" message:@"Successfully Uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
[alert show];
}
else
{
NSLog(@"An error occurred: %@", error);
[DrEditUtilities showErrorMessageWithTitle:@"Unable to save file"
message:[error description]
delegate:self];
}
}];
答案 0 :(得分:4)
插入转换属性以解决此问题
GTLUploadParameters *uploadParameters = nil;
self.driveFile = [[GTLDriveFile alloc]init];
NSData *dat = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:root]];
uploadParameters =
[GTLUploadParameters uploadParametersWithData:dat MIMEType:@"text/csv"];
self.driveFile.title = @"Report.csv";
GTLQueryDrive *query = nil;
if (self.driveFile.identifier == nil || self.driveFile.identifier.length == 0)
{
query = [GTLQueryDrive queryForFilesInsertWithObject:self.driveFile
uploadParameters:uploadParameters];
query.convert = YES; // Convert file format to spread sheet
}
else
{
query = [GTLQueryDrive queryForFilesUpdateWithObject:self.driveFile
fileId:self.driveFile.identifier
uploadParameters:uploadParameters];
}
UIAlertView *alert = [DrEditUtilities showLoadingMessageWithTitle:@"While data is uploading..."
delegate:self];
[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *updatedFile,
NSError *error)
{
appDel.File_Id = updatedFile.identifier;
[alert dismissWithClickedButtonIndex:0 animated:YES];
if (error == nil)
{
self.driveFile = updatedFile;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Feel Fix" message:@"Successfully Uploaded" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
[alert show];
}
else
{
NSLog(@"An error occurred: %@", error);
[DrEditUtilities showErrorMessageWithTitle:@"Unable to save file"
message:[error description]
delegate:self];
}
}];