[zz]winform导入excel
来源:互联网 发布:深入理解java虚拟机pdf 编辑:程序博客网 时间:2024/06/12 00:07
winfrom导入excel内容,要求能够excel中多个工作簿的内容。代码如下:
#region 导入excel数据 private void button2_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "表格文件 (*.xls)|*.xls"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog() == DialogResult.OK) { Import(openFileDialog.FileName); } } /// <summary> /// 导入excel数据 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static bool Import(string filePath) { try { //Excel就好比一个数据源一般使用 //这里可以根据判断excel文件是03的还是07的,然后写相应的连接字符串 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection con = new OleDbConnection(strConn); con.Open(); string[] names = GetExcelSheetNames(con); if (names.Length > 0) { foreach (string name in names) { OleDbCommand cmd = con.CreateCommand(); cmd.CommandText = string.Format(" select * from [{0}]", name);//[sheetName]要如此格式 OleDbDataReader odr = cmd.ExecuteReader(); while (odr.Read()) { if (odr[0].ToString() == "序号")//过滤列头 按你的实际Excel文件 continue; //数据库添加操作 /*进行非法值的判断 * 添加数据到数据表中 * 添加数据时引用事物机制,避免部分数据提交 * Add(odr[1].ToString(), odr[2].ToString(), odr[3].ToString());//数据库添加操作,Add方法自己写的 * */ } odr.Close(); } } return true; } catch (Exception) { return false; } } /// <summary> /// 查询表名 /// </summary> /// <param name="con"></param> /// <returns></returns> public static string[] GetExcelSheetNames(OleDbConnection con) { try { System.Data.DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new[] { null, null, null, "Table" });//检索Excel的架构信息 var sheet = new string[dt.Rows.Count]; for (int i = 0, j = dt.Rows.Count; i < j; i++) { //获取的SheetName是带了$的 sheet[i] = dt.Rows[i]["TABLE_NAME"].ToString(); } return sheet; } catch { return null; } } //下面这种方法获取excel Worksheets Name时,提示无法访问该exceL文件,所以改为上面获取工作簿名的方式 ///// <summary> ///// 获得excel sheet所有工作簿名字 ///// </summary> ///// <param name="filePath"></param> ///// <returns></returns> //public static string[] GetExcelSheetNames(string filePath) //{ // Microsoft.Office.Interop.Excel.ApplicationClass excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); // Microsoft.Office.Interop.Excel.Workbooks wbs = excelApp.Workbooks; // Microsoft.Office.Interop.Excel.Workbook wb = wbs.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, // Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, // Type.Missing, Type.Missing, Type.Missing, Type.Missing); // int count = wb.Worksheets.Count; // string[] names = new string[count]; // for (int i = 1; i <= count; i++) // { // names[i - 1] = ((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[i]).Name; // } // return names; //} #endregion
阅读全文
0 0
- [zz]winform导入excel
- winForm导入导出EXCEL
- WinForm Excel导入
- Winform datagridview Excel 导入导出
- winform中导入excel表格
- Winform Excel导入Oracle数据库
- [zz]使用OleDb,将Excel导入DataSet
- excel 导入导出access数据库(winform)
- winform导入excel到access数据库中
- WinForm中DataGridView数据导入Excel
- C# winform 中如何导入Excel
- Winform下的 Excel导入数据库数据
- C# WinForm下Excel导入导出
- c# winform 中导入Excel 的问题 !
- C# WINFORM Excel 导入导出 类
- C# WinForm下Excel导入导出
- winform导入excel到access数据库中
- winform DataGridView的数据导入Excel
- 自己总结下颜色表
- python 语法细节
- WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
- Windows7安装JDK的环境变量设置javac不是内部命令或外部命令
- [zz]使用OleDb,将Excel导入DataSet
- [zz]winform导入excel
- JS中return的用法及return;return false;return true的区别
- 【阿柟的复习计划】暑假要做些什么?
- C#对多个集合和数组的操作(合并,去重,判断)
- Java官方教程
- Java8 jvm参数简介
- 上传自己的库到github并作为依赖
- [zz]winform 窗体关闭事件
- 兼容IE浏览器的时间格式