日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java swing 导出文件_java swing (一) 导出excel文件并打开

發布時間:2023/12/31 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java swing 导出文件_java swing (一) 导出excel文件并打开 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

點擊XXX管理系統中的“導出Excel”按鈕,然后彈出如上圖,點擊“保存”以后,該Excel就保存到指定路徑,并且打開。

上述的動作,其實不難,主要是打開該文件時的路徑問題。

下面是我的一些構想和實現,僅此記錄。

1 . JFileChooser獲取導出的路徑path,然后裝載數據,裝載的代碼我就不貼出了,這不是重點。

public String selectSavePath(){

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd_HHmmss");

String name = dateformat.format(new Date());

name = name + ".xls";

//構造文件保存對話框

JFileChooser chooser = new JFileChooser();

chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

chooser.setDialogType(JFileChooser.SAVE_DIALOG);

chooser.setMultiSelectionEnabled(false);

chooser.setAcceptAllFileFilterUsed(false);

chooser.setDialogTitle("保存單位數據文件");

//取得文件名輸入框冰設置指定格式

JTextField fileNameField = getTextField(chooser);

fileNameField.setText(name);

//添加文件過濾器

chooser.addChoosableFileFilter(new FileFilter(){

public boolean accept(File f) {

return true;

}

public String getDescription() {

return "所有文件(*.*)";

}

});

chooser.addChoosableFileFilter(new FileFilter(){

public boolean accept(File f) {

if (f.getName().endsWith("xls") || f.isDirectory()) {

return true;

}else{

return false;

}

}

public String getDescription() {

return "Excel文件(*.xls)";

}

});

//打開對話框

int result = chooser.showSaveDialog(Global.mainFrame);//null

//文件確定

if(result==JFileChooser.APPROVE_OPTION) {

String outPath = chooser.getSelectedFile().getAbsolutePath();

if(new File(outPath).exists()){

if(!MessageTools.showConfirmDialog("文件已經存在,是否要覆蓋該文件?")){

return null;

}

}

return outPath;

}

return null;

}

FileOutputStream fileOut = new FileOutputStream(path); //String path = this.selectSavePath();

wb.write(fileOut); //org.apache.poi.hssf.usermodel.HSSFSheet 對象,裝載excel用

fileOut.close();

2. ?過濾路徑,并打開該文件

if (MessageTools.showConfirmDialog("導出數據成功,要打開該文件嗎?"))

{

//path = D:\\Backup\\我的文檔\\2012-11-09_110848.xls

String fileName = path.replace('\\', '/');

StringTokenizer st = new StringTokenizer(fileName, "/");

while (st.hasMoreTokens())

{

String sub = st.nextToken();

if ((sub.indexOf(' ') != -1) || (sub.indexOf('&') != -1) || (sub.indexOf('(') != -1) || (sub.indexOf(')') != -1) || (sub.indexOf('[') != -1) || (sub.indexOf(']') != -1) || (sub.indexOf('{') != -1) || (sub.indexOf('}') != -1) || (sub.indexOf('^') != -1) || (sub.indexOf('=') != -1) || (sub.indexOf(';') != -1) || (sub.indexOf('!') != -1) || (sub.indexOf('\'') != -1) || (sub.indexOf('+') != -1) || (sub.indexOf(',') != -1) || (sub.indexOf('`') != -1) || (sub.indexOf('~') != -1)) //過濾掉特殊字符

{

fileName = fileName.replaceFirst(sub, "\"" + sub + "\"");

}

}// fileName = D:/Backup/我的文檔/2012-11-09_110848.xls

Runtime.getRuntime().exec("cmd /E:ON /c start " + fileName);

}

用Runtime.getRuntime().exec()打開文件,文件路徑含有特殊符號的話則打開不了,需要把文件路徑過濾成特定格式。如:

過濾前 : ? ??D:\\Backup\\我的文檔\\2012-11-09_110848.xls

過濾后 ?: ? ?D:/Backup/我的文檔/2012-11-09_110848.xls

OK.

后記: 蛋疼的java們的XXX管理系統啊,jsp或者Extjs寫寫界面也就算了,連swing也上。。。哎,坑爹啊!估計以后得有好幾篇swing了。

總結

以上是生活随笔為你收集整理的java swing 导出文件_java swing (一) 导出excel文件并打开的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。