欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java生成excel并導出到對應位置的方式

 更新時間:2022年01月29日 10:57:31   作者:Burton_J  
這篇文章主要介紹了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() {
        Map<String, Integer> accts = new HashMap<String, Integer>() {
            {
                put("123456", 125);
                put("123451", 121);
                put("123457", 124);
                put("123459", 122); 
            }
        };
 
        // 創(chuàng)建HSSFWorkbook對象(excel的文檔對象)
        HSSFWorkbook wb = new HSSFWorkbook();
        // 建立新的sheet對象(excel的表單)
        HSSFSheet sheet = wb.createSheet("FXT");
        // 在sheet里創(chuàng)建第一行,參數(shù)為行索引(excel的行),可以是0~65535之間的任何一個
        HSSFRow row1 = sheet.createRow(0);
        // 創(chuàng)建單元格(excel的單元格,參數(shù)為列索引,可以是0~255之間的任何一個
        HSSFCell cellOne = row1.createCell(0);
        // 設置單元格內容
        cellOne.setCellValue("賬號");
        HSSFCell cellTwo = row1.createCell(1);
        // 設置單元格內容
        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)建單元格并設置單元格內容
            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 = "導出成功!";
            System.out.println(str);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
            String str1 = "導出失?。?;
            System.out.println(str1);
        }
        // 合并單元格CellRangeAddress構造參數(shù)依次表示起始行,截至行,起始列, 截至列
        //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 );
? ? ? ? ? ? // 然后根據(jù)實際情況去操作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;
? ? }

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論