External table is not in expected format - c#

I have to upload a Excel Sheet in document library and have to show all the details of list in gridview. But I am getting the error: "external table is not in expected format":
My code is:
void uploadDocument()
{
try
{
string publicFSdocLibrary = "PublicFSdoc";
if (uploadDoc.HasFile)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite oSite = new SPSite("http://chdsez301298d:1000/sites/Test/"))
//using (SPSite oSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb myWeb = oSite.OpenWeb())
{
SPListItem lstItem = SPContext.Current.ListItem;
myWeb.AllowUnsafeUpdates = true;
//SPList docList = myWeb.Lists[docLibrary];
SPFolder finalDocument = myWeb.Folders[publicFSdocLibrary];
// Prepare to upload
Boolean replaceExistingFiles = true;
string fileName = Path.GetFileName(uploadDoc.FileName);
filePath = Path.GetFullPath(uploadDoc.PostedFile.FileName);
// string newFilePath = getFilePath(filePath);
FileStream fileStream = System.IO.File.OpenRead(filePath);
// Upload document
SPFile spfile = finalDocument.Files.Add(getFileName(), fileStream, replaceExistingFiles);
SPListItem item = spfile.Item;
item["RequestNo"] = generateRequestNo();
item["AssessmentType"] = "Financial Solvency";
// Commit all changes
item.Update();
//Update document url to Assessment request list
myWeb.AllowUnsafeUpdates = false;
}
}
});
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
string getFileName()
{
string newFileName = generateRequestNo() + ".xlsx";
return newFileName;
}
void btnUpload_Click(object sender, EventArgs e)
{
uploadDocument();
getExcelData();
//savePublicFS();
//addAssesmentWfRequest();
//CloseForm();
//Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.close();", true);
}
void getExcelData()
{
try
{
if (uploadDoc.HasFile)
{
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
//string sheetName = "Sourcing Questionnaire";
string query = null;
string connString = "";
string fileName = Path.GetFileName(uploadDoc.FileName);
string strFileType = System.IO.Path.GetExtension(uploadDoc.FileName).ToString().ToLower();
string strNewPath = filePath;
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
// query = "SELECT * FROM [" + sheetName + "$]";
query = "SELECT * FROM [abc$]";
//Create the connection object
conn = new OleDbConnection(connString);
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
cmd = new OleDbCommand(query, conn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
gvdetails.DataSource = ds.Tables[0];
gvdetails.DataBind();
da.Dispose();
conn.Close();
conn.Dispose();
//return ds;
}
}
catch (Exception)
{
throw;
}
}
private String generateRequestNo()
{
try
{
String requestNo = String.Empty;
if (txtSupplierName.Text.Length <= 4)
{
requestNo = txtSupplierName.Text
+ "-" + requestNumber()
+ DateTime.Today.ToString("ddMMyyyy");
}
else
{
requestNo = txtSupplierName.Text.Substring(0, 4)
+ "-" + requestNumber()
+ DateTime.Today.ToString("ddMMyyyy");
}
return requestNo;
}
catch (Exception)
{
throw;
}
}
private String requestNumber()
{
try
{
String number = String.Empty;
number = "FS-";
return number;
}
catch (Exception)
{
throw;
}
}
I am getting error in getExcelData() method at line cmd = new OleDbCommand(query, conn);
Please help me as I struck here since last week.

Related

retrieve the email address from radio button data source options

I have radio buttons from which I can select the type of data source to retrieve the email addresses of recipients.
if (RadioButton1.Checked)
{
sql.Open();
string s = "select * from address";
SqlCommand t = new SqlCommand(s, sql);
t.ExecuteNonQuery();
sql.Close();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT address FROM address1";
cmd.Connection = sql;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
sql.Open();
da.Fill(dt);
sql.Close();
foreach (DataRow row in dt.Rows)
{
Msg.To.Add(row["address"].ToString());
}
}
else if (RadioButton2.Checked)
{
string connectionString = "";
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
FileUpload1.SaveAs(fileLocation);
if (fileExtension == ".xls")
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fileExtension == ".xlsx")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT address FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dt);
con.Close();
foreach (DataRow row in dt.Rows)
{
Msg.To.Add(row["address"].ToString());
}
}
}
else if (RadioButton3.Checked)
{
if (FileUpload2.HasFile)
{
string fileName = Path.GetFileName(FileUpload2.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
FileUpload2.SaveAs(fileLocation);
StreamReader sr = new StreamReader(fileLocation);
String line = sr.ReadToEnd();
string[] toAddressArray;
toAddressArray = line.Split(new char[] { ' ' });
foreach (string a in toAddressArray)
{
Msg.To.Add(a);
}
}
}
Now, what I want is when the recipient clicks the link in the html page which I have sent as mail body, I want to retrieve the mail id of the recipient which is from one of the three radio button data source options.
How to get the mail id?
Here is the complete code:
SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["mystring"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
getFiles();
if (!IsPostBack)
{
var list = getFiles();
dd1.DataSource = list;
dd1.DataTextField = "Key";
dd1.DataValueField = "Value";
dd1.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
{
SendHTMLMail();
}
void SendHTMLMail()
{
StreamReader reader = new StreamReader(dd1.SelectedItem.Value);
string readFile = reader.ReadToEnd();
Regex regx = new Regex("(?<!src=\")http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*([a-zA-Z0-9\\?\\#\\=\\/]){1})?", RegexOptions.IgnoreCase);
string output = regx.ToString();
string count = 0.ToString();
output = readFile;
string username = Server.UrlEncode(this.txtUsername.Text);
output = regx.Replace(output, new MatchEvaluator((match) =>
{
var url = Uri.EscapeDataString(match.Value.ToString());
return $"http://localhost:61187/two?sender={username}&link={url}&count={count}";
}));
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress(txtUsername.Text);
Msg.Subject = txtSubject.Text;
Msg.Body = output.ToString();
Msg.IsBodyHtml = true;
if (fuAttachment.HasFile)
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
Msg.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
if (RadioButton1.Checked)
{
sql.Open();
string s = "select * from address";
SqlCommand t = new SqlCommand(s, sql);
t.ExecuteNonQuery();
sql.Close();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT address FROM address1";
cmd.Connection = sql;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
sql.Open();
da.Fill(dt);
sql.Close();
foreach (DataRow row in dt.Rows)
{
Msg.To.Add(row["address"].ToString());
}
}
else if (RadioButton2.Checked)
{
string connectionString = "";
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
FileUpload1.SaveAs(fileLocation);
if (fileExtension == ".xls")
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fileExtension == ".xlsx")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT address FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dt);
con.Close();
foreach (DataRow row in dt.Rows)
{
Msg.To.Add(row["address"].ToString());
}
}
}
else if (RadioButton3.Checked)
{
if (FileUpload2.HasFile)
{
string fileName = Path.GetFileName(FileUpload2.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
FileUpload2.SaveAs(fileLocation);
StreamReader sr = new StreamReader(fileLocation);
String line = sr.ReadToEnd();
string[] toAddressArray;
toAddressArray = line.Split(new char[] { ' ' });
foreach (string a in toAddressArray)
{
Msg.To.Add(a);
}
}
}
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
Msg = null;
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
}
public List<KeyValuePair<string, string>> getFiles()
{
DirectoryInfo di = new DirectoryInfo(HostingEnvironment.MapPath("/App_Data"));
var fi = di.EnumerateFiles("*.html", SearchOption.TopDirectoryOnly);
var files = new List<KeyValuePair<string, string>>();
foreach (FileInfo file in fi)
{
files.Add(new KeyValuePair<string, string>(file.Name, file.FullName));
}
return files;
}

Unable to import excel to DB table in server

I am using the below code to import excel data into DB table. The code works fine in my local and when I move this code to server the import fails in between. Also I don't get any error messages and I get a message data saved successfully.
For Ex: The excel has 75,000 data's and only 13,500 records are getting inserted and the size of the excel file is 5 MB.
Any suggestions about the possible problems?
CS:
protected void btnImportData_Click(object sender, EventArgs e)
{
try
{
string strCS = string.Empty; ;
string strFileType = Path.GetExtension(FileUploadExcel.FileName).ToLower();
string query = "";
lblError.Text = "";
string FileName = string.Empty;
FileName = Path.GetFileName(FileUploadExcel.PostedFile.FileName);
string Extension = Path.GetExtension(FileUploadExcel.PostedFile.FileName);
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string path = Path.GetFileName(Server.MapPath(FileUploadExcel.FileName));
System.IO.File.Delete(Server.MapPath(FolderPath) + path);
FileUploadExcel.SaveAs(Server.MapPath(FolderPath) + path);
string filePath = Server.MapPath(FolderPath) + path;
if (strFileType != String.Empty)
{
if (strFileType.Trim() == ".xls")
{
strCS = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
strCS = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please upload the correct file format')", true);
return;
}
try
{
OleDbConnection conn = new OleDbConnection(strCS);
if (conn.State == ConnectionState.Closed)
conn.Open();
System.Data.DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetname = dt.Rows[0]["Table_Name"].ToString();
query = "SELECT * FROM [" + sheetname + "]";
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn = null;
}
string strSqlTable = "TABLENAME";
string sclearsql = "delete from " + strSqlTable;
SqlConnection sqlconn = new SqlConnection(strCon);
SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
sqlconn.Open();
sqlcmd.ExecuteNonQuery();
sqlconn.Close();
OleDbConnection oledbconn = new OleDbConnection(strCS);
oledbconn.Open();
OleDbCommand oledbcmd = new OleDbCommand(query, oledbconn);
oledbcmd.CommandTimeout = 120;
OleDbDataReader dReader = oledbcmd.ExecuteReader();
SqlBulkCopy bulkCopy = new SqlBulkCopy(strCon);
bulkCopy.DestinationTableName = strSqlTable;
bulkCopy.BulkCopyTimeout = 120;
bulkCopy.BatchSize = 1000;
bulkCopy.WriteToServer(dReader);
oledbconn.Close();
ClientScript.RegisterStartupScript(Page.GetType(), "alert", "alert('Data saved successfully');window.location='Panel.aspx';", true);
}
catch (Exception ex)
{
lblError.Text = "Upload status: The file could not be uploaded due to following reasons.Please check: " + ex.Message;
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please select a file to import the data.')", true);
}
}
catch (Exception ex)
{
lblError.Text = "Upload status: The file could not be uploaded due to following reasons.Please check: " + ex.Message;
}
}
Hi remove the data reader on you code which read the excel file twice and use the dataset to fill out your SQL table:
See below:
DataTable dtexcel = new DataTable();
using (OleDbConnection conn = new OleDbConnection(strCS))
{
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
//Looping a first Sheet of Xl File
DataRow schemaRow = schemaTable.Rows[0];
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
string query = "SELECT * FROM [" + sheet + "]";
OleDbDataAdapter daexcel = new OleDbDataAdapter(query, conn);
dtexcel.Locale = CultureInfo.CurrentCulture;
daexcel.Fill(dtexcel);
}
}
using (SqlConnection sqlconn = new SqlConnection(strCon))
{
SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
sqlconn.Open();
sqlcmd.ExecuteNonQuery();
}
using (SqlConnection sqlconn = new SqlConnection(strCon))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlconn))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = strSqlTable;
sqlconn.Open();
sqlBulkCopy.WriteToServer(dtexcel);
}
}
Have you tried using the dts wizard? it is a nice tool if you have a excel sheet with data.
you use it by opening the command prompt. windowsbutton + r, and the write cmd.
and from in here you write dtswizard
then a wizard opens in a new window where you can select the excel sheet that you want to insert. There is many tutorials on google.
Hope you can use this

Read excel file data in batches

I have an excel file that contains about 1.5 million records. My intention is to read first 100 000 records from the excel file into my datatable (here c# datatable i.e dt) do some processing on these records, then read the next 100000 records and so on until I have fetched all the records in excel file.(each time only 100000 rows)
Currently I am fetching all records using below code
public bool ReadDataFile(string filePath)
{
string strConnString = null;
string sheetName = null;
string ErrSheetName = null;
DataSet dsEx = new DataSet();
strConnString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + filePath + ";Mode=ReadWrite;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(strConnString);
try
{
conn.Open();
DataTable DtSheetName = new DataTable();
DataTable dt = new DataTable();
DtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
for (int i = 0; i <= DtSheetName.Rows.Count - 1; i++)
{
sheetName = DtSheetName.Rows[i]["TABLE_NAME"].ToString();
if (sheetName.Length >= 4)
{
conn.Close();
conn.Open();
using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheetName + "]", conn))
{
using (OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = cmd;
try
{
da.Fill(dt);
dt.TableName = sheetName.Replace("$", "");
}
catch (Exception ex)
{
ErrSheetName = ErrSheetName + "," + sheetName.Replace("$", "");
}
}
}
}
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ReadExcel()");
return false;
}
finally
{
if (!string.IsNullOrEmpty(ErrSheetName))
{
MessageBox.Show("Error in Data Of these files [" + ErrSheetName + "] Some Data may be lost !", "ReadExcel()");
}
conn.Close();
GC.Collect();
}
}

Returning a OleDbDataReader from a function

I am trying to return OleDbDataReader from a function. I search on internet and I found some help and I create a code
public IEnumerable<IDataRecord> ImportXLS(string path)
{
string connString = "";
string strFileType = ".xlsx";
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT * FROM [Sheet1$]";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
using (IDataReader xlsReader = cmd.ExecuteReader())
{
while (xlsReader.Read())
{
yield return (IDataReader)xlsReader;
}
}
conn.Close();
}
This is OK It Return xlsReader, and I try to catch this xlsReader by
public string UploadFile(string tPath, string FileName)
{
string msg = "";
FileImportModule.FileImport OFileImport = new FileImportModule.FileImport();
SqlDataReader reader = (SqlDataReader)OFileImport.ImportXLS(tPath);
var context = new MountSinaiEntities1();
string tableName = context.Tables.Find(1).tableName;
var tableFieldList = from a in context.TablesFields
where a.tableId == 1
select a.fieldName;
//1- SQL Bulk Copy
try
{
SqlConnection con = new SqlConnection(#"Data Source=abhishek-pc;Initial Catalog=MountSinai;User ID=sa;Password=abhi");
con.Open();
SqlBulkCopy bulkcopy = new SqlBulkCopy(con);
{
bulkcopy.DestinationTableName = tableName;
bulkcopy.WriteToServer(reader);
}
con.Close();
}
catch (Exception e)
{
//Handle Exception
}
}
but the error occur
Unable to cast object of type 'd__0' to type
'System.Data.SqlClient.SqlDataReader'
What is the Solution for that problem? Any alternative to use it as reader object...
You are returning a type of IEnumerable<IDataRecord>, so you cannot cast it to an SqlDataReader.
Just return xlsReader (making sure to dispose it properly) if that is what you want. Otherwise, just use the data you are returning.

How to get sheetname of the uploaded excel file using C#?

I would like to get the sheet name of the uploaded excel file using C# code. The file may be in .xls or .xlsx format. The Code I have used is as follows:
protected void btnGenerateCSV_Click(object sender, EventArgs e)
{
string sourceFile = ExcelFileUpload.PostedFile.FileName;
string worksheetName = ?? //(How to get the first sheetname of the uploaded file)
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sourceFile + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
if (sourceFile.Contains(".xlsx"))
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sourceFile + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\"";
try
{
conn = new OleDbConnection(strConn);
conn.Open();
cmd = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]", conn);
cmd.CommandType = CommandType.Text;
wrtr = new StreamWriter(targetFile);
da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
for (int x = 0; x < dt.Rows.Count; x++)
{
string rowString = "";
for (int y = 0; y < dt.Columns.Count; y++)
{
rowString += "\"" + dt.Rows[x][y].ToString() + "\",";
}
wrtr.WriteLine(rowString);
}
}
catch (Exception exp)
{
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
cmd.Dispose();
da.Dispose();
wrtr.Close();
wrtr.Dispose();
}
}
(How to get the first sheetname of the uploaded file)
string worksheetName = ??
I use this to get sheet names from a .xlsx file and loop through all the names to read sheets one by one.
OleDbConnection connection = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0 xml;HDR=YES;'");
connection.Open();
DataTable Sheets = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach(DataRow dr in Sheets.Rows)
{
string sht = dr[2].ToString().Replace("'", "");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter("select * from [" + sht + "]", connection);
}
DataTable Sheets = oleConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
for(int i=0;i<Sheets.Rows.Count;i++)
{
string worksheets= Sheets.Rows[i]["TABLE_NAME"].ToString();
string sqlQuery = String.Format("SELECT * FROM [{0}]", worksheets);
}
If the Excel is too big, This code will waste a lot of time in(conn.open()). Use Openxml will be better(use less time),but if the Excel is Open---Using openxml to read will have the exception but oldbhelper wile have no exception. My english is pool , sorry.-----Chinese boy
I use Microsoft excel library Microsoft.Office.Interop.Excel. Then you can use index to get the worksheet name as following.
string path = #"C\Desktop\MyExcel.xlsx" //Path for excel
using Excel = Microsoft.Office.Interop.Excel;
xlAPP = new Excel.Application();
xlAPP.Visible = false;
xlWbk = xlAPP.Workbooks.Open(path);
string worksheetName = xlWbk.Worksheets.get_Item(1).Name //pass Index here. Reemember that index starts from 1.
xlAPP.Quit();
releaseObject(xlWbk);
releaseObject(xlAPP);
//Always handle unmanaged code.
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}

Categories

Resources