广告
返回顶部
首页 > 资讯 > 后端开发 > Python >java生成excel并导出到对应位置的方式
  • 352
分享到

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

java生成excel导出对应位置java导出excel 2022-11-13 01:11:05 352人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录生成excel并导出到对应位置指定路径导入导出文件读取指定路径下的文件将文件导出至指定路径生成excel并导出到对应位置 package tech.BurtonPratice;

生成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图:

指定路径导入导出文件

使用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;
    }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: java生成excel并导出到对应位置的方式

本文链接: https://www.lsjlt.com/news/138022.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • java生成excel并导出到对应位置的方式
    目录生成excel并导出到对应位置指定路径导入导出文件读取指定路径下的文件将文件导出至指定路径生成excel并导出到对应位置 package tech.BurtonPratice; ...
    99+
    2022-11-13
    java生成excel 导出对应位置 java导出excel
  • java怎么生成excel并导出到对应位置
    本篇内容介绍了“java怎么生成excel并导出到对应位置”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!生成excel并导出到对应位置pac...
    99+
    2023-06-29
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作