java生成excel并導(dǎo)出到對(duì)應(yīng)位置的方式
更新時(shí)間:2022年01月29日 10:57:31 作者:Burton_J
這篇文章主要介紹了java生成excel并導(dǎo)出到對(duì)應(yīng)位置的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
生成excel并導(dǎo)出到對(duì)應(yīng)位置
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);
}
};
// 創(chuàng)建HSSFWorkbook對(duì)象(excel的文檔對(duì)象)
HSSFWorkbook wb = new HSSFWorkbook();
// 建立新的sheet對(duì)象(excel的表單)
HSSFSheet sheet = wb.createSheet("FXT");
// 在sheet里創(chuàng)建第一行,參數(shù)為行索引(excel的行),可以是0~65535之間的任何一個(gè)
HSSFRow row1 = sheet.createRow(0);
// 創(chuàng)建單元格(excel的單元格,參數(shù)為列索引,可以是0~255之間的任何一個(gè)
HSSFCell cellOne = row1.createCell(0);
// 設(shè)置單元格內(nèi)容
cellOne.setCellValue("賬號(hào)");
HSSFCell cellTwo = row1.createCell(1);
// 設(shè)置單元格內(nèi)容
cellTwo.setCellValue("金額");
//行數(shù)
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();
//創(chuàng)建一行行記錄
rowNum++;
// 在sheet里創(chuàng)建下一行
HSSFRow newRow = sheet.createRow(rowNum);
// 創(chuàng)建單元格并設(shè)置單元格內(nèi)容
newRow.createCell(0).setCellValue((String) key);
newRow.createCell(1).setCellValue((Integer) val);
}
// 第六步,將文件存到指定位置
try {
String path = "F:/a/b.xlsx";
File file = new File(path);
//如果已經(jīng)存在則刪除
if (file.exists()) {
file.delete();
}
//檢查父包是否存在
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
//創(chuàng)建文件
file.createNewFile();
FileOutputStream fout = new FileOutputStream(path);
wb.write(fout);
String str = "導(dǎo)出成功!";
System.out.println(str);
fout.close();
} catch (Exception e) {
e.printStackTrace();
String str1 = "導(dǎo)出失??!";
System.out.println(str1);
}
// 合并單元格CellRangeAddress構(gòu)造參數(shù)依次表示起始行,截至行,起始列, 截至列
//sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
}
}生成excel圖:

指定路徑導(dǎo)入導(dǎo)出文件
使用JFileChooser ,可以彈出對(duì)話框,然后選擇指定路徑上的文檔。
讀取指定路徑下的文件
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 );
? ? ? ? ? ? // 然后根據(jù)實(shí)際情況去操作input即可。
? ? ? ? }
? ? }將文件導(dǎo)出至指定路徑
? ? private boolean getOutputPath() {undefined
? ? ? ? boolean pathFlg = true;
? ? ? ? fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
? ? ? ? fileChooser.setDialogTitle("選擇文件導(dǎo)出的路徑");
? ? ? ? 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;
? ? }以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud Gateway的路由,過濾器和限流解讀
這篇文章主要介紹了SpringCloud Gateway的路由,過濾器和限流解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
spring-boot List轉(zhuǎn)Page的方法步驟
這篇文章主要介紹了spring-boot List轉(zhuǎn)Page的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Java用戶交互scanner及運(yùn)算結(jié)構(gòu)代碼詳解
這篇文章主要介紹了Java用戶交互scanner及運(yùn)算結(jié)構(gòu)代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
SpringBoot使用minio進(jìn)行文件管理的流程步驟
MinIO 是一個(gè)高性能的對(duì)象存儲(chǔ)系統(tǒng),兼容 Amazon S3 API,該軟件設(shè)計(jì)用于處理非結(jié)構(gòu)化數(shù)據(jù),如圖片、視頻、日志文件以及備份數(shù)據(jù)等,本文給大家介紹了SpringBoot使用minio進(jìn)行文件管理的流程步驟,需要的朋友可以參考下2025-01-01

