C#读取Excel导入到数据库及读取Excel工作表为任意表名的方法

摘要:
EventArgse){stringressultFile=“”;excel Str)){try{myCommand.Fill(dt);paramname=“excel FileName”>excel表名<if(File.Exists(excel FileName)){{stringexcelStr=“Provider=Microsoft.Ace.OleDB.12.0;IMEX=1'”;

添加openFileDialog1,用于选择Excel表

using System.IO;
using System.Data.OleDb;

//导入Excel表
private void btnInto_Click(object sender, EventArgs e)
{

string resultFile = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "表格文件(*.xls,*.xlsx)|*.xls;*.xlsx";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
resultFile = openFileDialog1.FileName;

GetExcel(resultFile);

}
}
/// <summary>
/// 获取Excel表格内容
/// </summary>
/// <param name="fileName">Excel表名字</param>
private void GetExcel(string fileName)
{

根据表名创建链接字符串
string excelStr = "Provider= Microsoft.Ace.OleDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";

string strSheetName = GetExcelFirstTableName(fileName);//获取第一个工作表名字
if (strSheetName!=null)
{

DataTable dt = new DataTable();

using (System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter("select * from[" + strSheetName + "]", excelStr))
{
try
{
myCommand.Fill(dt);

}
catch (Exception ex)
{

MessageBox.Show("操作失败" + ex.Message);
}

}
dgvShow.DataSource = dt;


}
else
{
MessageBox.Show("这是张空表!");
}

}


/// <summary>
/// 获取excel第一个工作表名字
/// </summary>
/// <param name="excelFileName">Excel表名字</param>
/// <returns></returns>
public static string GetExcelFirstTableName(string excelFileName)
{
string tableName = null;
if (File.Exists(excelFileName))
{
string excelStr = "Provider= Microsoft.Ace.OleDB.12.0;Data Source=" + excelFileName + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
using (OleDbConnection conn = new OleDbConnection(excelStr))
{
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);//获取table内容
tableName = dt.Rows[0][2].ToString().Trim();
}
}
return tableName;
}

免责声明:文章转载自《C#读取Excel导入到数据库及读取Excel工作表为任意表名的方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux下的Jenkins作为hub,Windows作为node节点,在Android手机上执行自动化脚本Linux、Windows如何进行性能监控与调优下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

MVC4 js里给对象赋值

如果需要用js给control传一个对象,那么对象的属性在c#的model必须加public,不然在js赋值时赋不了的,但是不报错,等你调试到control时,这些属性无聊如何都是null,这样会很郁闷的。 controller方法: [HttpPost] public JsonResult SaveSelectedAcPoint(Acu...

信贷业务(Ali)

1、信贷业务视角   信贷业务主要有两个视角,借款人和出资机构。借款人关心借多少钱,还多少钱,多少利息;机构关心信贷资产风险,收益。   领域模型上两个视角分开:个人--->账单、机构--->资产。出资机构可以多样化(机构对接)。多一个机构(多一种类型的对接)。账单--机构资产--机构对接。借记账务,贷记账务,资产账务,资信调查,信贷管理。 2...

PE注入

1 // PE注入.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 6 #include <windows.h> 7 8 #include <tlhelp32.h> 9 10 #include <process.h>...

吴裕雄--天生自然JAVAIO操作学习笔记:RandomAccessFile

import java.io.File ; import java.io.RandomAccessFile ; public class RandomAccessFileDemo01{ // 所有的异常直接抛出,程序中不再进行处理 public static void main(String args[]) throws Exception...

jackson实体转json时 为NULL不参加序列化的汇总

首先加入依赖<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId></dependency> 方法一、实体上使用 @JsonInclude(JsonInclude.Include....

C# https证书通信Post/Get(解决做ssl通道时遇到“请求被中止: 未能创建 SSL/TLS 安全通道”问题)

1 public static string HttpPost(string url, string param = null) 2 { 3 HttpWebRequest request; 4 5 //如果是发送HTTPS请求 6 i...