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的更多相关文章

  1. DataGridView导出到Excel的三个方法

    #region DataGridView数据显示到Excel /// <summary> /// 打开Excel并将DataGridView控件中数据导出到Excel /// </s ...

  2. c# datagridview导出到excel【转载】

    c# datagridview导出到excel[转载] http://hi.baidu.com/weizier/blog/item/8212caea1123b4d6d439c9fe.html 本作者使 ...

  3. C# - VS2019 DataGridView导出到Excel的三种方法

    //原文出处:http://www.yongfa365.com/Item/DataGridViewToExcel.html 1 #region DataGridView数据显示到Excel /// & ...

  4. Winform 中 dataGridView 导出到Excel中的方法总结

    最近,在做CS端数据导出到Excel中时网上找了很多代码感觉都不是自己想要的,通过自己的整理归纳得到一个比较通用的方法,就给大家分享一下: 该方法需要用到两个参数(即对象),一个  DataGridV ...

  5. winform DataGridView 导出到Excel表格 分类: WinForm 2014-07-04 10:48 177人阅读 评论(0) 收藏

    public bool ExportDataGridview(DataGridView gridView)         {             if (gridView.Rows.Count ...

  6. DataGridView 导出到Excel

    #region 导出四个表格到Excel /// <summary> /// 导出四个表格到Excel /// </summary> /// <param name=&q ...

  7. [WinForm]dataGridView导出到EXCEL

    方法一: SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Execl files (*.xls)|*.xls"; ...

  8. WinForm中DataGridView导出为Excel(快速版)

    public static void ExportExcel(DataGridView myDGV, string fileName) { string saveFileName = fileName ...

  9. 学习笔记 DataGridView数据导出为Excel

    DataGridView数据导出为Excel   怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...

随机推荐

  1. Java使用JDBC连接随意类型数据库(mysql oracle。。)

    package cn.liz.test; import java.io.InputStream; import java.sql.Connection; import java.sql.Driver; ...

  2. 深入了解MyBatis参数

    参考1 参考2 参考3

  3. Android网络开发之Socket通信

    HTTP通信中Client发送的每次请求都需要Server回送响应,在请求结束后,Client会主动释放连接.从建立连接到隔壁连接的过程成为一次连接.要保持Client程序的在线状态,需要不断地向Se ...

  4. TP3.2之WHERE组合条件处理

    1.条件都是int类型: $User->where('type=1 AND status=1')->select(); 2.条件包含字符串类型: 使用3.1以上版本的话,使用字符串条件的时 ...

  5. CentOS安装配置Samba

    介绍 Samba可以让我们在windows中访问linux系统中的文件,如果用来调试linux虚拟机中的代码会非常的方便 1.安装 yum -y update yum install samba sa ...

  6. PHP基于Sphinx+Swcs中文分词的全文的检索

    简介 Sphinx是开源的搜索引擎,它支持英文的全文检索.所以如果单独搭建Sphinx,你就已经可以使用全文索引了 但是有些时候我们还要进行中文分词所有scws就出现了,我们也可以使用Coreseek ...

  7. C# 读写网上邻居中的共享文件

    读写网上邻居共享的文件夹,和操作本地文件夹类似,只要有权限读取写入即可. 分为以下2步: 1.打通共享文件夹权限 2.操作文件 打通共享文件夹权限 /// <summary> /// 连接 ...

  8. 再看C#中的托付和事件

    在一口一个设计模式--观察者模式中.我们已经知道怎样应用观察者模式,但通过近期的深入学习,发现观察者模式存在下面缺陷: 1.抽象通知者依赖于抽象观察者: 2.每一个详细观察者被调用的方法必须一致. 比 ...

  9. Visual Studio Code 调试 nodejs (断点调试、自动重启、进程调试)

    学习链接: https://cnodejs.org/topic/5a9661ff71327bb413bbff5b https://github.com/nswbmw/node-in-debugging ...

  10. 怎么样快速完整备份和压缩 很大的 sqlserver 1TB 数据库 -摘自网络

    How to increase SQL Database Full Backup speed using compression and Solid State Disks The SQL 2008 ...