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了。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章