datagridview导出到excel
Microsoft.Office.Interop.Excel.Range range = null;
string saveFileName = "";
bool fileSaved = false;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.InitialDirectory = @"C:\Documents and Settings\Administrator\桌面";
saveDialog.Filter = "Microsoft.Office.Interop.Excel文件|*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0)
return; //被点了取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Microsoft.Office.Interop.Excel对象,可能您的机子未安装Microsoft.Office.Interop.Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
//写入标题
int indexOfCol = 0;
for (int i = 0; i < myDGV.ColumnCount; i++)
{
string strColumn=myDGV.Columns[i].Name;
if (strColumn == "PriceId" || strColumn == "FlowerId" || strColumn == "IsValid" || strColumn == "Version" || strColumn == "ValidDateTo" || strColumn == "ValidDateFrom")
{
continue;
}
if (ChargeCell(myDGV.Columns[i].Visible, isShow))
{
indexOfCol = indexOfCol + 1;
worksheet.Cells[1, indexOfCol] = myDGV.Columns[i].HeaderText;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, indexOfCol];
range.Interior.ColorIndex = 15;//背景颜色
range.Font.Bold = true;//粗体
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中
//加边框
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
range.ColumnWidth = 4.63;//设置列宽
range.EntireColumn.AutoFit();//自动调整列宽
}
}
//写入数值
for (int r = 0; r < myDGV.Rows.Count; r++)
{
indexOfCol = 0;
for (int i = 0; i < myDGV.ColumnCount; i++)
{
if (ChargeCell(myDGV.Columns[i].Visible, isShow))
{
indexOfCol = indexOfCol + 1;
worksheet.Cells[r + 2, indexOfCol] = myDGV.Rows[r].Cells[i].Value;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, indexOfCol];
range.Interior.ColorIndex = 19;//背景颜色
range.Font.Bold = false;//粗体
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//居中
//加边框
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
range.ColumnWidth = 14.63;//设置列宽
range.EntireColumn.AutoFit();//自动调整列宽
}
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
else
{
fileSaved = false;
}
xlApp.Quit();
GC.Collect();//强行销毁
if (fileSaved && isOpen && System.IO.File.Exists(saveFileName))
System.Diagnostics.Process.Start(saveFileName); //打开EXCEL
MessageBox.Show(fileName + "保存成功", "提示", MessageBoxButtons.OK);
datagridview导出到excel的更多相关文章
- DataGridView导出到Excel的三个方法
#region DataGridView数据显示到Excel /// <summary> /// 打开Excel并将DataGridView控件中数据导出到Excel /// </s ...
- c# datagridview导出到excel【转载】
c# datagridview导出到excel[转载] http://hi.baidu.com/weizier/blog/item/8212caea1123b4d6d439c9fe.html 本作者使 ...
- C# - VS2019 DataGridView导出到Excel的三种方法
//原文出处:http://www.yongfa365.com/Item/DataGridViewToExcel.html 1 #region DataGridView数据显示到Excel /// & ...
- Winform 中 dataGridView 导出到Excel中的方法总结
最近,在做CS端数据导出到Excel中时网上找了很多代码感觉都不是自己想要的,通过自己的整理归纳得到一个比较通用的方法,就给大家分享一下: 该方法需要用到两个参数(即对象),一个 DataGridV ...
- winform DataGridView 导出到Excel表格 分类: WinForm 2014-07-04 10:48 177人阅读 评论(0) 收藏
public bool ExportDataGridview(DataGridView gridView) { if (gridView.Rows.Count ...
- DataGridView 导出到Excel
#region 导出四个表格到Excel /// <summary> /// 导出四个表格到Excel /// </summary> /// <param name=&q ...
- [WinForm]dataGridView导出到EXCEL
方法一: SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Execl files (*.xls)|*.xls"; ...
- WinForm中DataGridView导出为Excel(快速版)
public static void ExportExcel(DataGridView myDGV, string fileName) { string saveFileName = fileName ...
- 学习笔记 DataGridView数据导出为Excel
DataGridView数据导出为Excel 怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...
随机推荐
- 最新美行地图Z13升级攻略
原文地址:http://bbs.gpsuu.com/read.php?tid-231134.html 2013年11月16日订车,4S答应送导航,却没有提送什么导航.12月24日提车,DVD导航一体 ...
- Python 实现的、带GUI界面的词云生成器
代码地址如下:http://www.demodashi.com/demo/14233.html 详细说明: "词云"就是数据可视化的一种形式,给出一段文本,根据文本中词语的出现频率 ...
- poj----Maximum sum(poj 2479)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30704 Accepted: 9408 Desc ...
- Eureka常见问题总结
在Spring-cloud做微服务架构时,会碰到各种坑.下面总结一下Eureka的常见问题. 一.Eureka的自我保护模式 如果在Eureka Server的首页看到以下这段提示,则说明Eure ...
- python练习笔记——编写一个装饰器,模拟登录的简单验证
编写一个装饰器,模拟登录的简单验证(至验证用户名和密码是否正确) 如果用户名为 root 密码为 123则正确,否则不正确.如果验证不通过则不执行被修饰函数 #编写一个装饰器,模拟登录的简单验证 #只 ...
- 转 虫师的selenium借助AutoIt识别上传(下载)详解
selenium借助AutoIt识别上传(下载)详解 2014-12-27 11:26 by 虫师, 755 阅读, 1 评论, 收藏, 编辑 AutoIt目前最新是v3版本,这是一个使用类似BAS ...
- Android调试技巧之Eclipse行号和Logcat
很多初入Android的开发者可能会发现经常遇到Force Close或ANR这样的问题,一般我们通过Android系统的错误日志打印工具Logcat可以看到出错的内容,今天一起来说下如何通过 Ecl ...
- C语言07指针高级
01内存四区 接口封装和设计思想引导 接口封装设计思想引导 Sckclient客户端api模型设计 第一套api函数 #ifndef _SCK_CLINT_H_ #define _SCK_CLINT_ ...
- javascript linkedlist data structures
在使用C++的时候我们经常会使用到各种容器,这些容器其实就是一种数据结构.在java中其实也是如此.但是由于javascript只给我们提供了一种内置的数据结构数组,准备来说是对象.没有我们常见的那些 ...
- Vim进阶技术:搜索和替换
行内搜索 行内搜索,也就是在当前行内进行搜索和移动,通常都与编辑命令一起使用. fx -- 移动到下一个字符x的位置,光标停留在x字符上面 tx -- 移动到下一个字符x的位置,光标停留在x前一个字符 ...