[zz]winform导入excel
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 = ;
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 > )
{
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[].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 = , 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
[zz]winform导入excel的更多相关文章
- Winform导入Excel数据到数据库
public partial class ImportExcel : Form { AceessHelpers accessHelper = new AceessHelpers(); public I ...
- winform导入excel或者csv
if (txt01.Text != "") { this.lbzhantie.Items.Clear(); this.dtzhuanhuo.Rows.Clear(); if (tx ...
- C#使用NOPI导入Excel
使用NOPI导入Excel文档 NOPI版本:2.3.0,依赖于NPOI的SharpZipLib版本:0.86,经测试适用于.net4.0+ 记录遇到的几个问题 NOPI中的IWorkbook接口:x ...
- C# winform 编程 向ACCESS数据库导入EXCEL表使用心得
public string MyConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ErLake.mdb&quo ...
- [Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!
引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...
- 利用Aspose.Cell控件导入Excel非强类型的数据
导入Excel的操作是非常常见的操作,可以使用Aspose.Cell.APOI.MyXls.OLEDB.Excel VBA等操作Excel文件,从而实现数据的导入,在导入数据的时候,如果是强类型的数据 ...
- 转:[Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!
引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...
- Winforn中导入Excel并显示然后获取多选框选中的内容
场景 使用NPOI导入Excel并赋值给DataTable,然后显示在DataGrdView上,并且添加多选框,然后获取选中行的内容. Winform中使用NPOI实现Excel导入并赋值给DataT ...
- C#导出和导入Excel模板功能
引用 Aspose.Cells; 基于WinForm 导入 private void btn_excel_input_Click(object sender, EventArgs e) { try ...
随机推荐
- 阿里小程序Serverless 操作指南
小程序云 小程序云(Mini Program Cloud)是阿里云面向小程序场景提供的一站式云服务,帮助开发者实现一云多端的业务战略,提供了有服务器和无服务器两种模式.云应用是有服务器模式,提供了包括 ...
- PHP FILTER_SANITIZE_SPECIAL_CHARS 过滤器
定义和用法 FILTER_SANITIZE_SPECIAL_CHARS 过滤器对特殊字符进行 HTML 转义. 该过滤器用于转义 "<>& 以及 ASCII 值在 32 ...
- linux IPC 消息队列
消息队列函数原型 在建立IPC通讯时(如消息队列,共享内存)必须建立一个ID值.通常情况下,这个ID值由ftok函数得到 #inlcude <sys/types.h> #include & ...
- 离线+生成树+并查集——cf1213G
#include<bits/stdc++.h> using namespace std; #define N 200005 #define ll long long struct Edge ...
- Hibernate 和 JPA 注解
转载请注明:Hibernate 和 JPA 注解 | 言曌博客 1.@Entity(name="EntityName") 必须, name为可选,对应数据库中一的个表 2.@Tab ...
- cesium安装及第一个示例
cesium安装及第一个示例 一.环境要求 二.浏览器要求 三.安装node.js 四.下载cesium包(地址为https://cesiumjs.org) 包括了 五.在你的项目里引入相关js与cs ...
- Redis数据结构之整数集合-intset
当一个集合只包含整数值元素,并且这个集合的元素数量不多时,Redis会使用整数集合(intset)来存储集合元素. intset是紧凑的数组结构,同时支持16位.32位和64位整数. 结构 struc ...
- span里面插入文字
.text-box span::before{ content:attr(data-text);}
- (Struts2学习系列五)Struts2默认action
当我们访问项目下一个不存在的Action的时候,页面就会报错,404找不到资源,这样对用户来说是非常不友好的,所以我们设置一个默认的Action,当找不到对应Action的时候,就会跳转到默认Acti ...
- iOS录音后播放声音变小的解决方法
目前需求是录音之后再播放出来.经常会出现播放声音变很小的情况. 解决方法: if (recorder.recording){ [recorder stop]; } [[AVAudioSession s ...