In my Windows application, I want to create an automation software using C#.NET which works in two steps:

  1. Select particular data and copy it from a page of a website. I have done it using web browser control for SelectAll. The data is not HTML data but databound data.
  2. Next, I want to save the copied contents of the page to an to excel sheet directly. There is no datatable or gridview to export, just the copied content directly from a page. I urgently need help for this code section.

I'll be grateful for your immediate help.

Dani AI

Generated

— since you already have the WebBrowser selection working, the simplest, robust pipeline is: read the clipboard, detect the best available clipboard format, turn that into rows/columns, then save either as a CSV (fast, no Excel required) or as a real .xlsx (via Excel Interop or an OpenXML/third‑party library). That keeps your UI code separate from export logic and avoids re-fetching the page. 's pointer about export examples is useful context and / touched on programmatic approaches — below is a concrete clipboard-first solution you can apply immediately.

Run on the UI (STA) thread or use an STA worker to read the clipboard, parse tab-delimited clipboard text into fields, then write CSV:

// safe STA read, parse tabs, write CSV
string GetClipboardTextSta()
{
    string result = null;
    var t = new Thread(() =>
    {
        if (Clipboard.ContainsText(TextDataFormat.UnicodeText))
            result = Clipboard.GetText(TextDataFormat.UnicodeText);
    });
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
    t.Join();
    return result;
}

var text = GetClipboardTextSta();
if (string.IsNullOrEmpty(text)) throw new InvalidOperationException("Clipboard has no text");

var lines = text.Split(new[]{'\r','\n'}, StringSplitOptions.RemoveEmptyEntries);
using(var sw = new StreamWriter(@"C:\temp\export.csv", false, Encoding.UTF8))
{
    foreach(var ln in lines)
    {
        var fields = ln.Split('\t').Select(f => "\"" + f.Replace("\"","\"\"") + "\"");
        sw.WriteLine(string.Join(",", fields));
    }
}

Notes and troubleshooting

  • Always check IDataObject.GetFormats() if you get unexpected output; some pages put an HTML table in the clipboard — parse HTML (HtmlAgilityPack) if so.
  • If you need .xlsx: use Excel Interop (requires Excel installed; clean up COM objects and avoid on servers) or build .xlsx via Open XML / EPPlus / ClosedXML from the parsed rows.
  • Test with several pages and log the raw clipboard text to confirm delimiters (tabs vs spaces vs commas) before parsing.

Recommended Answers

All 3 Replies

This site was of great help to me when I had a somewhat similar problem.

Use WebClient class methods to read the URLs.

Will .NET Excel API which supports converting .asp page to Excel file can sovle your problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.