文件类:

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. 黑马程序员_java08_多线程

    转载于:http://www.itxuexiwang.com/plus/view.php?aid=148 线程是程序中可以并行执行的任务. java运行系统总是选当前优先级最高的处于就绪状态的线程来执 ...

  2. DDD~领域层

    回到目录 再论Domain与Infrastructure 在面向领域的设计中,领域层(Domain)实现上是位于最底层的,其它层有对它的引用,包括基础设施层(Infrastructure)也是去引用领 ...

  3. Atitit usrQBM2331 参数格式化规范

    Atitit usrQBM2331 参数格式化规范 String sql = "insert agent(uid,parent_id,pwd,name,tel,wechat,bkkad,si ...

  4. PHP性能优化工具–xhprof安装

    PHP性能优化工具–xhprof安装,这里我先贴出大致的步骤: 1.获取xhprof 2.编译前预处理 3.编译安装 4.配置php.ini 5.查看运行结果 那么下面我们开始安装xhprof工具吧: ...

  5. 开始使用MarkDown写博客

    MarkDown 标题 #h1 ##h2 ###h3 h1 h2 h3 代码段 代码段缩进4个空格即可,如下: <div class="form-group"> < ...

  6. Linux常用命令01

    Linux对于我们来说,就是跑程序的运行平台,简单的来说,就是服务器,自己也没怎么系统的学习Linux的命令,随着项目的需要, 比如要查找日志,哪里出问题了,哪里报错了,因此自己也慢慢地懂一些常用的L ...

  7. tomcat 容器生命周期lifecycle

    1.复习java的事件机制 java事件机制包括三个部分:事件.事件监听器.事件源. 事件:一般继承自java.util.EventObject类,封装了事件源对象及跟事件相关的信息. 事件监听器:实 ...

  8. Struts+Hibernate+Spring实现用户登录功能

    通过登录案例实现三大框架之间的整合,登录功能是任何系统和软件必不可少的一个模块,然而通过这个模块来认识这些复杂的框架技术,理解数据流向和整个设计思路是相当容易的.只有在掌握了这些小模块的应用后,才能轻 ...

  9. Windows Azure Virtual Machine (24) Azure VM支持多网卡功能

    <Windows Azure Platform 系列文章目录> Update 2016-03-30 一个VM的多张网卡可以在一个VNet的同一个Subnet子网下,笔者亲自测试过了. 如果 ...

  10. oracle创建表相关

    --创建表 create table person( id number primary key, name ), birth date ); --创建序列 create sequence perso ...