当前位置:威尼斯 > 编程 > 张开路线【威尼斯】&lt

张开路线【威尼斯】&lt

文章作者:编程 上传时间:2019-09-27

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = strOpenFileFolder;//初始目录,不赋值也可以
openFileDialog1.Filter = “mxml文件|*.mxml”;//文件类型
openFileDialog1.ShowDialog();//弹出选择框
String strOpenFileFolder = openFileDialog1.FileName.Substring(0,openFileDialog1.FileName.LastIndexOf(‘\’));//打开的文件的目录
String strOpenFileName = openFileDialog1.FileName;//打开的文件的全限定名

文件类:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = strSaveFileName;//保存的默认目录,不赋值也可以
saveFileDialog1.Filter = “swf文件|*.swf”;//过滤显示的文件类型
saveFileDialog1.FileName = strOpenFileName.Substring(0,strOpenFileName.LastIndexOf(‘.’))+”.swf”;//保存的文件名
saveFileDialog1.ShowDialog();//弹出选择框
String strSaveFileFolder = saveFileDialog1.FileName.Substring(0,saveFileDialog1.FileName.LastIndexOf(‘\’));//保存的文件的目录
String strSaveFileName = saveFileDialog1.FileName;//保存的文件的全限定名

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace DataHandle_Print_Export_
{
class FileProcess
{
private FileProcess() { }
public static readonly FileProcess Instance = new FileProcess();

/// <summary>
/// 说明:打开文件提示框
/// </summary>
/// <param name="openType">打开文件类型</param>
/// <returns>打开路径</returns>
public string OpenDialog(string openType)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = openType;
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
return openFileDialog.FileName;
}
return "";
}

/// <summary>
/// 说明:保存文件提示框(传入后缀返回保存地址)
/// </summary>
/// <param name="ext">保存扩展名</param>
/// <returns>返回保存路径</returns>
public string SaveDialog(string ext)
{
string localFilePath = "", fileNameExt = "", newFileName = "", FilePath = "";
SaveFileDialog saveFileDialog = new SaveFileDialog();

//设置文件类型
//书写规则如:txt files(*.txt)|*.txt
saveFileDialog.Filter = string.Format("{0} File(*.{0})|*.{0}|所有文件(*.*)|*.*");
//设置默认文件名(可以不设置)
saveFileDialog.FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "."

  • ext;
    //获取或设置一个值,该值指示如果用户省略扩展名,对话框是否自动在文件名中添加扩展名。
    saveFileDialog.AddExtension = true;
    //保存对话框是否记忆上次打开的目录
    saveFileDialog.RestoreDirectory = true;

DialogResult result = saveFileDialog.ShowDialog();

if (result == DialogResult.OK)
{
//获得文件路径
localFilePath = saveFileDialog.FileName.ToString();

return localFilePath;
}
return "";
}

/// <summary>
/// 说明:文件流保存
/// </summary>
/// <param name="stream">需要保存的文件流</param>
/// <param name="path">文件路径</param>
public void FileSteamSave(Stream stream, string path)
{
Stream sourceStream = stream;
FileStream targetStream = null;
using (targetStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
{
const int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
{
targetStream.Write(buffer, 0, count);
}
targetStream.Close();
sourceStream.Close();
}
}

public void ExploreExcel(DevExpress.XtraGrid.Views.Grid.GridView gridView, string fileName)
{
bool isExport = true;
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Title = "请选择文件存放路径";
saveFile.Filter = "Excel文档(*.xls)|*.xls|Excel文档(*.xlsx)|*.xlsx";
saveFile.FileName = fileName;
if (saveFile.ShowDialog() == DialogResult.OK)
{
DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
options.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text;
options.SheetName = fileName;
gridView.OptionsPrint.AutoWidth = false;
gridView.AppearancePrint.Row.Font = new System.Drawing.Font("宋体", 9);
try
{
gridView.ExportToXls(saveFile.FileName, options);
}
catch (Exception ex)
{
MessageBox.Show("文件已打开,正在使用中,导出失败", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
isExport = false;
}

if (isExport)
{
if (MessageBox.Show("导出成功,是否打开文件?", "询问",MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
System.Diagnostics.Process.Start(saveFile.FileName);

}
}
}
}
}

客户端调用

FileProcess.Instance.ExploreExcel(gridView1, "Test" + DateTime.Now.ToString("yyyyMMddHHmmss"));

 

本文由威尼斯发布于编程,转载请注明出处:张开路线【威尼斯】&lt

关键词: