C#Winform导出Excel
2012-03-19 20:02
501 查看
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
namespace SaleApplication.Class
{
class ExportExcelOfWinform
{
public static bool OutToExcelFromDataGridView(string title, DataGridView dgv, bool isShowExcel)
{
int titleColumnSpan = 0;//标题的跨列数
string fileName = "";//保存的excel文件名
int columnIndex = 1;//列索引
if (dgv.Rows.Count == 0)
return false;
/*保存对话框*/
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "导出Excel(*.xls)|*.xls";
sfd.FileName = title + DateTime.Now.ToString("yyyyMMddhhmmss");
if (sfd.ShowDialog() == DialogResult.OK)
{
fileName = sfd.FileName;
/*建立Excel对象*/
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
if (excel == null)
{
MessageBox.Show("无法创建Excel对象,可能您的计算机未安装Excel!");
return false;
}
try
{
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcel;
/*分析标题的跨列数*/
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible == true)
titleColumnSpan++;
}
/*合并标题单元格*/
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveSheet;
//worksheet.get_Range("A1", "C10").Merge();
worksheet.get_Range(worksheet.Cells[1, 1] as Range, worksheet.Cells[1, titleColumnSpan] as Range).Merge();
/*生成标题*/
excel.Cells[1, 1] = title;
(excel.Cells[1, 1] as Range).HorizontalAlignment = XlHAlign.xlHAlignCenter;//标题居中
//生成字段名称
columnIndex = 1;
for (int i = 0; i < dgv.ColumnCount; i++)
{
if (dgv.Columns[i].Visible == true)
{
excel.Cells[2, columnIndex] = dgv.Columns[i].HeaderText;
(excel.Cells[2, columnIndex] as Range).HorizontalAlignment = XlHAlign.xlHAlignCenter;//字段居中
columnIndex++;
}
}
//填充数据
for (int i = 0; i < dgv.RowCount; i++)
{
columnIndex = 1;
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv.Columns[j].Visible == true)
{
if (dgv[j, i].ValueType == typeof(string))
{
excel.Cells[i + 3, columnIndex] = "'" + dgv[j, i].Value.ToString();
}
else
{
excel.Cells[i + 3, columnIndex] = dgv[j, i].Value.ToString();
}
(excel.Cells[i + 3, columnIndex] as Range).HorizontalAlignment = XlHAlign.xlHAlignLeft;//字段居中
columnIndex++;
}
}
}
worksheet.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch { }
finally
{
excel.Quit();
excel = null;
GC.Collect();
}
//KillProcess("Excel");
return true;
}
else
{
return false;
}
}
private static void KillProcess(string processName)//杀死与Excel相关的进程
{
System.Diagnostics.Process myproc = new System.Diagnostics.Process();//得到所有打开的进程
try
{
foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
{
if (!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
}
catch (Exception Exc)
{
throw new Exception("", Exc);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
namespace SaleApplication.Class
{
class ExportExcelOfWinform
{
public static bool OutToExcelFromDataGridView(string title, DataGridView dgv, bool isShowExcel)
{
int titleColumnSpan = 0;//标题的跨列数
string fileName = "";//保存的excel文件名
int columnIndex = 1;//列索引
if (dgv.Rows.Count == 0)
return false;
/*保存对话框*/
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "导出Excel(*.xls)|*.xls";
sfd.FileName = title + DateTime.Now.ToString("yyyyMMddhhmmss");
if (sfd.ShowDialog() == DialogResult.OK)
{
fileName = sfd.FileName;
/*建立Excel对象*/
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
if (excel == null)
{
MessageBox.Show("无法创建Excel对象,可能您的计算机未安装Excel!");
return false;
}
try
{
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcel;
/*分析标题的跨列数*/
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible == true)
titleColumnSpan++;
}
/*合并标题单元格*/
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveSheet;
//worksheet.get_Range("A1", "C10").Merge();
worksheet.get_Range(worksheet.Cells[1, 1] as Range, worksheet.Cells[1, titleColumnSpan] as Range).Merge();
/*生成标题*/
excel.Cells[1, 1] = title;
(excel.Cells[1, 1] as Range).HorizontalAlignment = XlHAlign.xlHAlignCenter;//标题居中
//生成字段名称
columnIndex = 1;
for (int i = 0; i < dgv.ColumnCount; i++)
{
if (dgv.Columns[i].Visible == true)
{
excel.Cells[2, columnIndex] = dgv.Columns[i].HeaderText;
(excel.Cells[2, columnIndex] as Range).HorizontalAlignment = XlHAlign.xlHAlignCenter;//字段居中
columnIndex++;
}
}
//填充数据
for (int i = 0; i < dgv.RowCount; i++)
{
columnIndex = 1;
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv.Columns[j].Visible == true)
{
if (dgv[j, i].ValueType == typeof(string))
{
excel.Cells[i + 3, columnIndex] = "'" + dgv[j, i].Value.ToString();
}
else
{
excel.Cells[i + 3, columnIndex] = dgv[j, i].Value.ToString();
}
(excel.Cells[i + 3, columnIndex] as Range).HorizontalAlignment = XlHAlign.xlHAlignLeft;//字段居中
columnIndex++;
}
}
}
worksheet.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch { }
finally
{
excel.Quit();
excel = null;
GC.Collect();
}
//KillProcess("Excel");
return true;
}
else
{
return false;
}
}
private static void KillProcess(string processName)//杀死与Excel相关的进程
{
System.Diagnostics.Process myproc = new System.Diagnostics.Process();//得到所有打开的进程
try
{
foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
{
if (!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
}
catch (Exception Exc)
{
throw new Exception("", Exc);
}
}
}
}
相关文章推荐
- C#Winform导出Excel
- C#winform导出EXCEL时的设置。
- C# WinForm下DataGridView导出Excel的实现
- C#导出Excel源码(WinForm)
- winForm c#导出Excel
- C# WinForm DataTable 导出 Excel 多行表头、合并单元格
- C#数据库数据导入导出系列之四 WinForm数据库导入导出到Excel
- 【转】c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- C#winform从数据集导出Excel(带指定Excel样式)
- 【经典】C#winform从数据集导出Excel(带指定Excel样式) 转
- c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第一篇
- C# WINFORM Excel 导入导出 类
- C#使用winform简单导出Excel的方法
- C#winForm导出excel
- C# winform 导出导入Excel/Doc 完整实例教程 使用Aspose.Cells.dll——第三篇
- c#.net 使用NPOI导入导出标准Excel (asp.net winform csharp)
- c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- C# Winform实现数据的导出为txt或者CSV或者excel格式
- C# 将数据导出到Excel汇总Webform,Winform(转)