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

时间: 2024-08-26 08:32:41

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

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

一.前言 项目中前端采用的Element UI 框架, 远程数据请求,使用的是axios,后端接口框架采用的asp.net webapi,数据导出成Excel采用NPOI组件.其业务场景,主要是列表页(如会员信息,订单信息等)表格数据导出,如表格数据进行了条件筛选,则需要将条件传至后端api,筛选数据后,导出成Excel. 思考过前端导出的3种方案: 1.使用location.href 打开接口地址.缺点: 不能传token至后端api, 无法保证接口的安全性校验,并且接口只能是get方式请求.

PHP从数据库导出EXCEL文件

参考博客链接:http://www.cnblogs.com/huangcong/p/3687665.html 我的程序代码 原生导出Excel文件 <?phpheader('Content-type: text/html; charset=utf-8');header("Content-type:application/vnd.ms-excel");header("Content-Disposition:filename=test.xls"); $conn =

TXLSReadWriteII2版本导出Excel文件:

//TXLSReadWriteII2版本导出Excel文件: procedure TForm1.N1Click(Sender: TObject); var i: Integer; aSaveDialog: TSaveDialog; aFileName, aStampTime: AnsiString; aXlsObj: TXLSReadWriteII2; p: PDataRec; begin aSaveDialog := TSaveDialog.Create(Self); try aSaveDia

使用Open Xml按模版导出Excel文件(下)

(3)将数据写入Excel 把数据写入到Excel文件是本文的关键技术.这里需要用到OpenXml.在Sdk里已经提供了足够的文件操作方法,我们就调用这些方法,将数据源的数据写到文件中. 首先我们定义了一些工具类,用于读取单元格的行列值. 在Excel中,每个单元格都有一个地址:如B3表示第二行第三列的格子,在Utility中的GetRowIndex和GetColumnIndex就是输入字符串”B3”,返回Int类型的索引2 和3.还有输入“B3”,返回列的字母“B”等等. 在WorkSheet

PHPExcel导出Excel文件

error_reporting(0); require_once 'phpexcel/PHPExcel.php'; require_once 'phpexcel/PHPExcel/IOFactory.php'; $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setCreator("BillHong") ->setLastModifiedBy("BillHong")//创建

Java web中不同浏览器间导出Excel文件名称乱码问题解决方案

问题描述: 对于不同浏览器存在对中文编码格式问题,从而在导出Excel文件时,中文文件名出现乱码的情况,即在程序中给要导出的文件指定一个中文名字时,在浏览器上出现的下载框中的文件名出现了乱码,解决如下: 解决方案: [java] view plain copy Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间 DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//设置显示格

PHP导出excel文件,第一步先实现PHP模板导出不带数据

今天继续研究PHP导出excel文件,把复杂的事情简单化,一步步实现功能,首先实现模板文件的导出,随后再实现写入数据后导出,最终实现功能,这是基本思路.中间可以加一步,先自己写入数据导出试试,随后再数据库导入.我首先把程序提交到自建的eubexcel.php文件,选用post提交,导出excel文件的程序在这个页面里书写,参考昨天下载的PHPExcel-1.8组件里的参考文档,先部署导出excel,具体代码如下 <?php error_reporting(E_ALL);ini_set('disp

使用Open Xml按模版导出Excel文件(上)

完整内容请参见我的网站 http://www.iyummy.com.cn/Blog/Detail/3 我们在做应用系统中经常需要将数据库中的数据导出成为Excel文件.如果是要导出报表的话,最好是能够根据定义好的模版生成一个美观的Excel. 以前要生成有样式的Excel.一般是使用Xml形式的Excel修改,或者使用Excel的Api.前者要修改样式的话特别麻烦,后者在Asp.net里会起很多进程. 庆幸的是从office 2007开始,微软使用了OpenXml来定义office的文件.使用r

JQGrid导出Excel文件

系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件及方法 Web jquery表格组件 JQGrid 的使用 - 5.Pager翻页.搜索.格式化.自定义按钮 Web jquery表格组件 JQGrid 的使用 - 6.准备工作 & Hello JQGrid Web jquery表格组件 JQGrid 的使用 - 7.查询数据.编辑数据.删除数据

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

Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: [csharp] view plain copy print? public static System.Data.DataTable ReadExcel(String strFileName) { Workbook book = new Workbook(); book.Open(strFileName); Worksheet sheet = book.Worksheets[0]; Cells cells =