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,绑定了数据源 ...
随机推荐
- Erlang中atom的实现
Erlang的原子(atom)在匹配中有着重要作用,它兼顾了可读性和运行效率. 通过atom,可以实现很多灵活高效的应用. atom可以看作是给字符串生成了一个ID,内部使用的是ID值,必要时可以取出 ...
- HDUOJ----1263水果
水果 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- 总结PLSQL的快捷键以及使用技巧
http://www.dedecms.com/knowledge/data-base/oracle/2012/0724/3643.html 最近在开发过程中,遇到一些麻烦,就是开发效率问题,有时候其他 ...
- ASP.NET 安全系列 Membership三步曲之入门篇 - Jesse Liu
Membership 三步曲 ASP.NET 安全系列 Membership三步曲之入门篇 ASP.NET 安全系列 Membership三步曲之进阶篇 ASP.NET 安全系列 Membership ...
- MongoDB基本管理命令 [转]
MongoDB基本管理命令 linux下配置安装mongodb 10分钟玩转mongoDB 官网安装教程 使用命令行方式连接mongodb: mongo /admin -u用户名 -p密码 --连接 ...
- PostgreSQL安装详细步骤(linux)
官方安装文档:http://www.postgresql.org/download/linux/redhat/ 1. 检查PostgreSQL 是否已经安装 Linux-软件包管理-rpm命令管理-查 ...
- FA_在建工程转固定资产(流程)
2014-06-08 Created By BaoXinjian
- PS_Form个性化选择Block自动查询和查询条件排序实现(案例)
2014-06-01 BaoXinjian
- linux下安装nginx和配置
1.系统:centos6.8 2.安装准备: 安装nginx前,我们首先要确保系统安装了g++.gcc.openssl-devel.pcre-devel和zlib-devel软件,可通过如图所示命令进 ...
- PHP安装Redis扩展教程
安装redis 下载软件包phpredis https://github.com/nicolasff/phpredis uzip master #解压得到 phpredis-master c ...