DataGridView导出数据到Excel
//传入DataGridView
/// <summary>
/// 输出数据到Excel
/// </summary>
/// <param name="dataGridView">DataGridView</param>
/// <param name="includeHeader">是否包含字段名</param>
/// <param name="savePath">保存路径</param>
/// <returns>成功TRUE/失败FALSE</returns>
public static bool ExportExcel(DataGridView dataGridView, bool includeHeader, string savePath)
{
bool succeed = true;
try
{
//创建工作薄
IWorkbook wbook = null;
//创建工作表
ISheet sheet = null;
wbook = ICreateIWorkBook(savePath, null);
sheet = wbook.CreateSheet("Sheet1"); //创建工作表
int columnCount = dataGridView.ColumnCount; //获取行数
int rowCount = dataGridView.RowCount; //获取列数
int rowIndex = ;
IRow row = null;
ICell cell;
int start = ; //是否包含表头行数
//dataGridView是否有数据
if (rowCount <= )
{
succeed = false;
Exception error = new Exception("无可用于导出的数据");
throw error;
}
//是否包含表头
if (includeHeader)
{
row = sheet.CreateRow(rowIndex);
//输入标题表头行
for (int i = ; i < columnCount; i++)
{
cell = row.CreateCell(i);
cell.SetCellValue(dataGridView.Columns[i].HeaderText);
}
++start; //如果包含表头行索引累加
} //DataGridView有多少行
for (; rowIndex < rowCount; ++rowIndex)
{
row = sheet.CreateRow(rowIndex + start); //创建行
for (int cellIndex = ; cellIndex < columnCount; ++cellIndex)
{
Type type = dataGridView[cellIndex, rowIndex].ValueType;
object value = dataGridView[cellIndex, rowIndex].Value;
cell = row.CreateCell(cellIndex); //创建单元格
//写入数据
if (type == typeof(Int32))
{
cell.SetCellValue((int)dataGridView[cellIndex, rowIndex].Value);
}
else if (type == typeof(Double))
{
cell.SetCellValue((double)value);
}
else if (type == typeof(float))
{
cell.SetCellValue((float)value);
}
else if (type == typeof(DateTime))
{
cell.SetCellValue((DateTime)value);
}
else if (type == typeof(Boolean))
{
cell.SetCellValue((bool)value);
}
else
{
cell.SetCellValue((string)value);
}
}
}
//保存的文件流保存或创建
FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate);
wbook.Write(fs); //写入文件
wbook.Close();
fs.Close(); }
catch (Exception ex)
{
succeed = false;
throw ex;
}
return succeed;
} /// <summary>
/// 根据文件格式返回表文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="fs">文件流</param>
/// <returns>IWorkBook 表</returns>
private static IWorkbook ICreateIWorkBook(string filePath, FileStream fs)
{
IWorkbook wbook = null;
//读取用户选择的保存格式
string extesion = Path.GetExtension(filePath).ToLower();
if (extesion.Equals(".xlsx"))
//if( saveFile.FileName.IndexOf(".xlsx") > 0);
{
if (null != fs)
{
wbook = new XSSFWorkbook(fs);
}
else
{
wbook = new XSSFWorkbook();
}
}
else if (extesion.Equals(".xls"))
//else if (saveFile.FileName.IndexOf(".xls") > 0) ; //这种方法也可以实现但.xlsx必须在前面
{
if (null != fs)
{
wbook = new HSSFWorkbook(fs);
}
else
{
wbook = new HSSFWorkbook();
}
}
else //如果不是上述两种格式,因为设置了过滤器几乎不会出现下面的情况
{
//MessageBox.Show("对不起,您所输入的文件格式不受支持!");
//return null;
Exception error = new Exception("文件格式不正确!");
throw error;
}
return wbook;
} //如有问题欢迎大家指正
DataGridView导出数据到Excel的更多相关文章
- 【转】c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
准备工作就是可以分页的DataGridView,和两个按钮,一个用来导出当前页数据到Excel,一个用来导出全部数据到Excel 没有使用SaveFileDialog,但却可以弹出保存对话框来 先做导 ...
- DataGridView导出数据到Excel及单元格格式的改动
在软件开发过程中,时常会遇到把一些数据信息从DataGridView中导出到Excel表格中的情况.假设写的多了就会发现挺简单的,我们最好还是来写一写,留作备用,毕竟有时候Ctrl+C和Ctrl+V还 ...
- 一个方便且通用的导出数据到 Excel 的类库
一个方便且通用的导出数据到 Excel 的类库 起源: 之前在做一个项目时,客户提出了许多的导出数据的需求: 导出用户信息 导出业务实体信息 各种查询都要能导出 导出的数据要和界面上看到的一致 可以分 ...
- 1.ASP.NET MVC使用EPPlus,导出数据到Excel中
好久没写博客了,今天特地来更新一下,今天我们要学习的是如何导出数据到Excel文件中,这里我使用的是免费开源的Epplus组件. 源代码下载:https://github.com/caofangshe ...
- 导出数据到Excel --使用ExcelReport有感
先看图,这是几个月前用NPOI写的导出数据到Excel,用了上百行代码,而且难控制,导出来也比较难看 excel打开的效果 下面是我用ExcelReport类库导出到Excel的操作 1.首先引用Ex ...
- 使用Open xml 操作Excel系列之二--从data table导出数据到Excel
由于Excel中提供了透视表PivotTable,许多项目都使用它来作为数据分析报表. 在有些情况下,我们需要在Excel中设计好模板,包括数据源表,透视表等, 当数据导入到数据源表时,自动更新透视表 ...
- Dynamics CRM导出数据到Excel
原创地址:http://www.cnblogs.com/jfzhu/p/4276212.html 转载请注明出处 Pivot Table是微软BI的一个重要工具,所以这里讲一下Dynamics CRM ...
- MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult
导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...
- php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
而关于php的也有,但是大多都是用phpExcel导出的方法或者spreadsheet等类或者控件之类的导出方法,而我所在维护的系统却用很简单的方法,如下,网上很少有讲如何设置要导出数据的EXcel格 ...
随机推荐
- random模块(随机数)
random.random() #0-1之间的随机数 random.randint(1,10) #1-10 包括10的随机数 --> int random.choice(list) #随机选取列 ...
- SQL设置时间格式
SELECT STR_TO_DATE('Jul 20 2013 7:49:14:610AM','%b %d %Y %h:%i:%s:%f%p') from DUAL; -- 执行后得到结果:'2013 ...
- Spring框架:@ResponseBody 中文乱码----------我的主题站内单点登录
问题背景 本文并不是介绍@ResponseBody注解,也不是中文乱码问题的大汇总笔记,这些网上都有很多内容了.这边仅对几年前,一个卡壳了挺久时间的问题的解决过程做一个记录,以警惕自己,达到自醒得目的 ...
- React-redux深入理解
首先,一张 Redux 解释图镇楼: [回顾]Redux 的核心: store 是什么?(createStore 函数的实现) const store = createStore(reducer); ...
- DashBoard创建各种表(一)
创建透视表 1.首先需要创建一个DashBoard,然后点击开始导航栏中的透视,创建一个透视表,透视表可以交叉式的显示报告,让我们可以更直观的看到多维数据. 2.把CategroyName和Categ ...
- python pip安装其他模块到中途失败问题
当网速很差时,pip安装到中途总是出现一大片红色然后失败.而且往往安装下载很久,失败了就要从新开始,失败如下 就是,当你出现这个错误Could not find a version that sati ...
- css常用样式总结:
一.input和textarea修改placeholder的颜色: input::-webkit-input-placeholder { /* WebKit browsers */ color: #c ...
- python爬虫之常见的加密方式
前言 数据加密与解密通常是为了保证数据在传输过程中的安全性,自古以来就一直存在,古代主要应用在战争领域,战争中会有很多情报信息要传递,这些重要的信息都会经过加密,在发送到对应的人手上. 现代 ,在网络 ...
- python批量下载微信好友头像,微信头像批量下载
#!/usr/bin/python #coding=utf8 # 自行下载微信模块 itchat 小和QQ496631085 import itchat,os itchat.auto_login() ...
- 上手d3js
0---什么是d3js: d3js是一个开源的,基于对svg操作的数据可视化框架,简单的说他提供了数据提供了一系列的数据可视化工具,可以用他很方便的创造出关于svg的动画:svg动画具有可伸缩,不失真 ...