Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I'm using following way to open an Excel file in side webBrowser Control in my windows application. it opens correctly. I want save data if some one change or add new values to cells. How can I do this in my application.

webBrowser1.Navigate(@"C:\Documents and Settings\shiv.singh\Desktop\Exceptions.xlsx");


thanks
Posted
Updated 17-Jan-14 0:09am
v2
Comments
ZurdoDev 17-Jan-14 7:44am    
This code will open the Excel file on the clients machine. You no longer have any control over it. What exactly are you wanting to do?
ShivKrSingh 18-Jan-14 0:33am    
This is windows application and I have integrated excel file in webBrowser control because I searched lot but not found any other solution. I also want full control over Excel file. I want to check if data modification take place it should save in existing file. If you have other way to use excel file in application, please do let me know....
ZurdoDev 18-Jan-14 16:28pm    
Look into creating an Excel project using the VSTO (Visual Studio Tools for Office) or perhaps integrating the Microsoft Interop dll into your project instead.

1 solution

C#
//Don't forget the directive
using System.Net;

...

private void webBrowser1_Navigating_1(object sender, WebBrowserNavigatingEventArgs e)
{
    //Provide the path and file name for the download
    string targetPath = @"C:\ExcelFileTarget\MyDownLoadedExcelFile.xlsx";
    //Create a web client and the event for the download
    WebClient client = new WebClient();  
    client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
    //Place the file in the desired folder.
    client.DownloadFileAsync(e.Url, targetPath);  
}

private void button1_Click(object sender, EventArgs e)
{
    //Use what ever method you want to load the file into the browser    
    downloadMyExcelFile();
}

private void downloadMyExcelFile()
{
    //As the file is loaded let's use the navigate event 
    //to trigger the file download. There should be a test to 
    //assure there is a file to upload.
    string urlPath = @"C:\ExcelFileSource\MyExcelFile.xlsx";
    webBrowser1.Navigate(urlPath);
}

void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    //This event can be left empty, but the event needs to happen
    MessageBox.Show("File downloaded");
}


Good luck!
 
Share this answer
 
Comments
ShivKrSingh 18-Jan-14 0:49am    
hi, In your code it is used to download existing file. but I want to save it only if there is any modification take place in data.
S Houghtelin 20-Jan-14 9:01am    
Thanks for reading my answer. I chose the navigate event as an easy way to demonstrate. I was leaving it up to you to genrate an On cell change or Save Changes Event. I was going by your question "How to save Excel File from Web Browser.." For that I've provided the correct answer.

Regards

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