基于SpringMvc的数据库表格导出

xiaoxiao2021-02-28  110

在项目开发过程中,用到了基于用户操作的数据库表格导出,记录下来,以备下次使用:

后台代码实现(基于SpringMvc):

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.chengpai.person.bean.Person; import com.chengpai.person.bean.bumenMidObject; import com.chengpai.person.service.PersonService; import util.Mobile; import util.Upload; @Controller public class PersonController { @RequestMapping(value = "testex.do", method = RequestMethod.GET) @ResponseBody public Mobile ll(HttpServletRequest request, HttpServletResponse response) { response.setContentType("text/html;charset=UTF-8"); Person p = new Person(); p.setId(0); p.setName("zhangsan"); p.setPassword("123"); Person p1 = new Person(); p1.setId(1); p1.setName("zhangsansan"); p1.setPassword("1234"); List<Person> list = new ArrayList<Person>(); list.add(p); list.add(p1); // 创建一个excel文件 HSSFWorkbook demoexcel = new HSSFWorkbook(); // 创建Sheet对象 HSSFSheet demoSheet = demoexcel.createSheet("用户列表1"); // 创建excel文件标题 String[] tableHeader = { "用户编号", "用户名", "用户密码" }; // 创建行的对象 HSSFRow row = demoSheet.createRow(0);// 参数为行下标,下标从0开始 // 创建单元格 for (int i = 0; i < tableHeader.length; i++) { HSSFCell cell = row.createCell(i);// 在一行中创建N个单元格,单元格下标从0开始 // 将标题信息添加入单元格 cell.setCellValue(tableHeader[i]); } for (int i = 0; i < list.size(); i++) { HSSFRow row1 = demoSheet.createRow(i + 1); HSSFCell cell1 = row1.createCell(0); cell1.setCellValue(list.get(i).getId()); HSSFCell cell2 = row1.createCell(1); cell2.setCellValue(list.get(i).getName()); HSSFCell cell3 = row1.createCell(2); cell3.setCellValue(list.get(i).getPassword()); } // 设置以网格的形式输出对象 demoSheet.setGridsPrinted(true); String filename = "aaa用户信息.xls"; try { response.addHeader("Content-DisPosition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 将内存中的对象写入到硬盘 try { OutputStream os = response.getOutputStream(); demoexcel.write(os); os.flush(); os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } boolean a = true; Mobile mo = null; if (a) { mo = new Mobile("1", "添加成功", null); } else { mo = new Mobile("0", "添加失败", null); } return mo; } }前台H5界面的实现,(ajax触发请求):

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="js/jquery.min.js"></script> <title>文件上传</title> <script type="text/javascript"> $(document).ready(function () { $("#btnImportOK").click(function () { var formData = new FormData($("#uploadForm")[0]); $.ajax({ type: "POST", data: formData, url: "testex.do", contentType: false, processData: false, }).success(function (data) { if (data.status) { console.log(data.url); } else { console.log(data.msg); } }).error(function (data) { alert(data); console.log(data); }); }); }); </script> </head> <body> <a href="testex.do" >下载</a> </body> </html> 打开网页,浏览器会自动打开文件保存路径。

转载请注明原文地址: https://www.6miu.com/read-37942.html

最新回复(0)