利用JfileChooser javaswing类库 进行调用弹出的窗体 选择文件所在的路径。
JFileChooser file = ExcelUtil.getFile();
// 判断是否关闭或取消保存框
if (file != null) {
// 的到保存路径
return file.getSelectedFile().getAbsolutePath() + ".xls";
}
public static JFileChooser getFile() {
// 默认打开D盘
JFileChooser file = new MyChooser("D:/");
// 下面这句是去掉显示所有文件这个过滤器。
file.setAcceptAllFileFilterUsed(false);
// 添加excel文件的过滤器
file.addChoosableFileFilter(new ExcelFileFilter("xls"));
int result = file.showSaveDialog(null);
// JFileChooser.APPROVE_OPTION是个整型常量,代表0。就是说当返回0的值我们才执行相关操作,否则什么也不做。
if (result == JFileChooser.APPROVE_OPTION) {
// 获得你选择的文件绝对路径。并输出。当然,我们获得这个路径后还可以做很多的事。
String path = file.getSelectedFile().getAbsolutePath();
System.out.println(path);
} else {
file = null;
System.out.println("你已取消并关闭了窗口!");
}
return file;
}
// 文件过滤器 只保存xls文件
private static class ExcelFileFilter extends FileFilter {
String ext;
ExcelFileFilter(String ext) {
this.ext = ext;
}
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String fileName = f.getName();
int index = fileName.lastIndexOf('.');
if (index > 0 && index < fileName.length() - 1) {
String extension = fileName.substring(index + 1).toLowerCase();
if (extension.equals(ext))
return true;
}
return false;
}
public String getDescription() {
if (ext.equals("xls")) {
return "Microsoft Excel文件(*.xls)";
}
return "";
}
}
private static class MyChooser extends JFileChooser {
private static final long serialVersionUID = 1L;
MyChooser(String path) {
super(path);
}
public void approveSelection() {
File file = this.getSelectedFile();
if (file.exists()) {
int copy = JOptionPane.showConfirmDialog(null, "是否要覆盖当前文件?", "保存", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (copy == JOptionPane.YES_OPTION)
super.approveSelection();
} else
super.approveSelection();
}
}
2.利用poi的机制对excel进行注入编写。、、
再导入之前要注意区分开可能别人导入格式可能跟自己的不一样。其中.xls 和.xlsx 结尾的。
List> excelList = Excel.findProductNumberExcel(filePath);
private static List> findProductNumberExcelXlsx(String path) {
List> list = new ArrayList>();
try {
// SXSSFWorkbook XSSFWorkbook
// XSSFWorkbook xwb = new XSSFWorkbook(path);
FileInputStream file = new FileInputStream(path);
// CreationHelper ch=new CreationHelper()
XSSFWorkbook xwb = new XSSFWorkbook(file);
// 读取第一章表格内容
XSSFSheet sheet = xwb.getSheetAt(0);
// 定义 row、cell
XSSFRow row;
// 循环输出表格中的内容
for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
List strList = new ArrayList();
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
XSSFCell ce = row.getCell(j);
ce.setCellType(HSSFDataFormat.getBuiltinFormat("0"));
strList.add(ce.toString());
}
list.add(strList);
}
file.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return list;
}
/**
*
*
* @param path
* @return
*/
private static List> findProductNumberExcelXls(String path) {
List> list = new ArrayList>();
try {
Workbook book = Workbook.getWorkbook(new File(path));
Sheet sheet[] = book.getSheets();// 得到所有Excel中页的列表.
for (int i = 0; i < sheet[0].getRows(); i++) {
List strList = new ArrayList();
for (int j = 0; j < sheet[0].getColumns(); j++) {
strList.add(sheet[0].getCell(j, i).getContents());
}
list.add(strList);
}
book.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return list;
}
public static List> findProductNumberExcel(String path) {
if (getExcelStyle(path).intValue() == 1) {
return findProductNumberExcelXls(path);
} else if (getExcelStyle(path).intValue() == 2)
// return findProductNumberExcelXls(path);
return findProductNumberExcelXlsx(path);
else
return null;
}
/**
* 判断选择的excl文件格式
*
* @param path
* @return 1.97-2003格式Excel文件 2.2007+新格式Excel文件
*/
private static Integer getExcelStyle(String path) {
int result = 0;
if (!StringUtils.isBlank(path)) {
String str = path.substring(path.lastIndexOf("."), path.length());
if (".xls".equals(str))
result = 1;
else if (".xlsx".equals(str))
result = 2;
}
return result;
}、
下面就可以进行导入excel对excel进行操作。
public static Boolean saveToExcel(List list,String path,List excelTitle,String[] excelTitleName, String sheetName) { // Page pageResult = searchSector(page, search); // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet(sheetName); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 HSSFCell cell = row.createCell((short) 0); if (excelTitle != null) { for (int i = 0; i < excelTitle.size(); i++) { cell.setCellValue(excelTitleName[excelTitle.get(i)]); cell.setCellStyle(style); cell = row.createCell((short) i + 1); } } // 第五步,写入实体数据 实际应用中这些数据从数据库得到, int i = 0; for (Map strList : list) { row = sheet.createRow((int) i + 1); for (int j = 0; j < excelTitle.size(); j++) { if(strList.get("name_" + j)!=null){ // 第四步,创建单元格,并设置值 if (strList.get("name_" + j).getClass().equals(Integer.class)) { Integer s = (Integer) strList.get("name_" + j); row.createCell((short) j).setCellValue((Integer) s); } else { String s = (String) strList.get("name_" + j); row.createCell((short) j).setCellValue((String) s); } } } i++; } // 第六步,将文件存到指定位置 //try { //OutputStream fOut = resp.getOutputStream(); //wb.write(fOut); //fOut.flush(); //fOut.close(); //return true; // //} catch (Exception e) { //e.printStackTrace(); //return false; //} FileOutputStream fileoutputstream = null; try { FileOutputStream fout = new FileOutputStream(path); wb.write(fout); fout.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } //return null; }