java⽣成excel并导出到对应位置的⽅式⽬录
⽣成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;
mkdirs方法
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 = ateRow(0);
// 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何⼀个
HSSFCell cellOne = ateCell(0);
// 设置单元格内容
cellOne.setCellValue("账号");
HSSFCell cellTwo = ateCell(1);
// 设置单元格内容
cellTwo.setCellValue("⾦额");
/
/⾏数
int rowNum = 1;
//遍历hashmap
Iterator iterator = Set().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) ();
Object key = Key();
Object val = Value();
//创建⼀⾏⾏记录
rowNum++;
// 在sheet⾥创建下⼀⾏
HSSFRow newRow = ateRow(rowNum);
// 创建单元格并设置单元格内容
}
// 第六步,将⽂件存到指定位置
try {
String path = "F:/a/b.xlsx";
File file = new File(path);
//如果已经存在则删除
if (ists()) {
file.delete();
}
//检查⽗包是否存在
File parentFile = ParentFile();
if (!ists()) {
parentFile.mkdirs();
}
//创建⽂件
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 = SelectedFile().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 = SelectedFile().getAbsolutePath();
System.out.println("fileChooser.outFile:" + outFile);
// outFile可选择的路径。
} else {undefined
pathFlg = false;
}
return pathFlg;
}
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。