web表格用excel导出

简介: web表格用excel导出

网页上的表格有时需要导出存储。

需要用到java的一个工具包jxl.jar,地址https://download.csdn.net/download/bushqiang/10320406

 

1.网页上的table

<a href="export">导出表格</a>
  <table border="5">
    <tr>
      <th>Month</th>
      <th>money</th>
    </tr>
    <tr>
      <td>January</td>
      <td>10000</td>
    </tr>
    <tr>
      <td>February</td>
      <td>8000</td>
    </tr>
  </table>

2.后台的Servlet

  @RequestMapping("/export")
  public void export(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String path=export();
    System.out.println(path);
    response.sendRedirect("/contestForxls/" + path);
  }

3.java创建表格的方法

public static String export() {
    //生成xls的位置,要在webapps下,下载时才能访问的到
    final String exportPath = "D:\\OnlineJudge\\tomcat_normal\\webapps\\contestForxls\\";
    // 添加标题
    List<String> title = new ArrayList<String>();
    title.add("month");
    title.add("money");
    OutputStream os = null;
    // 输出的excel的路径
    String filePath = exportPath + "test.xls";
    try {
      //如果文件夹不存在就创建
      File baseFile = new File(exportPath);
      if (!baseFile.exists()) {
        baseFile.createNewFile();
      }
      //如果文件夹存在就删除
      File exportFile = new File(filePath);
      if (exportFile.exists()) {
        exportFile.delete();
      }
      // Excel单元格的一些样式
      WritableCellFormat wcCenter = new WritableCellFormat();
      wcCenter.setAlignment(Alignment.CENTRE);
      WritableCellFormat wcCenterRed = new WritableCellFormat();
      wcCenterRed.setAlignment(Alignment.CENTRE);
      wcCenterRed.setBorder(Border.ALL, BorderLineStyle.THIN);
      wcCenterRed.setBackground(jxl.format.Colour.RED);
      WritableCellFormat wcCenterGreen = new WritableCellFormat();
      wcCenterGreen.setAlignment(Alignment.CENTRE);
      wcCenterGreen.setBorder(Border.ALL, BorderLineStyle.THIN);
      wcCenterGreen.setBackground(jxl.format.Colour.GREEN);
      // 创建Excel工作薄
      WritableWorkbook wwb;
      // 新建立一个jxl文件,即生成.xls文件
      os = new FileOutputStream(filePath);
      wwb = Workbook.createWorkbook(os);
      // 添加第一个工作表并设置第一个Sheet的名字
      WritableSheet sheet = wwb.createSheet("test", 0);
      Label label;
      for (int i = 0; i < title.size(); i++) {
        // Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z
        // 在Label对象的子对象中指明单元格的位置和内容
        label = new Label(i, 0, title.get(i), wcCenter);
        // 将定义好的单元格添加到工作表中
        sheet.addCell(label);
      }
      // 下面往xls表格里面填充数据,是一行一行的添加
      int row = 1;// 行,第0行给标题占用了
      //第一行
      int col = 0;// 列
      label = new Label(col++, row, "January", wcCenter);
      sheet.addCell(label);
      label = new Label(col++, row, "10000", wcCenter);
      sheet.addCell(label);
      //第二行
      row++;
      col = 0;// 列
      label = new Label(col++, row, "February", wcCenter);
      sheet.addCell(label);
      label = new Label(col++, row, "8000", wcCenter);
      sheet.addCell(label);
      // 写入数据
      wwb.write();
      // 关闭文件
      wwb.close();
      int index = filePath.lastIndexOf('\\');
      if (index != -1) {
        filePath = filePath.substring(index + 1);
      }
      return filePath;
    } catch (Exception e) {
      System.out.println("导出错误!");
      e.printStackTrace();
      // return "导出错误!";
      return null;
    } finally {
      if (os != null) {
        try {
          os.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

4.第三步就直接可以创建一个excel表格了,生成的数据是写死的,项目中的一般都是从数据库中获取,前端表格也应该是从数据库中获取。效果图。

微信图片_20221213180150.png

微信图片_20221213180241.png

poi的方式:https://www.jianshu.com/p/dd1e4f28757b

https://www.cnblogs.com/staticking/p/9914042.html

相关文章
|
1月前
|
NoSQL 关系型数据库 MySQL
多人同时导出 Excel 干崩服务器?怎样实现一个简单排队导出功能!
业务诉求:考虑到数据库数据日渐增多,导出会有全量数据的导出,多人同时导出可以会对服务性能造成影响,导出涉及到mysql查询的io操作,还涉及文件输入、输出流的io操作,所以对服务器的性能会影响的比较大;结合以上原因,对导出操作进行排队; 刚开始拿到这个需求,第一时间想到就是需要维护一个FIFO先进先出的队列,给定队列一个固定size,在队列里面的人进行排队进行数据导出,导出完成后立马出队列,下一个排队的人进行操作;还考虑到异步,可能还需要建个文件导出表,主要记录文件的导出情况,文件的存放地址,用户根据文件列表情况下载导出文件。
多人同时导出 Excel 干崩服务器?怎样实现一个简单排队导出功能!
|
1月前
|
前端开发
Web前端---表格和表单
Web前端---表格和表单
27 1
|
1月前
|
存储 前端开发
web前端开发----------表格
web前端开发----------表格
47 0
|
2月前
|
关系型数据库 MySQL 区块链
将excel表格数据导入Mysql新建表中
将excel表格数据导入Mysql新建表中
excel判断表格中是否有重复值
excel判断表格中是否有重复值
|
3月前
|
Java Apache Spring
springboot如何导出Excel某个表的表字段以及字段类型
springboot如何导出Excel某个表的表字段以及字段类型
32 0
|
3月前
|
关系型数据库 MySQL 数据库连接
python查询数据库的某个表,将结果导出Excel
python查询数据库的某个表,将结果导出Excel
46 0
|
2月前
|
XML Java 数据格式
使用Freemarker模版导出xls文件使用excel打开提示文件损坏
使用Freemarker模版导出xls文件使用excel打开提示文件损坏
58 0
|
5天前
|
easyexcel 数据库
公司大佬对excel导入、导出的封装,那叫一个秒啊
封装公司统一使用的组件的主要目标是为了简化开发人员的调用流程,避免各个项目组重复集成和编写不规范的代码。文中提到对阿里EasyExcel进行了二次封装,提供了导入和导出功能,并支持模板的导入和导出。此外,还处理了读取数据与实际保存数据不一致的情况,通过提供自定义转换器来解决。
18 0