java生成excel并导出到对应位置的方式

作者:Burton_J 时间:2021-12-30 16:05:14 

生成excel并导出到对应位置

package tech.BurtonPratice;
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.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@RunWith(JUnit4.class)
public class PoiExcel {
   @Test
   public void exportExcel() {
       Map<String, Integer> accts = new HashMap<String, Integer>() {
           {
               put("123456", 125);
               put("123451", 121);
               put("123457", 124);
               put("123459", 122);
           }
       };

// 创建HSSFWorkbook对象(excel的文档对象)
       HSSFWorkbook wb = new HSSFWorkbook();
       // 建立新的sheet对象(excel的表单)
       HSSFSheet sheet = wb.createSheet("FXT");
       // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
       HSSFRow row1 = sheet.createRow(0);
       // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
       HSSFCell cellOne = row1.createCell(0);
       // 设置单元格内容
       cellOne.setCellValue("账号");
       HSSFCell cellTwo = row1.createCell(1);
       // 设置单元格内容
       cellTwo.setCellValue("金额");

//行数
       int rowNum = 1;
       //遍历hashmap
       Iterator iterator = accts.entrySet().iterator();
       while (iterator.hasNext()) {
           Map.Entry entry = (Map.Entry) iterator.next();
           Object key = entry.getKey();
           Object val = entry.getValue();
           //创建一行行记录
           rowNum++;
           // 在sheet里创建下一行
           HSSFRow newRow = sheet.createRow(rowNum);
           // 创建单元格并设置单元格内容
           newRow.createCell(0).setCellValue((String) key);
           newRow.createCell(1).setCellValue((Integer) val);
       }

// 第六步,将文件存到指定位置
       try {
           String path = "F:/a/b.xlsx";
           File file = new File(path);
           //如果已经存在则删除
           if (file.exists()) {
               file.delete();
           }
           //检查父包是否存在
           File parentFile = file.getParentFile();
           if (!parentFile.exists()) {
               parentFile.mkdirs();
           }
           //创建文件
           file.createNewFile();
           FileOutputStream fout = new FileOutputStream(path);
           wb.write(fout);
           String str = "导出成功!";
           System.out.println(str);
           fout.close();
       } catch (Exception e) {
           e.printStackTrace();
           String str1 = "导出失败!";
           System.out.println(str1);
       }
       // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
       //sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));  
   }
}

生成excel图:

java生成excel并导出到对应位置的方式

指定路径导入导出文件

使用JFileChooser ,可以弹出对话框,然后选择指定路径上的文档。

读取指定路径下的文件

private JFileChooser fileChooser = new JFileChooser(".");
private void getInputFile() throws Exception {undefined
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setDialogTitle("选择输入Excel文件");
        int ret = fileChooser.showOpenDialog(null);
        if (ret == JFileChooser.APPROVE_OPTION) {undefined
            File inputFile = fileChooser.getSelectedFile().getAbsoluteFile();
            FileInputStream input = new FileInputStream(inputFile );
            // 然后根据实际情况去操作input即可。
        }
    }

将文件导出至指定路径

    private boolean getOutputPath() {undefined
        boolean pathFlg = true;
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setDialogTitle("选择文件导出的路径");
        int ret = fileChooser.showOpenDialog(null);
        if (ret == JFileChooser.APPROVE_OPTION) {undefined
            String outFile = fileChooser.getSelectedFile().getAbsolutePath();
            System.out.println("fileChooser.outFile:" + outFile);
           // outFile可选择的路径。 
        } else {undefined
            pathFlg = false;
        }
        return pathFlg;
    }

来源:https://blog.csdn.net/weixin_37739042/article/details/88797101

标签:java,生成excel,导出,位置
0
投稿

猜你喜欢

  • Java获得当前时间前指定几个小时具体时间的方法示例

    2023-11-24 04:56:49
  • Android项目开发之UI设计器

    2022-03-26 14:28:32
  • UnityShader使用图像叠加实现运动模糊

    2021-09-24 14:45:16
  • Spring Boot启动过程(四)之Spring Boot内嵌Tomcat启动

    2023-09-21 00:16:18
  • IDEA设置maven修改settings.xml配置文件无法加载仓库的解决方案

    2023-08-23 22:39:35
  • Java枚举类使用Lombok方式

    2022-06-28 22:07:02
  • 浅谈为什么要使用mybatis的@param

    2023-07-01 20:12:39
  • c# 类成员的可访问性代码详解

    2022-03-14 22:43:59
  • 基于集合的子集与集合的全排列的相关问题

    2023-09-23 07:03:46
  • SpringBoot如何进行对象复制的实践

    2023-11-23 03:40:19
  • SpringBoot如何优雅的整合Swagger Api自动生成文档

    2022-08-10 00:49:29
  • Java编程探索之泛型擦除实例解析

    2022-08-30 02:13:35
  • java实现读取、删除文件夹下的文件

    2021-12-06 20:07:48
  • Java数据结构 递归之迷宫回溯案例讲解

    2023-04-01 11:16:38
  • Spring注解驱动开发实现属性赋值

    2023-05-07 04:40:22
  • Java多线程yield心得分享

    2023-11-29 05:58:15
  • 解决IDEA鼠标点击光标变大问题

    2022-12-07 11:52:37
  • Java类的继承实例详解(动力节点Java学院整理)

    2023-01-28 13:19:31
  • 关于cron表达式每天整点执行一次的问题

    2023-06-24 13:51:27
  • springsecurity 企业微信登入的实现示例

    2023-06-16 16:39:35
  • asp之家 软件编程 m.aspxhome.com