OpenDialog/SaveDialog 中 Filter用法

Filter 属性 赋值为一字符串 用于过滤文件类型;

字符串说明如下:

‘|’分割的两个,一个是注释,一个是真的Filter,显示出来的是那个注释。如果要一次显示多中类型的文件,用分号分开。
如:

Open1.Filter="图片文件(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp";

则过滤的文件类型为 “|”号  右边的 *.jpg;*.gif;*.bmp 三种类型文件,在OpenDialog/SaveDialog中显示给用户看的文件类型字符串则是 :“|”号  左边的 图片文件(*.jpg,*.gif,*.bmp)。

再如:

Open1.Filter="图像文件(*.jpg;*.jpg;*.jpeg;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.png";

附上一小段自己写的代码:

1 /// <summary>
2 /// 选择要保存Excel的文件路径
3 /// </summary>
4 /// <param name="saveFileName">文件完整路径</param>
5 /// <returns>布尔值,判断文件保存路径是否可用</returns>
6   public bool GetExcelDestination(ref string saveFileName)
7 {
8 SaveFileDialog saveDialog = new SaveFileDialog();
9 saveDialog.DefaultExt = "xlsx";
10 saveDialog.Filter = "Excel 2007文件|*.xlsx|Excel 99-03文件|*.xls";
11 saveDialog.ShowDialog();
12
13 //被点了取消
14   if (saveDialog.FileName.IndexOf(":") < 0)
15 {
16 return false;
17 }
18
19 //模板Excel不存在
20 if ((!File.Exists(Application.StartupPath.Trim() + @"\templates\template.xlsx")) || (!File.Exists(Application.StartupPath.Trim() + @"\templates\template.xls")))
21 {
22 MessageBox.Show("模板Excel不存在!请确保文件安装目录" + Application.StartupPath.Trim() + "下的templates文件夹里的template.xlsx文件存在!");
23 return false;
24 }
25
26 //如果选择保存的文件路径是模板文件,则禁止保存
27 if (saveDialog.FileName == Application.StartupPath.Trim() + @"\templates\template.xlsx" || saveDialog.FileName == Application.StartupPath.Trim() + @"\templates\template.xls")
28 {
29 MessageBox.Show("请不要选择模板Excel作为导出文件!");
30 return false;
31 }
32
33 //如果对方机子未安装Excel
34 Excel._Application myExcel = new Excel.Application();
35 if (myExcel == null)
36 {
37 MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel!");
38 return false;
39 }
40
41 //错误检测全通过后
42 saveFileName = saveDialog.FileName;
43 return true;
44 }

posted on 2011-04-12 14:43  Osiris_Syou  阅读(2424)  评论(0编辑  收藏  举报

导航