Click here to Skip to main content
16,022,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my 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.
Posted
Updated 16-Jun-14 21:46pm
v5

1 solution

Concerning the copied data you can access it using the Clipboard class ... Details on how to use it are available on MSDN here

To export copied data to an excel sheet, follow these steps:

1- Convert the copied data to a 2-dimensional string (you can do this by parsing the strings in nested for loops).

2- Create an instance of Microsoft Excel and implement the DisplayInExcel method:

C#
using System;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

public class ExcelDemo
{
        private Excel.Application xlApp;
        private Excel._Workbook xlBook;
        private Excel._Worksheet xlSheet;
        private object misValue = Missing.Value;

        public void DisplayInExcel(string[,] Data)
         {
            Excel.Workbooks xlBooks;
            Excel.Sheets xlSheets;
            Excel.Range range;

            try
            {
                // Instantiate Excel and start a new workbook.
                xlApp = new Excel.Application();
                xlBooks = xlApp.Workbooks;
                xlBook = xlBooks.Add(Missing.Value);
                xlSheets = xlBook.Worksheets;
                xlSheet = (Excel._Worksheet)xlSheets.get_Item(1);

                //Set the default location for saving files
                xlApp.DefaultFilePath = @"C:\test";

                //Get the range where the starting cell has the address
                //m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
                range = xlSheet.get_Range("A2", misValue);
                range = range.get_Resize(Data.GetLength(0), Data.GetLength(1));

                //Set the range value to the array.
                range.set_Value(Missing.Value, Data);

                //Autofit
                xlSheet.Rows.AutoFit();
                xlSheet.Columns.AutoFit();

                //Save the workbook
                string timeStamp = DateTime.Now.ToString("ddMMyyyymmss");
                string save = string.Concat("test1", timeStamp, ".xls");

                //Save as
                xlBook.SaveAs(save, Excel.XlFileFormat.xlWorkbookNormal, misValue,
                misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue,  misValue,
                misValue, misValue, misValue);

           }
           catch(Exception ex)
             {
               //Log exception
             }
       }
}
 
Share this answer
 
Comments
Member 10872859 17-Jun-14 5:06am    
Thanks for your guidance.

Sir, I have already used ClipBoard class for this purpose.My code is as follows:
private void btnExport_Click(object sender, EventArgs e)
{
string browserContents = webBrowser1.Document.Body.InnerText;

webBrowser1.Document.ExecCommand("SelectAll", false, null);

webBrowser1.Document.ExecCommand("Copy", false, null);

Clipboard.SetDataObject(browserContents);

IDataObject iData = Clipboard.GetDataObject();
}

And I know the process of generation of excel sheet too.

I am just stuck at the point of saving the copied data into excel. As you can see in code, data is saved from web browser control.

Can you please help me with this step according to my case.
1- Convert the copied data to a 2-dimensional string (you can do this by parsing the strings in nested for loops)
Kareem Abou Saad 17-Jun-14 7:54am    
Yes I will give you an example:
Consider the string: Kareem,20,Programmer.
Each value separated by a comma represents a field, such the it resembles the following pattern: Name,Age,Occupation.

string s = "Kareem,20,Programmer";
string[] info = String.Split(","); //Splits the string into fields separated by a comma.
string[] fields = {"Name", "Age", "Occupation");

//Fill the 2-dimensional array using for loops
String[,] ExportData()
{
String[,] Data = new String[fields.Length, 2];

int iRow;
for (iRow = 0; iRow < fields.Length; iRow++)
{
//Put the row and column address in the cell.
Data[iRow, 0] = fields[iRow];
Data[iRow, 1] = info[iRow];
}

return Data;
}

Sorry for the late reply by the way I hope I helped.
Member 10872859 17-Jun-14 7:58am    
Thanks Sir..I got the clues from your hints. I have managed to send data of full page to first column of excel sheet. Now I am looking forward for automatic selection of particular content from the content in my web browser control. The complete data needs not to be exported to excel.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900