软件编程
位置:首页>> 软件编程>> java编程>> java生成excel并导出到对应位置的方式

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

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

标签: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);
           }
       };

// 创建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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com