Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of exporting Excel by Java

Shulou Source: shulou.com Published: 2022-06-02 09:52:29 10月03日 Update

This article will explain in detail how to export Excel from Java. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Just wrote an import Excel table, now to write an export, in fact, the form is similar, nonsense, paste the code

Export

Here, according to individual needs, I write time settings here, and request export according to conditions. Here, it varies from person to person.

ExportData () {/ / determine whether the user has entered the operation type if (this.type! = "") {/ / default all operation types. No parameter this.param ["code"] = this.code is passed. } / / determine whether the user has filled in the start date if (this.startDate! ='') {this.param ['statrDate'] = new Date (this.startDate) } / / determine whether the user has filled in the end date if (this.endDate! ='') {this.param ['endDate'] = new Date (this.endDate);} this.param [this.optionSelected] = this.param.searchText; var url = "${ctx} / operatelog/exportOperateLog" Window.open (url);}

The Controller layer in java accepts requests, encapsulates the data queried by the database, and then outputs it using ExcelUtils.

RequestMapping ("/ exportOperateLog") @ ResponseBody public void exportOperateLog (HttpServletRequest request, HttpServletResponse response, OperateLogParam param) {SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyyMMddHHmmss"); try {response.reset (); response.setContentType ("text/html;charset=GBK"); / / set the type of response content response.setHeader ("Content-disposition", "attachment;filename=SysteamLog" + dateFormat.format (new Date ()) + ".xls;") String strTableName = "system log"; / / this is the title List headers = new ArrayList (); headers.add ("serial number"); / / the header here can be named according to the field of the data, as you like headers.add ("operation date and time"); headers.add ("operation type") Headers.add ("data 1"); headers.add ("data 2"); headers.add ("operator id"); headers.add ("operator"); List operateLogList = operateLogService.findOperateLogList (param); List operateListStr = new ArrayList (); for (int I = 0; I < operateLogList.size ()) ITunes +) {OperateLogParam opeLogPar = operateLogList.get (I) String [] strarr = new String [] {(StringUtils.isEmpty (opeLogPar.getOperateLogId (). ToString ())? "-": opeLogPar.getOperateLogId (). ToString ()), (StringUtils.isEmpty (opeLogPar.getCreateTime (). ToString ())? "-": opeLogPar.getCreateTime (). ToString ()), (StringUtils.isEmpty (opeLogPar.getTitle ())? "-": opeLogPar.getTitle ()) (StringUtils.isEmpty (opeLogPar.getData1 ())? "-": opeLogPar.getData1 ()), (StringUtils.isEmpty (opeLogPar.getData2 ())? ": opeLogPar.getData2 ()), (StringUtils.isEmpty (opeLogPar.getAdminId (). ToString ())?": opeLogPar.getAdminId (). ToString ()) (StringUtils.isEmpty (opeLogPar.getOperatorName ())? "-": opeLogPar.getOperatorName (),} OperateListStr.add (strarr);} ExcelUtil.createExcel (strTableName, headers, operateListStr, response.getOutputStream ());} catch (IOException e) {e.printStackTrace ();}

The ExcelUtils in here is in my other note.

Excel import function

Https://www.yisu.com/article/121533.htm

Export effect

On how to achieve Java export Excel function to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Data type function content date user operator article time more knowledge article yes vary from person to person almost personal code field serial number nonsense Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB Redmi MySQL Linux