Code function :

 

At the front end jsp There is a link or button on the page , Click to enter Controller Interface of layer , Then pop up a pop-up box to download the exported Excel, Finally, this excel Download the form to the specified path of the client !

tool :IDEA

pom.xml Add dependency :
        <dependency>             <groupId>com.alibaba</groupId>            
<artifactId>fastjson</artifactId>             <version>1.2.54</version>        
</dependency>         <dependency>            
<groupId>org.apache.commons</groupId>            
<artifactId>commons-lang3</artifactId>             <version>3.4</version>      
  </dependency>        <dependency>            
<groupId>org.apache.poi</groupId>             <artifactId>poi</artifactId>    
        <version>3.16</version>         </dependency>         <dependency>    
        <groupId>org.apache.poi</groupId>            
<artifactId>poi-ooxml</artifactId>             <version>3.16</version>        
</dependency>         <dependency>            
<groupId>commons-beanutils</groupId>            
<artifactId>commons-beanutils</artifactId>             <version>1.9.3</version>
        </dependency>
 

 
 
Controller Layer interface code :

 

 

 

 
@RequestMapping("/download") public void downstudents(HttpServletRequest
request, HttpServletResponse response,@RequestParam String startTime,
@RequestParam String endTime)throws IOException {
// I query the data in the database according to the starting time from the front end , If there is no input variable requirement , Just keep the first two String[] headers = { "ID", " theme ",
" full name ", " mobile phone "," Creation time "," start time "," End time "};// Exported Excel head , This should be changed according to our own project List dataset =
videoMeetInfoMapper.selectByTime(startTime,endTime);// Query data , Change it according to your own project
// The bottom one just doesn't move at all (Excel There are no pictures in the data ) // Declare a workbook HSSFWorkbook workbook = new
HSSFWorkbook(); // Generate a table HSSFSheet sheet = workbook.createSheet(); //
Set the default column width of the table to 15 Bytes sheet.setDefaultColumnWidth((short) 18); HSSFRow row =
sheet.createRow(0); for (short i = 0; i < headers.length; i++) { HSSFCell cell
= row.createCell(i); HSSFRichTextString text = new
HSSFRichTextString(headers[i]); cell.setCellValue(text); } // Traversing set data , Generate data rows
Iterator it = dataset.iterator(); int index = 0; while (it.hasNext()) {
index++; row = sheet.createRow(index); demo t = (demo) it.next();
// Using reflection , according to javabean Order of attributes , Dynamic call getXxx() Method to get the attribute value Field[] fields =
t.getClass().getDeclaredFields(); for (short i = 0; i < fields.length; i++) {
HSSFCell cell = row.createCell(i); Field field = fields[i]; String fieldName =
field.getName(); String getMethodName = "get" + fieldName.substring(0,
1).toUpperCase() + fieldName.substring(1); try { Class tCls = t.getClass();
Method getMethod = tCls.getMethod(getMethodName, new Class[]{}); Object value =
getMethod.invoke(t, new Object[]{}); String textValue = null; if (value
instanceof Date) { Date date = (Date) value; SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); textValue = sdf.format(date); } else {
// Other data types are treated simply as strings textValue = value.toString(); } HSSFRichTextString
richString = new HSSFRichTextString(textValue); HSSFFont font3 =
workbook.createFont(); font3.setColor(HSSFColor.BLUE.index);// definition Excel Data color
richString.applyFont(font3); cell.setCellValue(richString); } catch
(SecurityException e) { // TODO Auto-generated catch block e.printStackTrace();
} catch (NoSuchMethodException e) { // TODO Auto-generated catch block
e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO
Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException
e) { // TODO Auto-generated catch block e.printStackTrace(); } catch
(InvocationTargetException e) { // TODO Auto-generated catch block
e.printStackTrace(); } } } response.setContentType("application/octet-stream");
response.setHeader("Content-disposition",
"attachment;filename=createList.xls");// default Excel name response.flushBuffer();
workbook.write(response.getOutputStream()); } dataset =
videoMeetInfoMapper.selectByTime(startTime,endTime);// Query data , Change it according to your own project
// The bottom one just doesn't move at all (Excel There are no pictures in the data ) // Declare a workbook HSSFWorkbook workbook = new
HSSFWorkbook(); // Generate a table HSSFSheet sheet = workbook.createSheet(); //
Set the default column width of the table to 15 Bytes sheet.setDefaultColumnWidth((short) 18); HSSFRow row =
sheet.createRow(0); for (short i = 0; i < headers.length; i++) { HSSFCell cell
= row.createCell(i); HSSFRichTextString text = new
HSSFRichTextString(headers[i]); cell.setCellValue(text); } // Traversing set data , Generate data rows
Iterator it = dataset.iterator(); int index = 0; while (it.hasNext()) {
index++; row = sheet.createRow(index); demo t = (demo) it.next();
// Using reflection , according to javabean Order of attributes , Dynamic call getXxx() Method to get the attribute value Field[] fields =
t.getClass().getDeclaredFields(); for (short i = 0; i < fields.length; i++) {
HSSFCell cell = row.createCell(i); Field field = fields[i]; String fieldName =
field.getName(); String getMethodName = "get" + fieldName.substring(0,
1).toUpperCase() + fieldName.substring(1); try { Class tCls = t.getClass();
Method getMethod = tCls.getMethod(getMethodName, new Class[]{}); Object value =
getMethod.invoke(t, new Object[]{}); String textValue = null; if (value
instanceof Date) { Date date = (Date) value; SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); textValue = sdf.format(date); } else {
// Other data types are treated simply as strings textValue = value.toString(); } HSSFRichTextString
richString = new HSSFRichTextString(textValue); HSSFFont font3 =
workbook.createFont(); font3.setColor(HSSFColor.BLUE.index);// definition Excel Data color
richString.applyFont(font3); cell.setCellValue(richString); } catch
(SecurityException e) { // TODO Auto-generated catch block e.printStackTrace();
} catch (NoSuchMethodException e) { // TODO Auto-generated catch block
e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO
Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException
e) { // TODO Auto-generated catch block e.printStackTrace(); } catch
(InvocationTargetException e) { // TODO Auto-generated catch block
e.printStackTrace(); } } } response.setContentType("application/octet-stream");
response.setHeader("Content-disposition",
"attachment;filename=createList.xls");// default Excel name response.flushBuffer();
workbook.write(response.getOutputStream()); }
 

Browser input link test :
         localhost:8080/download?startTime=2017-07-26
8:57:59&endTime=2018-08-28 18:57:59( Whether there are parameters depends on the project situation )

 

The screenshot is shown below , You can download the Excel To client :

 

 

                                                                               

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network