问题描述
问候...
在此,我要感谢所有查看此问题并试图帮助我完成任务的人.
我觉得我应该修改我的问题,以包含Addin的目标,这将使其他人可以指导我完成该过程.
我正在尝试使用Visual Studio 2010在C#中为Outlook 2007创建外接程序.
以下是我要实现的任务.
1.从应用程序复制数据(全部数据为代码格式,如下所示)
1.TEST/TEST MR
2 XY 400 E 13NOV 2 CPHARN HK1 0630 0740 13 NOV E SK/2EY42J
3 XY 413 L 15NOV 4 ARNCPH HK1 2120 2230 15NOV E SK/2EY42J
2.将上述数据转换为通俗易懂的格式. (如下例)
Greetings...
In advance i would like to thanks to all those who have viewed this question and tried to help me in achieving my task.
I felt that i should modify my question to include the objective of my Addin which would enable others to guide me through the process.
I am trying to create an Addin for Outlook 2007 in C# using Visual Studio 2010.
Below is the Task that I am trying to achieve.
1. Copy Data from an Application (entire data is in codes format, example as below)
1.TEST/TEST MR
2 XY 400 E 13NOV 2 CPHARN HK1 0630 0740 13NOV E SK/2EY42J
3 XY 413 L 15NOV 4 ARNCPH HK1 2120 2230 15NOV E SK/2EY42J
2. Convert the above data to understandable format for layman. (example as below)
Carrier Flight Class Dep.Date Day From To Status Dep.Time Arr.Time Arr.Date
XYZ Airlines 400 E 13NOV Tue Copenhagen - Denmark Stockholm - Sweden Used 0630 0740 13NOV
XYZ Airlines 413 L 15NOV Thu Stockholm - Sweden Copenhagen - Denmark Used 2120 2230 15NOV
3.只需单击Outlook 2007功能区上的按钮,即可将转换后的数据粘贴到电子邮件中光标的当前位置.
我面临的主要挑战是我拥有大量数据(IATA 3字母机场代码,并有各自的机场名称),数据可扩展到约1万条记录.
我需要帮助才能知道如何将这些数据包含在我的C#代码中,而不是将数据保存在excel,SQL等外部源中.
您的帮助/协助将不胜感激.
Wahed.
3. Paste the converted data on the current location of cursor in the email with just a click of button on the ribbon of outlook 2007.
The main challenge that i am facing is that i have a large amount of Data (IATA 3 Letter Airport Code and there respective Airport name) the data stretches to about 10K records.
I need help to know how can i include this data in my c# code instead of saving the data in an external source like excel, SQL, etc.
Your help/assistance in this would be really grateful.
Wahed.
推荐答案
/// <summary>
/// Utility class to help store and retrieve objects into and out of an XML file.
/// </summary>
public static class StorageManager<t>
{
public static void Store(string filename, T obj, FileMode mode)
{
try
{
FileStream fileStream = new FileStream(filename, mode);
using (fileStream)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(fileStream, obj);
}
}
catch (Exception ex)
{
Debug.WriteLine(">>> STORAGE FATAL ERROR - " + ex.Message + " <<<");
}
}
public static T Retrieve(string filename)
{
try
{
T obj = default(T);
if (File.Exists(filename))
{
using (FileStream fileStream = new FileStream(filename, FileMode.Open))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
obj = (T)serializer.ReadObject(fileStream);
}
}
return obj;
}
catch (Exception ex)
{
Debug.WriteLine(">>> ISO STORAGE FATAL ERROR - " + ex.Message + " <<<");
return default(T);
}
}
}</t>
要使用此类,您需要创建一个包含要序列化的对象的类.像这样的东西:
To use this class, you need to create a class that holds the object you want to serialize. Something like this:
/// <summary>
/// Class to hold a group of sensor readings taken at the same time that we will save out to XML
/// </summary>
[Serializable]
public class DataPoint
{
public DateTime SampleTime;
public string SampleName;
public string SampleDataType;
public string SampleValue;
}
调用存储管理器以保存DataPoints的集合非常简单:
Calling the storage manager to save a collection of DataPoints is pretty easy:
StorageManager<Collection<DataPoint>>.Store(@"C:\temp\data.xml", _collectionOfDataPoints, FileMode.Append);
检索数据几乎一样容易:
Retrieving the data is almost as easy:
Collection<datapoint> collectionOfDataPoints = StorageManager<collection><datapoint>>.Retrieve(@"C:\temp\data.xml");</datapoint></collection></datapoint>
这种方法有一些限制...例如,对象必须可序列化.有些不是(例如Dictionary和Type),您必须更改它们的存储方式或以其他方式解决.如果您确实愿意,可以使用DataContractSerializer进行更深入的了解,但是如果您保持要序列化的类简单,我发现它可以很好地发挥作用.
一旦将对象包含在集合中,就可以使用LINQ查询来查找所需的内容. Google上有大量的示例,因此我不再赘述.
我建议您在类库中创建机场代码类.然后将其链接到您的主项目.然后,您也可以将其链接到管理项目中,在其中您可以输入数据(或导入数据),然后使用storagehelper将其序列化为一个xml文件,您可以使用存储管理器将其导入到您的应用程序中班级.这样,您就可以使用一个幕后应用程序,该应用程序将允许您管理机场代码XML文件,而您的主程序将这些数据简单地消费到内存中的集合中,可以使用LINQ进行查询.
至于使用网站更新文件,可以简单地将文件上传到网站,然后让您的应用程序访问该URL并下载文件.再次,有很多示例,说明如何使用Webclient或流来形成Web请求并将文件下载到您的应用程序中.
请记住,如果此程序在Vista或更高版本上运行,则无法将文件保存到app目录中.您必须将其保存到AppData文件夹中,您可以轻松地从Environment命名空间中获取它.
祝你好运!
There are some limits to this approach... for example, the objects must be serializable. There are some which are not (like Dictionary and Type) and you have to either change how you are storing them or otherwise work around it. You can get a lot more in depth with the DataContractSerializer if you really want to but I find it works great on its own if you keep the class you are serializing simple.
Once you have the objects in a collection, you can use a LINQ query to find what you want. There are tons of examples out there on Google for that so I wont go into it.
I would suggest that you create your airport code class in a class library. Then link it into your main project. You can also, then, link it into a management project where you would be able to enter the data (or otherwise import it) then use the storagehelper to serialize it out to an xml file that you can import into your app with the storage manager class. This way you have a behind-the-scenes app which will allow you to manage the airport codes XML file while your main program will simply consume that data into an in-memory collection which can be queried with LINQ.
As for updating the file using a web site, it can be as simple as uploading the file to a web site then having your app hit that URL and download the file. Again, there are lots of examples on how to form a web request and download a file into your app using a webclient or a stream.
Just remember that if this is running on Vista or higher, you can''t save the file into the app directory. You will have to save it into the AppData folder which you can easily get from the Environment namespace.
Good luck!
这篇关于用C#代码存储大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!