本文介绍了将Excel工作表导入c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我从excel表导入数据并将数据绑定到网格视图时出错。
错误:无法找到可安装的ISAM。
我认为问题出在连接字符串中。
这是我的代码:
Hello every one, I am getting an error while importing data from excel sheet and binding that data to grid view.
Error: Could not find installable ISAM.
I think problem is in connection string.
This is my code:
string path = fileupload.PostedFile.FileName;
string strmail = string.Empty;
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 8.0;HDR=YES;ReadOnly=False;";
OleDbConnection objConn = new OleDbConnection(connectionString);
objConn.Open();//Here is the error
推荐答案
public DataSet GetDataFromExcel(string filePath)
{
try
{
string strConn;
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
DataTable dt = new DataTable();
dt = null;
using (OleDbConnection oleDB = new OleDbConnection(strConn))
{
oleDB.Open();
dt = oleDB.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
return null;
ListItemCollection items = new ListItemCollection();
int i = 0;
//if (dt.Rows.Count > 1)
//return null;
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
string excelSheetName;
string lastCharacter = "";
excelSheetName = dt.Rows[rowIndex]["TABLE_NAME"].ToString();
excelSheetName = excelSheetName.Replace("''", "");
lastCharacter = excelSheetName.Substring(excelSheetName.Length - 1, 1);
if (lastCharacter == "
这篇关于将Excel工作表导入c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!