Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Web export file core code example

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "web export file core code example", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "web export file core code example" it!

Front-end code

1. You can write directly: location.href = "background request address"

two。 In XMLHttpRequest form (ajax axios fetch):

Axios ({url: url, params: parameter, method:'get' ResponseType: 'blob'}) .then (data= > {if (! data) {alert ("file download failed") return} if (typeof window.navigator.msSaveBlob! = =' undefined') {/ / process IE window.navigator.msSaveBlob (new Blob ([data])) FileName+'.xls')} else {let url = window.URL.createObjectURL (new Blob ([data])) let link = document.createElement ('a') link.style.display = 'none' link.href = url link.setAttribute (' download') FileName+'.xls') document.body.appendChild (link) link.click () document.body.removeChild (link) / / remove the element window.URL.revokeObjectURL (url) after download; / / release the blob object}}) background code / / download the file core code response.setContentType ("application/x-msdownload;charset=utf-8") from the server; String fileName= "Chinese file name .xls"; String userAgent = request.getHeader ("user-agent"). ToLowerCase () If (userAgent.contains ("msie") | | userAgent.contains ("like gecko") {fileName = URLEncoder.encode (fileName, "UTF-8");} else {fileName = new String (fileName.getBytes ("UTF-8"), "iso-8859-1");} response.setHeader ("Content-disposition", "attachment; filename=" + fileName) InputStream inputStream = null; OutputStream outputStream=null; try {String imgurl = "actual address of the file on the server"; inputStream = new BufferedInputStream (new FileInputStream (imgurl)); outputStream= response.getOutputStream (); byte [] buf = new byte [1024]; int len While ((len = inputStream.read (buf)) > 0) {outputStream.write (buf, 0, len);} response.flushBuffer ();} catch (Exception e) {logger.info ("- get file exception through stream -" + e.getMessage ()) } finally {if (inputStreamstreams null) {inputStream.close ();} if (outputStreamstreams invalid null) {outputStream.close () }} / / Export excel / / the code for generating workbook is omitted here String fileName = "Registration Certificate Statistics .xls"; String userAgent = req.getHeader ("user-agent") .toLowerCase () If (userAgent.contains ("msie") | | userAgent.contains ("like gecko")) {fileName = URLEncoder.encode (fileName, "UTF-8");} else {fileName = new String (fileName.getBytes ("UTF-8"), "iso-8859-1") } / / set mandatory download not to open resp.setContentType ("application/force-download"); resp.setHeader ("Content-disposition", "attachment; filename=" + fileName); OutputStream out = resp.getOutputStream (); workbook.write (out); resp.flushBuffer ()

An example of generating workbook

At this point, I believe that everyone on the "web export file core code examples" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report