导出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"));
导出Excel文件(针对Dev)的更多相关文章
- 基于Vue + axios + WebApi + NPOI导出Excel文件
一.前言 项目中前端采用的Element UI 框架, 远程数据请求,使用的是axios,后端接口框架采用的asp.net webapi,数据导出成Excel采用NPOI组件.其业务场景,主要是列表页 ...
- ExtJS Grid导出excel文件
ExtJS Grid导出excel文件, 需下载POI:链接:http://pan.baidu.com/s/1i3lkPhF 密码:rqbg 1.将Grid表格数据连同表格列名传到后台 2.后台导出e ...
- PHP从数据库导出EXCEL文件
参考博客链接:http://www.cnblogs.com/huangcong/p/3687665.html 我的程序代码 原生导出Excel文件 <?phpheader('Content-ty ...
- jxl导出Excel文件
一.java项目实现读取Excel文件和导出Excel文件 实现读取和导出Excel文件的代码: package servlet; import java.io.FileInputStream; im ...
- PHP导出excel文件
现在教教你如何导入excel文件: 在我的文件储存里面有一个com文件夹的,将其解压放在ThinkPHP/Library/文件夹里面,然后就是写控制器啦!去调用这个插件: <?php names ...
- 【转】 (C#)利用Aspose.Cells组件导入导出excel文件
Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: public static System.Data.DataTable ReadExcel(String strFi ...
- PHPExcel导出excel文件
今天园子刚开,先来个货顶下,后续园丁qing我会再慢慢种园子的,希望大家多来园子逛逛. PHPExcel导出excel文件,先说下重要的参数要记住的东西 impUser() 导入方法 exportEx ...
- 导出Excel文件
/// <summary> /// 类说明:Assistant /// 更新网站:[url=http://www.sufeinet.com/thread-655-1-1.html]http ...
- 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法
-----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...
- MSSQL2005 导出excel文件
Title:MSSQL2005 导出excel文件 --2011-01-16 16:01 EXEC master..xp_cmdshell 'bcp "select * from 数据库名 ...
随机推荐
- 使用protractor操作页面元素
Protractor是为Angular JS应用量身打造的端到端测试框架.它可以真实的驱动浏览器,自动完成对web应用的测试.Protractor驱动浏览器使用的是WebDriver标准,所以使用起来 ...
- mysql: error while loading shared libraries: libmysqlclient.so.16
[root@host_41 mysql]# mysqlmysql: error while loading shared libraries: libmysqlclient.so.16: cannot ...
- SQL——索引
索引是根据表中一列或若干列按照一定顺序建立的列值与记录行之间的对应关系表.在数据库系统中建立索引主要有以下作用: l快速存取数据: l保证数据记录的唯一性: l实现表与表之间的参照完整性: l在使用O ...
- IOS Socket 03-建立连接与登录
1. 搭建python服务器 这里我们用到python服务器Socket Server.如何运行Server?下面介绍 1)通过百度云下载文件 http://pan.baidu.com/s/1i5yb ...
- 容器使用的12条军规——《Effective+STL中文版》试读
容器使用的12条军规——<Effective+STL中文版>试读 还 记的自己早年在学校学习c++的时候,老师根本就没有讲STL,导致了自己后来跟人说 起会C++的时候总是被鄙视, ...
- KnockoutJS 3.X API 第四章 表单绑定(7) event绑定
目的 event绑定即为事件绑定,即当触发相关DOM事件的时候回调函数.例如keypress,mouseover或者mouseout等 例如: Mouse over me Details var vi ...
- Spring配置汇总
现在主流的JavaWeb应用几乎都会用到Spring,以下是Spring的配置,以及结合Web的SpringMVC配置的汇总. jar包的引入 与Web项目集成 Spring配置文件 SpringMV ...
- ligerUI Tree 实例 代码
http://www.oschina.net/code/snippet_1762525_47819#68813
- Entity Framework 5中应用表值函数进行Linq查询
Entity Framework 5引入了表值函数(Table-Valued Functions TVFs).表值函数的返回类型是一个Table类型,可用在SQL查询语句中.最简单的表值函数,读取客户 ...
- java线程四种状态
一个线程可以有四种状态: 1.新(new), 即线程刚刚创建,而并未执行 2.可运行(runnable),意味着一旦时间分片机制有空闲的CPU周期提供给一个线程,那个线程便可立即开始运行.因此,线程可 ...