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,绑定了数据源 ...
随机推荐
- HighCharts终极版本
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- android.widget.BaseAdapter调用DataSetObservable.notifyChanged/Invalidated
在android.widget.BaseAdapter类中定义了两个notifyDataXXX方法. public void notifyDataSetChanged() { mDataSet ...
- PHP-VC9/VC6 TS/NTS等版本之间的区别
PHP的更新升级是越来越快了,PHP 5.2 版本已经更新到5.2.17不再更新, 5.3版本的更新到了5.3.8,PHP 5.4马上就要发布,甚至PHP6.0也在开发中.有这么多版本供我们选择,真是 ...
- springmvc最优化
java代码 package com.tgb.web.controller.annotation; import javax.servlet.http.HttpServletRequest; impo ...
- HDUOJ-----2838Cow Sorting(组合树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- automake安装出错
automake命令出错 configure.ac:64: error: possibly undefined macro: AM_ICONV If this token and others ...
- evernote如何笔记共享
首先将你的笔记共享 访问你的共享笔记地址,就可看到共享笔记的内容了 https://www.evernote.com/pub/用户名/笔记名 https://www.evernote.com/ ...
- vi中全选的命令或者快捷方式
http://blog.163.com/boby_boke/blog/static/126877354200910308522382/网上有两种说法比较多:“:1,$y”和 “dG” 但是我查到有资料 ...
- OAF_OAF控件系列7 - Tree的实现(案列)
2014-06-02 Created By BaoXinjian