• 导出Excel文件(针对Dev)


    文件类:

    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"));

  • 相关阅读:
    对文件的操作
    三级菜单优化
    三级菜单项目
    对字符串的操作(一些重要的方法)
    四、saltstack如何管理对象?
    三、saltstack证书管理
    二、saltstack基础配置
    一、saltstack简介和安装
    set集合
    异常处理语法
  • 原文地址:https://www.cnblogs.com/SharonHwang/p/4911710.html
Copyright © 2020-2023  润新知