C#Winform导出Excel
来源:互联网 发布:网络监控用什么软件 编辑:程序博客网 时间:2024/06/01 09:41
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms;using Microsoft.Office.Interop.Excel;namespace ERPSystem.Utility{ public 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); } } }}
转自:http://www.cnblogs.com/sydeveloper/archive/2012/03/19/2406558.html
0 0
- C#winForm导出excel
- C#Winform导出Excel
- C#Winform导出Excel
- C#winform导出EXCEL时的设置。
- C#Winform中从datagridview导出Excel
- winForm c#导出Excel
- WinForm导出excel
- WinForm GridView导出Excel
- winform导出excel
- WinForm中导出Excel
- winForm导入导出EXCEL
- C# WinForm导出Excel
- winform 导出excel文件
- winform导出Excel
- winform导出excel
- [C#]WinForm中DataGrid扩展 - 快速导出Excel文件
- [C#]WinForm中DataGrid扩展 - 快速导出Excel文件
- C#WinForm 使用StreamWriter导出数据成Excel文件。
- It is not safe to rely on the system's timezone settings.
- 查看,调整优先级和调度类别
- POJ 1850 Code (组合数学)
- 找工作之担心
- 关于C++类中访问权限的若干疑问(虚函数访问权限)
- C#Winform导出Excel
- 安卓模拟器BlueStacks+TCPdump对APP抓包分析
- 2.4.0HTML5头部和元信息
- 二分图基础理论说明
- Cocos2d-X 简单画图的具体实现
- 青蛙台阶跳
- UIView
- Qt之信号与槽
- 开源音频处理库SoundTouch的下载地址