首页 热点资讯 义务教育 高等教育 出国留学 考研考公

jsp 中Excel的读写操作原理和实现方式是什么?

发布网友 发布时间:2022-04-22 02:38

我来回答

1个回答

热心网友 时间:2022-05-17 02:23

1.创建excel文件<br>//这里的jxl不是java的标准jar包,需要在项目中另外加载import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class ExcelDownload extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 生成xls try { Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_kkmmss "); String random = sdf.format(d); String targetFile = random + ".excel"; response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment; filename=\"" + targetFile + "\""); OutputStream os = response.getOutputStream(); WritableWorkbook wwb = Workbook.createWorkbook(os); // 新建一张表 WritableSheet wsheet = wwb.createSheet("record", 0); // 设置表头 Label label = new Label(0, 0, ""); wsheet.addCell(label); label = new Label(0, 0, "会员姓名"); wsheet.addCell(label); label = new Label(1, 0, "卡号"); wsheet.addCell(label); label = new Label(2, 0, "联系地址"); wsheet.addCell(label); label = new Label(3, 0, "邮编"); wsheet.addCell(label); label = new Label(4, 0, "联系电话"); wsheet.addCell(label); label = new Label(5, 0, "手机"); wsheet.addCell(label); label = new Label(6, 0, "Email"); wsheet.addCell(label); label = new Label(7, 0, "性别"); wsheet.addCell(label); wwb.write(); wwb.close(); os.close(); response.flushBuffer(); } catch (Exception e) { System.out.println("生成信息表(Excel格式)时出错:"); e.printStackTrace(); } } }

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com