文件类:

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. jQuery的attr与prop

    jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...

  2. multiOTP配置安装

    https://code.google.com/p/google-authenticator/ 是google提供的OTP解决方案. http://www.multiotp.net/ 是一个开源otp ...

  3. Java连接Oracle数据库开发银行管理系统【三、实现篇】

    说明:里面的主要代码都加的有注释部分,所以代码显得很长,如果有错误的地方,谢谢指出. 注意需要导入数据库jar包 ------------------------------------------- ...

  4. C#教程(1) -- .Net与C#简介

    (1).Net .Net指.Net平台或者是.Net Framework框架. 如果你把.Net平台想象成一个厨房,那么.Net Framework框架就是其中的柴米油盐酱醋茶. 如果你把.Net平台 ...

  5. javascript中this指向

    在简单函数中,this是指向当前对象,可用来获取当前对象某个属性,但随着函数变复杂,this很多情况不指向当前对象,而是指向window. 1.在独立调用函数中,具有全局执行环境,this指向wind ...

  6. Python内建的对象列表

    Python内建的对象列表 刚写Python肯定会遇到这样的情况,想写些什么,但又不知从何写起... 在我看来问题在于我们不知道有什么东东可以拿来玩,这里列出Python的内建对象,稍微归类了一下,多 ...

  7. SpringBoot常用配置简介

    SpringBoot常用配置简介 1. SpringBoot中几个常用的配置的简单介绍 一个简单的Spring.factories # Bootstrap components org.springf ...

  8. 快速入门系列--WCF--04元数据和异常处理

    本章节将进行元数据和异常处理的介绍,这部分内容概念型比较强,可以快速浏览一下就好. 客户端和服务器借助于终结点进行通信,服务的提供者通过一个或者多个终结点将服务发布出来,服务的消费者则通过创建与之匹配 ...

  9. canvas游戏之贪食蛇

    直接上效果图: 这个贪食蛇关键地方在于数组,它的长度增加其实是数组的增长,就是数组的向前追加等操作,核心就是数组的操作. 完整代码: <!DOCTYPE html> <html> ...

  10. 编译原理(简单自动词法分析器LEX)

    编译原理(简单自动词法分析器LEX)源程序下载地址:  http://files.cnblogs.com/files/hujunzheng/%E6%B1%87%E7%BC%96%E5%8E%9F%E7 ...