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的更多相关文章

  1. Winform导入Excel数据到数据库

    public partial class ImportExcel : Form { AceessHelpers accessHelper = new AceessHelpers(); public I ...

  2. winform导入excel或者csv

    if (txt01.Text != "") { this.lbzhantie.Items.Clear(); this.dtzhuanhuo.Rows.Clear(); if (tx ...

  3. C#使用NOPI导入Excel

    使用NOPI导入Excel文档 NOPI版本:2.3.0,依赖于NPOI的SharpZipLib版本:0.86,经测试适用于.net4.0+ 记录遇到的几个问题 NOPI中的IWorkbook接口:x ...

  4. C# winform 编程 向ACCESS数据库导入EXCEL表使用心得

    public string MyConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ErLake.mdb&quo ...

  5. [Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!

    引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...

  6. 利用Aspose.Cell控件导入Excel非强类型的数据

    导入Excel的操作是非常常见的操作,可以使用Aspose.Cell.APOI.MyXls.OLEDB.Excel VBA等操作Excel文件,从而实现数据的导入,在导入数据的时候,如果是强类型的数据 ...

  7. 转:[Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!

    引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...

  8. Winforn中导入Excel并显示然后获取多选框选中的内容

    场景 使用NPOI导入Excel并赋值给DataTable,然后显示在DataGrdView上,并且添加多选框,然后获取选中行的内容. Winform中使用NPOI实现Excel导入并赋值给DataT ...

  9. C#导出和导入Excel模板功能

    引用  Aspose.Cells; 基于WinForm 导入 private void btn_excel_input_Click(object sender, EventArgs e) { try ...

随机推荐

  1. Mac上安装Android SDK

    今天开始学习IOS,所以先买了个设备先,但是开始使用了苹果本,还是需要继续开发Android,因为那是我现在吃饭的东西,所以就需要在Mac上配置Android SDK,原以为安装SDK很简单,和Win ...

  2. iptables对请求的URL作IP访问控制

    服务器运行环境是Tomcat,现在要实现的目的是,只允许特定的IP访问某个目录,一种方法是在tomcat配置文件server.conf中,使用RemoteAddrValve对虚拟主机做访问控制.另外一 ...

  3. Ext 面板(Panel)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. python re 正則匹配規則

  5. 洛谷 P3803 【模板】多项式乘法(FFT)

    题目链接:P3803 [模板]多项式乘法(FFT) 题意 给定一个 \(n\) 次多项式 \(F(x)\) 和一个 \(m\) 次多项式 \(G(x)\),求 \(F(x)\) 和 \(G(x)\) ...

  6. canvas 画一条折线

    设置画布对象 canvas id="myCanvas" ref="canvas" //获取Canvas对象(画布) var canvas = document. ...

  7. Aggregate report 聚合报告

  8. HTML5和CSS3工具资源汇总

    HTML5 & CSS3 准备就绪 该网站通过扇形图表的形式展现了从08年到10年以来各大浏览器对HTML5和CSS3的支持情况.发展势头还是很可观的. HTML5安全手册 CSS3按钮生成器 ...

  9. D3.js 弦生成器(V3版本)

    弦生成器(Chord Generator)   弦生成器(Chord Generator)根据两段弧来绘制弦,共有五个访问器,分别为source().target().radius().startAn ...

  10. forEach方法

    *forEach() * -这个方法只支持ie8以上的浏览器 * -forEach方法需要一个函数作为参数 * -像这种函数,由我们创建但是不由我们调用,我们称为回调函数 * 数组中由几个元素函数就会 ...