开发者

How To Generate a HTML Report From A String Of URLs

开发者 https://www.devze.com 2023-01-22 10:58 出处:网络
In my program I have a string which contains URLs separated by /n (One per line) Let\'s say the string is called \"links\". I want to take this string and generate a HTML file that will automatically

In my program I have a string which contains URLs separated by /n (One per line)

Let's say the string is called "links". I want to take this string and generate a HTML file that will automatically open in my default browser which will make each URL a hyperlink (one per line). How would I make such a report n开发者_运维百科ot using any third party components using WPF C# 4.0? I want the report to be generated by clicking a button called "Export".


There are plenty of ways to do this, but here is a quick and dirty example (debugging may be necessary since I wrote this on the fly). [Edit: Now uses Uri objects to formulate the actual address.]

private void export_Click(object sender, RoutedEventArgs e)
    {
        string tempFileName = "list.html";
        string links = "http://www.google.com/#sclient=psy&hl=en&site=&source=hp&q=test+me&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=ddfbf15c2e2f4021\nhttp://www.testme.com/Test-Prep.html?afdt=Q3RzePF0jU8KEwja-5WM7PqkAhUUiZ0KHaoG_wcYASAAMJbwoAM4MEC4w6uX7dS53gdQlvCgA1CEra8PUJzr_xNQg73wFVCKttweUJStzNoBUNv67ZsD";
        List<Uri> uriCollection = new List<Uri>();

        foreach (string url in links.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
        {
            uriCollection.Add(new Uri(url));
        }

        // Create temporary file.
        using (TextWriter writer = new StreamWriter(tempFileName))
        {
            try
            {
                writer.WriteLine("<html>");
                writer.WriteLine("<head><title>Links</title></head>");
                writer.WriteLine("<body>");

                writer.WriteLine("<p>");

                foreach (Uri uri in uriCollection)
                {
                    writer.WriteLine("<a href=\"{0}\">{1}</a><br />", uri.OriginalString, uri.Host);
                }

                writer.WriteLine("</p>");

                writer.WriteLine("</body>");
                writer.WriteLine("</html>");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ex.Message);
            }
            finally
            {
                writer.Close();
            }
        }

        // Open browser with temporary file.
        if (File.Exists(tempFileName))
        {
            System.Diagnostics.Process.Start(tempFileName);
        }
    }

The 'Export' button is wired to the event 'export_Click'. I hard-coded the the string with '\n''s for the example. Simply break these apart using split and write a temporary file creating the HTML you need. Then, once the file is completed, you can open it using the Process.Start() method.

Ideally this can be done using DataBinding and other elements available in WPF if the need to open a browser window was not required. This would also remove any external dependencies the program may have.

0

精彩评论

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