java怎么生成excel并导出到对应位置
发表于:2024-10-07 作者:千家信息网编辑
千家信息网最后更新 2024年10月07日,本篇内容介绍了"java怎么生成excel并导出到对应位置"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所
千家信息网最后更新 2024年10月07日java怎么生成excel并导出到对应位置
本篇内容介绍了"java怎么生成excel并导出到对应位置"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
生成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() { Mapaccts = new HashMap () { { 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; }
"java怎么生成excel并导出到对应位置"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
单元
文件
路径
位置
生成
选择
一行
参数
对象
格内
之间
内容
实际
情况
文档
更多
知识
起始
实用
成功
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
汽车行业工具软件开发
实惠的仓库管理软件开发设计
服务器自动化维护工作流程
珠海手机软件开发价格
网络安全的学习内容
四川正规软件开发大概要多少钱
稳健安全的sql分布式数据库
甘肃运营网络技术服务哪家好
战地一武器被服务器锁定怎么解
郑州网络安全新闻
计算机网络和三级网络技术
网络安全怎么写英语作文
阿里云服务器 快照
数据库字典项目
计算机数据库和sql哪个好
oracle数据库表库空间
电脑可以自己做服务器吗
硕士从事软件开发有前途吗
数据库仪器价格
安全许可服务器是什么意思
开发微信小程序需要连数据库吗
怎么看电脑dns服务器地址
网络安全问题不会带来哪些损失
2021网络安全工作报告
坚果云服务器多大
安河桥吉他谱软件开发
十堰电子仓库软件开发
解析网络安全现状
国税有软件开发吗
数据库仪器价格