从visual studio到Xcode,从MFC走向Cocoa应用程序(2)

7。打开openHPLCFiles.m文件书写代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
//  openHPLCFiles.m
//
//  Created by Jianhong Ou on 2/23/10.
//  Copyright 2010 University of Massachusetts. All rights reserved.
//
 
#import "openHPLCFiles.h"
 
@implementation openHPLCFiles
- (IBAction)openHPLCFiles:(id)sender {
 
	// Create the File Open Dialog class.
	NSOpenPanel* openDlg = [NSOpenPanel openPanel];
 
	// Enable the selection of files in the dialog.
	[openDlg setCanChooseFiles:YES];
 
	// Enable the selection of directories in the dialog.
//	[openDlg setCanChooseDirectories:YES];
 
	// Enable the selection of multiple files in the dialog.
	[openDlg setAllowsMultipleSelection:YES];
 
	// Display the dialog.  If the OK button was pressed,
	// process the files.
	if ( [openDlg runModal] == NSOKButton )
	{
		// Get an array containing the full filenames of all
		// files and directories selected.
		NSArray* files = [openDlg filenames];
 
		// pSaveData to store all the data for output;
		NSString* pSaveData = @"Date\tTime\tSampleID\tRetTime\tArea\t...\n";
 
		// Loop through all the files and process them.
		for(int i = 0; i < [files count]; i++ )
		{
			NSString* fileName = [files objectAtIndex:i];
			if ([fileName isAbsolutePath]) {
				// open the file.
				NSString* fileString = [NSString stringWithContentsOfFile:fileName encoding:NSASCIIStringEncoding error:nil];
 
				// read each line from each file.
				NSArray* lines = [fileString componentsSeparatedByString:@"\n"];
 
				// read the sample attributes from line 2;
				NSString* reportAttrb = [lines objectAtIndex:1];
 
				// get the date time and sample Id;
				NSArray* sampleAttrbs = [reportAttrb componentsSeparatedByString:@"\t"];
				pSaveData = [pSaveData stringByAppendingString:[sampleAttrbs objectAtIndex:3]];
				pSaveData = [pSaveData stringByAppendingString:@"\t"];
 
				pSaveData = [pSaveData stringByAppendingString:[sampleAttrbs objectAtIndex:4]];
				pSaveData = [pSaveData stringByAppendingString:@"\t"];
 
				NSString* sampleId = [sampleAttrbs objectAtIndex:5];
 
/*				NSArray* target = [NSArray arrayWithObjects:@" ",@"/",@".",nil];
				NSArray* replacement = [NSArray arrayWithObjects:@"_",@"-",@"_",nil];
				for (int m = 0; m < [target count]; m++){
					sampleId = [sampleId stringByReplacingOccurrencesOfString:[target objectAtIndex:m] withString:[replacement objectAtIndex:m]];
				}*/
				pSaveData = [pSaveData stringByAppendingString:sampleId];
				pSaveData = [pSaveData stringByAppendingString:@"\t"];
 
				int cnt = [lines count]-2;
				for(int j = 3; j< cnt; j++)
				{
					NSArray* pk = [[lines objectAtIndex:j] componentsSeparatedByString:@"\t"];
					pSaveData = [pSaveData stringByAppendingString:[pk objectAtIndex:1]];
					pSaveData = [pSaveData stringByAppendingString:@"\t"];
					pSaveData = [pSaveData stringByAppendingString:[pk objectAtIndex:2]];
					pSaveData = [pSaveData stringByAppendingString:@"\t"];
				}
				pSaveData = [pSaveData stringByAppendingString:@"\n"];
			}
		}
		// save file
		NSString* savePath = [[files objectAtIndex:0] stringByDeletingLastPathComponent];
		savePath = [savePath stringByAppendingPathComponent:@"reports.xls"];
		NSData* output = [pSaveData dataUsingEncoding:NSASCIIStringEncoding];
		@try {
			[output writeToFile:savePath atomically:FALSE];
		}
		@catch (NSException * e) {
			NSLog(@"save: Caught %@: %@",[e name],[e reason]);
		}
		@finally {}
	}
}
@end

运行程序,打开多个HPLC文件,生成报告格式如下:

生成的报告文件
生成的报告文件

发表评论

电子邮件地址不会被公开。 必填项已用*标注