开发者

Inserting a Dictionary into a preexisting excel file, by date

开发者 https://www.devze.com 2023-03-12 03:22 出处:网络
The Data is stored in a Dictionary and arranged by month. (the string is what needs to be inserted.) I have a preexisting excel file: it has all the dates of the year on the first column, and various

The Data is stored in a Dictionary and arranged by month. (the string is what needs to be inserted.)

I have a preexisting excel file: it has all the dates of the year on the first column, and various entries in the following column. This data needs to be preserved.

My task is to basically insert the Dictionary into this excel file. What complicates things is that the Date value in the dictionary开发者_高级运维, needs to correspond to the date value in the excel column (date and month). To explicate: ("xxxx", 1980-05-12) needs to be inserted into the excel column with the first cell as "12-May" (this was generated via Fill->series).

And I have no idea. I'm spluttering by on bits of programming I'd picked up a couple of years ago. I've already extracted the data from the web page, and sorted it and all - automating some of the boring manual work. But I am faltering at the last mile, and seriously do not want to manually enter a couple of thousand data points when I know a simple script would suffice.

So, any help would be appreciated.


private void FindAndSetDate(WorkSheet ws, Dictionary<DateTime,string> dict)
{
    Range find = null;
    foreach(KeyValuePair<DateTime,String> kvp in Dict)
    {
        find = ws.Cells.Find(kvp.Key, Type.Missing,
        Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
        Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,
        Type.Missing, Type.Missing);

        if(find!=null) find.Offset[0,1].Value=kvp.Value;    
    }   

}

take a look at this thread How to iterate through a column in an Excel application through C# Console?


If you have Excel installed you could use excel interop to fill the excel sheet. Formatting the date is easy with the DateTime string format options. In your case myDateTime.ToString("dd-MMM") would result in the desired format.

0

精彩评论

暂无评论...
验证码 换一张
取 消