文件类:

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

导出Excel文件(针对Dev)的更多相关文章

  1. 基于Vue + axios + WebApi + NPOI导出Excel文件

    一.前言 项目中前端采用的Element UI 框架, 远程数据请求,使用的是axios,后端接口框架采用的asp.net webapi,数据导出成Excel采用NPOI组件.其业务场景,主要是列表页 ...

  2. ExtJS Grid导出excel文件

    ExtJS Grid导出excel文件, 需下载POI:链接:http://pan.baidu.com/s/1i3lkPhF 密码:rqbg 1.将Grid表格数据连同表格列名传到后台 2.后台导出e ...

  3. PHP从数据库导出EXCEL文件

    参考博客链接:http://www.cnblogs.com/huangcong/p/3687665.html 我的程序代码 原生导出Excel文件 <?phpheader('Content-ty ...

  4. jxl导出Excel文件

    一.java项目实现读取Excel文件和导出Excel文件 实现读取和导出Excel文件的代码: package servlet; import java.io.FileInputStream; im ...

  5. PHP导出excel文件

    现在教教你如何导入excel文件: 在我的文件储存里面有一个com文件夹的,将其解压放在ThinkPHP/Library/文件夹里面,然后就是写控制器啦!去调用这个插件: <?php names ...

  6. 【转】 (C#)利用Aspose.Cells组件导入导出excel文件

    Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: public static System.Data.DataTable ReadExcel(String strFi ...

  7. PHPExcel导出excel文件

    今天园子刚开,先来个货顶下,后续园丁qing我会再慢慢种园子的,希望大家多来园子逛逛. PHPExcel导出excel文件,先说下重要的参数要记住的东西 impUser() 导入方法 exportEx ...

  8. 导出Excel文件

    /// <summary> /// 类说明:Assistant /// 更新网站:[url=http://www.sufeinet.com/thread-655-1-1.html]http ...

  9. 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法

    -----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...

  10. MSSQL2005 导出excel文件

    Title:MSSQL2005 导出excel文件  --2011-01-16 16:01 EXEC master..xp_cmdshell 'bcp "select * from 数据库名 ...

随机推荐

  1. java 堆栈分析4

    jprofiler ,又是一款好工具... —— 不过显然,我觉得有了jvisualvm就足够了,难道它会比jvisualvm还强大很多!?? 什么时候需要它呢?它有什么特别好用的地方吗? 带来什么方 ...

  2. 开发者最常用的 8 款 Sublime Text 3 插件

    转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0228/177.html?1456925631Sublime Text作为一个尽为人知的代码编辑器, ...

  3. CentOS6.5下安装JDK

    之前一直没有完全的总结出一篇关于Linux下安装Java的过程,今天正好就整理下. 下载jdk 如果在官网下载比较慢,那么可以到我的云盘分享上,下载jdk 1.8.0的版本: 下载地址参考链接 解压缩 ...

  4. 关于Lua优质文章链接

    Lua 语言 15 分钟快速入门(博客-伯乐在线) http://blog.jobbole.com/70480/

  5. salesforce 零基础学习(三十九) soql函数以及常量

    在salesforce中,我们做SOQL查询时,往往需要用到计算式,比如求和,求平均值,或者过滤数据时,往往需要通过时间日期过滤,SOQL已经封装了很多的函数,可以更加方便我们的sql查询而不需要自己 ...

  6. SQLServer清空数据库中所有的表并且ID自动归0

    exec sp_MSforeachtable 'Truncate Table ?'

  7. Java environment variables and their functionality

    Explanations of Functionalities: 1. PATH env variable It is used to search the command directory whe ...

  8. (2)艺术创新思维的PS成果

        一些艺术创新思维课的PS成果. (1)选择一首喜欢的歌并用图表示出来: (2)用形式美法则设计一张图来表示"痴心妄想"这四个词语,可以采用字体的变形,也可以采用词语的意境. ...

  9. java之内部类详解

    序言 有位小同学要我写一篇这个的总结,我说那好吧,那就动手写总结一下这个内部类的知识,感觉这个在面试中也会经常遇到,内部类.反射.集合.IO流.异常.多线程.泛型这些重要的基础知识大家都比较容易记不住 ...

  10. 理解模板引擎Razor 的原理

    Razor是ASP.NET MVC 3中新加入的技术,以作为ASPX引擎的一个新的替代项.简洁的语法与.NET Framework 结合,广泛应用于ASP.NET MVC 项目.Razor Pad是一 ...