老师,我点击导出后,下载的文件没有后缀名,打开直接损坏了!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | package com.imooc.servlet; import com.imooc.entity.Student; import com.imooc.service.ExcelService; import org.apache.poi.ss.usermodel.Workbook; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @WebServlet (name = "ExportExcelServlet" , urlPatterns = "/exportExcel" ) public class ExportExcelServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ExcelService excelService = new ExcelService(); List<Student> studentList = excelService.redExcel( "c:/upload/Student.xlsx" ); Workbook workbook = excelService.exportExcel( false , studentList); response.setHeader( "Context-Disposition" , "attachment;filename=Students.xlsx" ); ServletOutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); workbook.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } } package com.imooc.service; import com.imooc.dto.ImportExcelParamDto; import com.imooc.entity.Student; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; public class ExcelService { /** * 读取上传的Excel文件 * * @param dto(上传的参数对象) * @return 返回上传文件中的数据对象 */ public List<Student> redExcel(ImportExcelParamDto dto) { List<Student> impStudentList = new ArrayList<>(); Workbook workbook = null ; try { //获取上传的Excel文件输入流,并将其传给workbook workbook = WorkbookFactory.create(dto.getFile().getInputStream()); //获得Excel文件的第一个sheet Sheet sheet = workbook.getSheetAt( 0 ); //获得Sheet下的最后一个有效行 int rowNum = sheet.getLastRowNum(); //循环遍历所有有效行数中的单元格,获得单元格中的数据 for ( int i = 1 ; i <= rowNum; i++) { Row row = sheet.getRow(i); String name = row.getCell( 0 ).getStringCellValue(); int age = ( int ) row.getCell( 1 ).getNumericCellValue(); Date date = row.getCell( 2 ).getDateCellValue(); //将获取的数据封装成对象 Student st = new Student(name, age, date); //将封装的对象添加到List中 impStudentList.add(st); } } catch (IOException e) { e.printStackTrace(); } if (workbook != null ) { try { workbook.close(); } catch (IOException e) { e.printStackTrace(); } } return impStudentList; } /** * 读取服务器本地的Excel文件 * * @param FILE_PATH(服务器本地文件路径) * @return 读取到的数据对象集合 */ public List<Student> redExcel(String FILE_PATH) { List<Student> stList = new ArrayList<>(); Workbook workbook = null ; try { //获取上传的Excel文件输入流,并将其传给workbook workbook = WorkbookFactory.create( new File(FILE_PATH)); //获得Excel文件的第一个sheet Sheet sheet = workbook.getSheetAt( 0 ); //获得Sheet下的最后一个有效行 int rowNum = sheet.getLastRowNum(); //循环遍历所有有效行数中的单元格,获得单元格中的数据 for ( int i = 1 ; i <= rowNum; i++) { Row row = sheet.getRow(i); String name = row.getCell( 0 ).getStringCellValue(); int age = ( int ) row.getCell( 1 ).getNumericCellValue(); Date date = row.getCell( 2 ).getDateCellValue(); //将获取的数据封装成对象 Student st = new Student(name, age, date); //将封装的对象添加到List中 stList.add(st); } } catch (IOException e) { e.printStackTrace(); } finally { if (workbook != null ) { try { workbook.close(); } catch (IOException e) { e.printStackTrace(); } } } return stList; } /** * 将读取到的上传数据写入到服务器本地的Excel * * @param FILE_PATH 服务器本地Excel路劲 * @param impStudentList 要写入到Excel中的数据 */ public Workbook writeExcel(String FILE_PATH, List<Student> impStudentList) { Workbook workbook = null ; InputStream fileInputStream = null ; if (impStudentList != null ) { try { fileInputStream = new FileInputStream( new File(FILE_PATH)); workbook = WorkbookFactory.create(fileInputStream); Sheet sheet = workbook.getSheetAt( 0 ); int lastRowNum = sheet.getLastRowNum(); int index = 0 ; for ( int i = lastRowNum + 1 ; i <= lastRowNum + impStudentList.size(); i++) { Row row = sheet.createRow(i); Student st = impStudentList.get(index); row.createCell( 0 ).setCellValue(st.getName()); row.createCell( 1 ).setCellValue(st.getAge()); row.createCell( 2 ).setCellValue(st.getDate()); index++; } } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null ) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } return workbook; } /** * 创建新的Excel提供下载 * @param excelVersion excel版本 * @param impStudentList 要写到Excel中的数据 * @return Workbook对象 */ public Workbook exportExcel( boolean excelVersion, List<Student> impStudentList) { Workbook workbook = null ; if (excelVersion) { workbook = new XSSFWorkbook(); } else { workbook = new HSSFWorkbook(); } Sheet sheet = workbook.createSheet(); int lastRowNum = sheet.getLastRowNum(); int index = 0 ; for ( int i = lastRowNum + 1 ; i <= lastRowNum + impStudentList.size(); i++) { Row row = sheet.createRow(i); Student st = impStudentList.get(index); row.createCell( 0 ).setCellValue(st.getName()); row.createCell( 1 ).setCellValue(st.getAge()); row.createCell( 2 ).setCellValue(st.getDate()); index++; } return workbook; } } |
我的需求是这样的,在页面点击导出Excel的按钮,跳转到导出界面,界面显示的是我服务器本地一个Excel中的数据,然后点击“导出”,后台去服务器本地查找到这个Excel文件然后将他的写入一个Excel文件中(这个新建的Excel不在本地进行保存)然后将这个Excel文件提供用户下载!
问题,我下载出来的文件没有后缀名,而且文件名和我自己定义的也不一样,请老师帮忙看看!
12
收起
正在回答
2回答
同学这里传的是false
创建的是xls的呢
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧