Winform datagridview Excel 导入导出
来源:互联网 发布:淘宝网数据包下载 编辑:程序博客网 时间:2024/05/01 21:48
public class ExcelHelper
{
#region DataGidView数据到导出excel
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "EXCEL文件|*.xls";
if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
if (excelapp == null)
{
MessageBox.Show("无法创建Excel对象,可能你的机器上没有安装Excel!");
return;
}
Microsoft.Office.Interop.Excel.Workbook wbk = excelapp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet wst = (Microsoft.Office.Interop.Excel.Worksheet)wbk.Worksheets[1];
try
{
//写入标题
for (int i = 0; i < myDGV.ColumnCount; i++)
{
wst.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
}
//写入数据
for (int r = 0; r < myDGV.Rows.Count; r++)
{
for (int i = 0; i < myDGV.ColumnCount; i++)
{
wst.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
}
System.Windows.Forms.Application.DoEvents();
}
wst.Columns.EntireColumn.AutoFit();//列宽自适应
wbk.SaveAs(savefile.FileName);
MessageBox.Show("导出成功");
}
catch (Exception ex)
{
MessageBox.Show("导出失败:"+ex.Message,"导出异常");
}
//资源释放
System.Runtime.InteropServices.Marshal.ReleaseComObject(wst);
wst = null;
wbk.Close(null,null,null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbk);
wbk = null;
excelapp.Workbooks.Close();
excelapp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelapp);
excelapp = null;
System.GC.Collect();
}
}
#endregion
#region Excel文件数据导入DataGidView
/// <summary>
/// 读取excel内的数据,返回DataTable
/// </summary>
/// <param name="strExcelFileName"></param>
/// <returns></returns>
public DataTable ImportExcel(string strExcelFileName)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + strExcelFileName + "';Excel 8.0; HDR=YES;IMEX=1;";
string strExcel = string.Format("select * from [{0}$]", "sheet1");
DataSet ds = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, "sheet1");
return ds.Tables["sheet1"];
}
}
#endregion
}
- Winform datagridview Excel 导入导出
- datagridview导入导出excel
- [WinForm]DataGridView直接导出EXCEL
- WinForm DataGridView导出到Excel
- [WinForm]dataGridView导出到EXCEL
- winForm导入导出EXCEL
- C# winform DataGridView导出到EXCEL
- WinForm下DataGridView导出Excel的实现
- Winform的DataGridView导出到Excel
- C# WinForm下DataGridView导出生成Excel
- winform DataGridView 导出到Excel表格
- winform中DataGridView导出到Excel
- C#Winform中从datagridview导出Excel
- C# winform 将dataGridView导出到excel
- DataGridView从excel导入、导出到excel
- WinForm中DataGridView数据导入Excel
- winform DataGridView的数据导入Excel
- Winform将DataGridView导出Excel,调用打印机打印DataGridView
- 新一代的数据库审计解决方案--Oracle数据库
- spring +web
- Apache HTTP Server 与 Tomcat 的三种连接方式介绍
- 关于财务公司软件
- 获取手机中的图片
- Winform datagridview Excel 导入导出
- 孙鑫VC学习笔记:第五讲 文本编程
- 动态获取Struts2执行动作的Method Name
- 虚拟列表控件(CListCtrl)
- web.xml文件解析,以及tomcat启动常见错误总结一哈。
- VS2005调试技巧集合
- PHP中的if
- 简单计算器(hdu 1237)
- Linux内核spin_lock与spin_lock_irq分析