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

最新文章

  1. Android标题栏最右边添加按钮
  2. iOS常用手势识别器
  3. Javascript高级程序设计——面向对象之创建对象
  4. # java对xml文件的基本操作
  5. JS---------IIFE(Imdiately Invoked Function Expression 立即执行的函数表达式)
  6. Java 报表之JFreeChart(第二讲)
  7. MyBatis知多少(15)数据模型
  8. mobileTech
  9. MySQL之聚合
  10. mysql问题Connection using old (pre-4.1.1) authentication protocol refused (client option &#39;secure_auth&#39; enabled)的解决方法
  11. 在Qt中使用sleep(包含为win and *nix下sleep函数的实现及用法)
  12. PHP程序开发人员要掌握的知识
  13. 80端口被NT kernel &amp; System 占用pid= 4的解决方法
  14. redis主从配置+哨兵模式
  15. jQuery遍历table中的tr td并获取td中的值
  16. Linux串口通信之termios结构体说明
  17. 【XAF问题】如何判断这个对象的进出类型
  18. POJ2484
  19. requests SSLError: hostname &#39;ccc.xxx.com&#39; doesn&#39;t match &#39;*.b0.upaiyun.com&#39;
  20. oh-my-zsh的安装与基本配置

热门文章

  1. sql 投影查询
  2. linux IPC的PIPE
  3. 深入理解js——构造函数的继承
  4. phpstorm使用说明
  5. 解决spring boot中普通类中使用service为null 的方法
  6. HTTP与HTTPS的区别与联系
  7. java.util.Arrays,java.lang.Math,java.lang.System 类的常用方法汇总
  8. FreeBSD_11-系统管理——{Part_9-SVN}
  9. Java服务定位器模式
  10. JQuery日记6.7 Javascript异步模型(二)