[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 ...
随机推荐
- table标签详解
1.table标签中没有 tbody标签,浏览器会自动加上去的 2.一般表格的布局可以不使用 thead.tfoot 以及 tbody 元素.这样浏览器解析时会自动给一个 tbody标签的. 完整的 ...
- TotoiseSVN + VisualSVN Server 使用
1.SVN中 项目文件版本 分服务器的版本和本地的版本.服务器版本(SVN会自动给每个版本加版本号的)永远都是最新的. 2.svn的更新,在文件不冲突的时候,会自动将服务器的版本和本地的版本合并. ...
- (转)短信vs.推送通知vs.电子邮件:app什么时候该用哪种方式来通知用户?
转:http://www.360doc.com/content/15/0811/00/19476362_490860835.shtml 现在,很多公司都关心的一个问题是:要提高用户互动,到底采取哪一种 ...
- svn启动服务
bin目录添加到环境变量classpathsvn --version 查看版本svnadmin create D:\\xx 创建本地中央仓库启动svn服务 cmd命令 svnserve -d -r D ...
- git分布式版本控制系统权威指南学习笔记(一):配置文件、简单流程和小问题
文章目录 git配置文件简介 git config各种命令 配置级别: 用户信息 文本编辑器 差异分析工具 配置命令别名 公钥 git协同流程 简单流程 初始化版本库 提交至缓存区 查看状态 提交分支 ...
- Linux内核代码布局
上文参考:http://www.cnblogs.com/long123king/p/3545991.html 先分析一下linker script的语法. #ifdef CONFIG_X86_32#d ...
- error C4996: 'getcwd': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getcwd. See online help for details. c:\users\12968\desktop\testapp\testapp\testapp.c
解决办法: 属性>C/C++>预处理器定义>分别输入: _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE >保存退出即可
- python去除rpm仓库中同名低版本的包
编程思路1 遍历目标路径的rpm包并保存特性包列表: 2 利用python模块rpmUtils提取RPM包的特征信息:包名 版本号 架构 3 遍历特性列表中存在重复包名的rpm, 将低版本的rpm包 ...
- 32-Ubuntu-用户权限-03-修改文件权限
chmod 简介 chmod可以修改用户或组对文件或目录的权限. 命令格式如下: chmod +/-rwx 文件名/目录名 修改文件权限 例:demo.txt 1.增加权限 例:增加demo.txt的 ...
- 拓展KMP求回文串
题目:hdu3613: 题意:有26字母对应的价值,然后给出以个串,把它分成两段字串,如果字串是回文串,串的价值就是每个字符和,不是就为0.求最大价值. 博客 分析:拓展KMP的应用求回文字串. #i ...