Click here to Skip to main content
15,910,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am navigating Excel sheet in WebBrowser control.

eBrowser.Navigate("D:\test.xlsx", false);

if I am updating something in excel sheet which is displayed in WB control then I want to save that changes back to location or wants to import sheet with changes in data table.
I tried to get WB control document on Navigated and DocumentComplete event but always getting wb.Document as null.

So please help me to save the Excel from WB on button click.
I don't want to use ctrl+s option which we can use to save excel directly.
Posted
Updated 5-Dec-13 23:38pm
v3
Comments
Ziee-M 9-Dec-13 7:34am    
Hi, post some code so we can help you.
rajgaikwad01 10-Dec-13 0:25am    
Hi Ziee-M,

Thanks for your reply
Please see the below code:

private void eBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
try
{
Application.DoEvents();
HtmlDocument hDoc = eBrowser.Document;
}
catch (Exception)
{

throw;
}
}

but here I am getting hDoc as Null.
I want to read updated Excel file from WB control.
Is there any option to get Excel data in Data Table.

I tried to read data from Excel in Data table but I am getting only default data which was present in Excel sheet. But if I am updating Excel using WB control that changes we need.
Please see the below code which I have written to get data from Excel:

private DataTable GetExcelData(Excel.Workbook wb)
{
DataTable dTable = new DataTable();
DataColumn dColumn = null;
try
{
Excel.Worksheet wSheet = (Excel.Worksheet)wb.Worksheets.get_Item(1);
Excel.Range wRange = wSheet.UsedRange;
if (wRange.Columns.Count > 0)
{
for (int cCnt = 1; cCnt <= wRange.Columns.Count; cCnt++)
{
dColumn = new DataColumn();
dColumn.DataType = System.Type.GetType("System.String");
string cName = (string)(wRange.Cells[1, cCnt] as Excel.Range).Value2;
dColumn.ColumnName = cName; //cCnt.ToString();
dTable.Columns.Add(dColumn);
}

for (int rCnt = 2; rCnt <= wRange.Rows.Count; rCnt++)
{

DataRow dRow = dTable.NewRow();
int CellEnpty = 0;
for (int cCnt = 1; cCnt <= wRange.Columns.Count; cCnt++)
{
string sCellValue = "";
double ConvertVal = 0.0;
try
{
sCellValue = (string)(wRange.Cells[rCnt, cCnt] as Excel.Range).Value2;
}
catch (Exception)
{
ConvertVal = (double)(wRange.Cells[rCnt, cCnt] as Excel.Range).Value2; ;
sCellValue = ConvertVal.ToString();
}
if (sCellValue != null)
{
if (sCellValue != "")
{
dRow[cCnt - 1] = sCellValue;
}
else
{
CellEnpty += 1;
}
}
else
{
CellEnpty += 1;
}
}
if (CellEnpty < 3)
{
dTable.Rows.Add(dRow);
}
}

}
return dTable;
}
catch (Exception)
{

throw;
}
}
Ziee-M 10-Dec-13 3:21am    
hi, i dont think its a good idea to use Application.DoEvents(). if you have a long running loop, your application will crash sooner or later, you will have to use threads and synchornisation later.
From my expirence working with webbrowser, the WebBrowser.document is not very stable, i stopped working with it, but it should work fine in your case, try using DocumentText, you get a string variable. you can use methods like String.Substring, Replace, remove (Strings methods) to get your data.
If your Html code is clean, you will have no problem. althought i am not sure if this is the best solutions.
Don't hesitate to ask more questions.
Ziee-M 10-Dec-13 3:36am    
Also try to remove Application.DoEvents(); from there, its used in the wrong place. in general we use it to wait until the webpage is loaded successfully
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
again, its not a good idea to use it in long running loop.
in my case unhandeled errors started occuring after 5000 loop, so i switched to threads.
rajgaikwad01 10-Dec-13 4:42am    
Hi,

I tried with DocumentText but I am getting null value.
Only I am getting value for DocumentTitle property as "test.xlsx".
I have searched on Google for this issue so I saw the examples given with WB have Application.DoEvent so I tried same.

if I am checking "eBrowser.ReadyState" it always returns "Uninitialized".


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