Cefsharp in Chrome for JS Export in Excel

Source: Internet
Author: User

Objective

 Wandering in the blog park for more than a year, usually is to learn from the success of others experience, always feel that they should share something, but suffering from their own limited technology, usually do not love to write things, so has not written anything. Graduation more than a year, in the real work encountered a variety of problems, deeply experienced the problem when the anxiety and tangled. Today decided to write your first reality of the problems encountered. Hope to be able to provide some help to those who encounter similar problems. The article may not write well, do not spray!! 

Background

Using JS to <table> data in Excel is not a novelty, in IE often use ActiveXObject to create Excel.Application objects to handle. But Chrome does not support the creation of ActiveXObject object way to handle, so someone in chrome with some kind of JS to process the Export page table data, the specific way to see the Mountain-dimensional space blog .

Environment

First of all, the background of their participation in the project, the project structure is to adopt the B/S and C/s hybrid architecture. A business implementation is a Java implementation that can be accessed through a browser, but a subset of the functionality must be used. NET implementation. So the use of. NET to do the client, using the embedded browser in the client's way to achieve the overall function. The client has started using WebBrowser as an inline browser, but to be honest, ie's performance is poor, and compatibility is not good. So finally decided to use the WebKit kernel browser. Finally, Cefsharp was selected.

"Experiencing problems"

If the reader does not see the space of the Mountain Space blog , it is recommended to read the blog before continuing to look at the following.

Using the JS implementation described in the blog, Chrome enables you to export pages <table> tabular data as Excel. But there was no reaction in the cefsharp. Trace Debug found js has executed location.href = uri + base64 (template (Tables)), do not have any action after completion. The execution of the Onbeforeresourceload method in Cefsharp can only be performed to the RequestHandler control class is finished.

The specific reason is unknown.

Solution

View the RequestResponse parameter of the Onbeforeresourceload method to find the URL is the last data we passed. Finally, the following actions are made to solve the problem.

To implement the Onbeforebrowse method, do Excel export in this method:

1   Public BOOLOnbeforebrowse (Iwebbrowser Browser, irequest request, Navigationtype Naigationvtype,BOOLisredirect)2         {3               if(URL.) Contains ("application/vnd.ms-excel;base64"))4             {5                 stringtmpcontent = URL;//get the contents of the file delivered6                 stringContenthead ="Data:application/vnd.ms-excel;base64,";7                 intStartIndex =Tmpcontent.indexof (contenthead);8                 intName_startindex = Tmpcontent.indexof (contenthead) +contenthead.length;9                 intName_endindex = Tmpcontent.indexof ('#');Ten                 stringFileName ="Excel Table"; One                 if(Name_endindex! =-1) A                 { -FileName = uri.unescapedatastring (tmpcontent.substring (Name_startindex, Name_endindex-name_startindex)); -Tmpcontent = tmpcontent.substring (Name_endindex +1); the                 } -                 Else -                 { -Tmpcontent =tmpcontent.substring (name_startindex); +                 } -                 byte[] Output =convert.frombase64string (tmpcontent); +SaveFileDialog Dialog =NewSaveFileDialog (); ADialog. FileName = filename+". xls"; atDialog. Filter ="(Excel file) |*.xls"; -DialogResult result =dialog. ShowDialog (); -                 if(Result = =DialogResult.OK) -                 { -                     using(FileStream fs =NewFileStream (dialog. FileName, FileMode.Create, FileAccess.Write)) -                     { inFs. Write (Output,0, output. Length); - FS. Flush (); to                     } +                     return true; -                 } the             } *             return false; $}

So that the function can be implemented normally. If you have a better way to achieve, welcome to communicate together!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.