WinForm中DataGrid扩展类 - 快速导出Excel文件,带保存对话框,并杀死进程。相对完美的解决方案[转http://yuunagi.blogbus.com/logs/38361340.html]
来源:互联网 发布:python help 退出 编辑:程序博客网 时间:2024/04/28 15:20
using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace Xc_db_rygl
{
/// <summary>
/// ExportDataGrid 的摘要说明。
/// </summary>
public class ExportDataGrid
{
public DataGrid ExportGrid;
public Form ParentWindow=null;
/// <summary>
/// made by 江边孤鸟 who's email is jbgh608@163.com
/// </summary>
/// <param name="parentWindow">父亲窗口</param>
/// <param name="grid">要导出的DataGrid</param>
public ExportDataGrid(Form parentWindow,DataGrid grid)
{
ExportGrid=grid;
ParentWindow= parentWindow;
SaveFileDialog();
}
保存对话框
#region 导出Excel
public bool ExportExcel(string FilePath,string p_ReportName)
{
if ( this.ExportGrid.TableStyles.Count == 0 ) return false;
DataGridTableStyle ts = this.ExportGrid.TableStyles[0];
// 创建Excel对象
Excel.Application xlApp = new Excel.ApplicationClass();
if ( xlApp == null )
{
MessageBox.Show("Excel无法启动");
return false;
}
// 创建Excel工作薄
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];
// 设置标题
Excel.Range range = xlSheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,ts.GridColumnStyles.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = p_ReportName;
xlApp.ActiveCell.Font.Size = 20;
xlApp.ActiveCell.Font.Bold = true;
xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
// 列索引,行索引,总列数,总行数
int colIndex = 0;
int RowIndex = 0;
int colCount = ts.GridColumnStyles.Count;
int RowCount = this.ParentWindow.BindingContext[this.ExportGrid.DataSource,this.ExportGrid.DataMember].Count;
// 创建缓存数据
object[,] objData = new object[RowCount + 1, colCount ];
// 获取列标题
foreach(DataGridColumnStyle cs in ts.GridColumnStyles)
{
objData[RowIndex,colIndex++] = cs.HeaderText;
}
// 获取数据
for(RowIndex =1;RowIndex<=RowCount;RowIndex++)
{
for(colIndex=0;colIndex < colCount;colIndex++)
{
objData[RowIndex,colIndex] = this.ExportGrid[RowIndex-1,colIndex].ToString();
}
Application.DoEvents();
}
// 写入Excel
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Bold = true;
range = xlSheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount+2,colCount]);
range.Value2 = objData;
// 保存
try
{
xlApp.Cells.EntireColumn.AutoFit();
xlApp.Cells.VerticalAlignment = Excel.Constants.xlCenter;
xlApp.Cells.HorizontalAlignment = Excel.Constants.xlCenter;
//xlApp.Visible = true;
xlBook.Saved = true;
xlBook.SaveCopyAs(FilePath + ".xls");
MessageBox.Show("导出成功1!");
this.ParentWindow.Cursor=Cursors.Default;
}
catch
{
MessageBox.Show("保存出错,请检查!");
return false;
}
finally
{
xlApp.Quit();
GC.Collect();
KillProcess("excel") ;
}
return true;
}
#endregion
#region 杀死进程
private void KillProcess(string processName)
{
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//得到所有打开的进程
try
{
foreach (Process thisproc in Process.GetProcessesByName(processName))
{
thisproc.Kill();
}
}
catch(Exception Exc)
{
throw new Exception("",Exc);
}
}
#endregion
}
}
- WinForm中DataGrid扩展类 - 快速导出Excel文件,带保存对话框,并杀死进程。相对完美的解决方案[转http://yuunagi.blogbus.com/logs/38361340.html]
- C# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据;从Excel导入到DataGridView[转http://yuunagi.blogbus.com/logs/38361242.html]
- 提高SQL查询效率[转http://yuunagi.blogbus.com/logs/72011034.html]
- [C#]WinForm中DataGrid扩展 - 快速导出Excel文件
- [C#]WinForm中DataGrid扩展 - 快速导出Excel文件
- http://bigwhite.blogbus.com/logs/1347304.html
- i++ 原子操作的讨论 转自http://gcoder.blogbus.com/logs/75275569.html
- wget 用法详解 http://linux.blogbus.com/logs/46636997.html
- http://www.blogbus.com/eastsun-logs/7762285.html
- 关于pthread_mutex_t的初始化 http://www.blogbus.com/kavine-logs/46146510.html
- C程序内存分配(转自:http://lorsen.blogbus.com/logs/51374259.html)
- debian apt-get install的程序的安装目录如何确定 - [Ubuntu](转自:http://nicklj.blogbus.com/logs/33400628.html)
- excel2003导入时间到sqlserver2005(这篇比较好)(转自:http://wep2008.blogbus.com/logs/35390970.html)
- Visual Studio 2005/2008/2010 简单更换配色主题(转自:http://apit.blogbus.com/logs/84846293.html)
- 在Windows下对比学习Linux堆栈溢出http://att2.blogbus.com/logs/5134302.html
- openmeeting 改造成flash聊天室 http://fallenlord.blogbus.com/logs/16471632.html
- 实现拖放读取Excel2007数据表 转处:http://iamsam.blogbus.com/logs/37711535.html
- backtrace揪出segmentation fult【转至http://chenzhuoyou.blogbus.com/logs/35484209.html】
- memset
- C语言的谜题 (转载。。。
- memcpy
- 节假日安排:
- Delphi2010 中 Windows7新的 taskbar 功能的使用
- WinForm中DataGrid扩展类 - 快速导出Excel文件,带保存对话框,并杀死进程。相对完美的解决方案[转http://yuunagi.blogbus.com/logs/38361340.html]
- C# 类的可访问修饰符
- About Custom Draw /MFC
- Perl引用,解引用的对比总结(转)
- 把一本书变成一张薄纸的思维导图
- 用js javascript 创建网页快捷方式
- "大泥球"仍然是最常见的软件设计
- C# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据;从Excel导入到DataGridView[转http://yuunagi.blogbus.com/logs/38361242.html]
- ORACLE 导出,备份,exp sequence