Reading of Excel File c# - c#

Hi guys! I encountered a problem is that I have difficulty reading this Excel file into data grid view as shown above. Any help will be greatly appreciated. Thanks.
These were my codes to read Excel file but there's a error that I have difficulty troubleshooting it.
private void btnSearch_Click(object sender, EventArgs e)
{
Excel.Workbook workbook;
Excel.Worksheet NwSheet;
Excel.Range ShtRange;
Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
OpenFileDialog filedlgExcel = new OpenFileDialog();
filedlgExcel.Title = "Select file";
filedlgExcel.InitialDirectory = #"c:\";
filedlgExcel.FileName = txtFileName.Text;
filedlgExcel.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
filedlgExcel.FilterIndex = 1;
filedlgExcel.RestoreDirectory = true;
if (filedlgExcel.ShowDialog() == DialogResult.OK)
{
workbook = ExcelObj.Workbooks.Open(filedlgExcel.FileName);
NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
ShtRange = NwSheet.UsedRange; //gives the used cells in sheet
//Reading Excel file.
//Creating dataTable to read the containt of the Sheet in File.
//Set header name
for (int Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
{
dt.Columns.Add(new DataColumn((ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
}
dt.AcceptChanges();
//store coumn names to array
string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray();
//populate fields
for (int Rnum = 2; Rnum <= ShtRange.Rows.Count; Rnum++)
{
DataRow dr = dt.NewRow();
for (int Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
{
dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
}
dt.Rows.Add(dr);
dt.AcceptChanges();
}
workbook.Close(true, Missing.Value, Missing.Value);
ExcelObj.Quit();
foreach (DataRow dr in dt.Rows)
{
string strEmployee = dr["Employee Name"].ToString();
obj1 = new List<employeeschedule>();
for (int i = 1; i < dt.Columns.Count; i++)
{
string period = dr[i].ToString();
string[] split = period.Split('–');
employeeschedule es = new employeeschedule();
string day = columnNames[i];
if (split[0] == "REST")
{
es.day = day;
es.startTime = "0000";
es.endTime = "0000";
es.restDay = "Yes";
}
if (split[0] == "OFF")
{
es.day = day;
es.startTime = "0000";
es.endTime = "0000";
es.restDay = "Yes";
}
else
{
es.day = day;
es.startTime = split[0];
es.endTime = split[1];
es.restDay = "No";
}
obj1.Add(es);
}
dict.Add(strEmployee, obj1);
dgvEmployeeShift.DataSource = dt;
}
}
}
The error falls on this part:
dt.Columns.Add(new DataColumn((ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
It states that "Cannot perform runtime binding on a null reference".

I love using Interop when automating Excel but for your requirement why not use OleDb? It is much faster than using Interop?
TRIED AND TESTED
private void btnSearch_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection MyCon ;
System.Data.DataSet DtSet ;
System.Data.OleDb.OleDbDataAdapter MyCommand ;
//~~> Replace this with relevant file path
MyCon = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Sample.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"");
//~~> Replace this with the relevant sheet name
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyCon);
MyCommand.TableMappings.Add("Table", "MyTable");
DtSet = new System.Data.DataSet();
//~~> Fill Dataset
MyCommand.Fill(DtSet);
//~~> Set Source
dataGridView1.DataSource = DtSet.Tables[0];
MyCon.Close();
}

This worked for me ...........
Browses your local folder , picks up the excel ,displays in grid view.
Using oledb is what i suggest
private void OpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
if (openfiledialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
this.textBox_path.Text = openfiledialog1.FileName;
}
}
private void LoadExcel_Click(object sender, EventArgs e)
{
//string pathConn = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+textBox_path.Text+";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
string pathConn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox_path.Text + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";
OleDbConnection conn = new OleDbConnection(pathConn);
conn.Open();
OleDbDataAdapter mydataadapter = new OleDbDataAdapter("Select * from ["+textBox_sheet.Text+"$]", conn);
DataTable dt = new DataTable();
mydataadapter.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
}

Related

Prevent import of duplicate records from Excel sheet in C#

I need to prevent importing duplicating values from excel sheet. here is my code. this works fine. but I need to improve this code to prevent duplication.
need to fix this quickly.
private void btnGet_Click(object sender, EventArgs e)
{
try
{
intMode = 1;
dsMain = new DataSet();
if (dgGrid.Columns.Count != 0)
{
for (int i = dgGrid.Columns.Count; i > 0; i--)
{
dgGrid.Columns.Remove(dgGrid.Columns[0].Name);
}
}
dgGrid.Columns.Clear();
odfExcelGet.Title = "Excel Upload";
odfExcelGet.FileName = "Excel";
odfExcelGet.Filter = "Excel File (*.xls;*.xlsx;)|*.xls;*.xlsx;";
odfExcelGet.InitialDirectory = #"c:\";
odfExcelGet.ShowDialog();
txtAddress.Text = odfExcelGet.FileName;
this.Cursor = Cursors.WaitCursor;
string ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
ConnectionString += odfExcelGet.FileName;
ConnectionString += #";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";";
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", ConnectionString);
da.Fill(dsMain);
dgGrid.DataSource = dsMain.Tables[0];
//rest of code
}

How to Read an Excel file to DataGridView in C#

I'm having an issue reading an excel file onto a datagridview. After running the application, it keeps telling me
"The process cannot access the file
'C:\Users\emmanuel.adefuye\Documents\ExcelTestFile.xlsx' because it is
being used by another process"
private void OpenExcelFile_Click(object sender, EventArgs e)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(#"C:\\Users\\emmanuel.adefuye\\Documents\\ExcelTestFile.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
{
//String name = "First Name";
OpenFileDialog selectedFile = new OpenFileDialog();
selectedFile.ShowDialog();
selectedFile.OpenFile();
String constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
"C:\\Users\\emmanuel.adefuye\\Documents\\ExcelTestFile.xlsx" +
";Extended Properties='Excel 8.0;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + selectedFile.SafeFileName + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
}
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
//iterate over the rows and columns and print to the console as it appears in the file
//excel is not zero based!!
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
//new line
if (j == 1)
Console.Write("\r\n");
//write the value to the console
if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");
}
}
//cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}`
There are a few different ways to read the data and manipulate it, but for your core issue of
"The process cannot access the file
'C:\Users\emmanuel.adefuye\Documents\ExcelTestFile.xlsx' because it is
being used by another process"
Your issue is that you're opening the file here
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(#"C:\\Users\\emmanuel.adefuye\\Documents\\ExcelTestFile.xlsx");
And then again attempting to access it here
OpenFileDialog selectedFile = new OpenFileDialog();
selectedFile.ShowDialog();
selectedFile.OpenFile();
String constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
"C:\\Users\\emmanuel.adefuye\\Documents\\ExcelTestFile.xlsx" +
";Extended Properties='Excel 8.0;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
You could just use this great nuget package:
https://www.nuget.org/packages/NPOI/
and read excel like that:
public static XSSFWorkbook ReadExcelFile(string strFilePath)
{
XSSFWorkbook hssfwb;
using (FileStream file = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
{
hssfwb = new XSSFWorkbook(file);
}
return hssfwb;
}
private void OpenExcelFile_Click(object sender, EventArgs e)
{
var dt = ReadExcelFile(#"C:\\Users\\emmanuel.adefuye\\Documents\\ExcelTestFile.xlsx");
}

Faster way of storing Excel worksheet to system.data.datatable using C#

I am trying to find a faster way to read an XML file that can be opened in Excel 2010. I cannot immediately read the XML file using readxml method because it contains Workbook, style, cell, data and other tags. So my approach was to open it in Excel then get the data on sheet 2 only. The sample file contains 9,000+ rows and takes about 2mins 49secs to store in a datatable. The actual file has 25,000+ rows. This is what I have tried:
private void bulkInsert()
{
var s = new Stopwatch();
s.Start();
try
{
KillExcel();
GCollector();
Excel.Application app = null;
app = new Excel.Application();
Excel.Worksheet sheet = null;
Excel.Workbook book = null;
book = app.Workbooks.Open(#"my directory for the file");
sheet = (Worksheet)book.Sheets[2];
sheet.Select(Type.Missing);
var xlRange = (Excel.Range)sheet.Cells[sheet.Rows.Count, 1];
int lastRow = (int)xlRange.get_End(Excel.XlDirection.xlUp).Row;
int newRow = lastRow + 1;
var cellrow = newRow;
int columns = sheet.UsedRange.Columns.Count;
Excel.Range test = sheet.UsedRange;
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Node_SegmentName");
dt.Columns.Add("Type");
dt.Columns.Add("Sub-Type");
dt.Columns.Add("Description");
dt.Columns.Add("Parameter_DataIdentifier");
dt.Columns.Add("RuntimeValue");
dt.Columns.Add("Category");
dt.Columns.Add("Result");
dt.TableName = "SsmXmlTable";
//slow part
for (i = 0; i < lastRow; i++)
{
DataRow excelRow = dt.NewRow();
for (int j = 0; j < columns; j++)
{
excelRow[j] = test.Cells[i + 2, j + 1].Value2;
}
dt.Rows.Add(excelRow);
}
dataGridView1.DataSource = dt;
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.WriteXml(AppDomain.CurrentDomain.BaseDirectory + String.Format("\\XMLParserOutput{0}.xml", DateTime.Now.ToString("MM-d-yyyy")));
DataSet reportData = new DataSet();
reportData.ReadXml(AppDomain.CurrentDomain.BaseDirectory + String.Format("\\XMLParserOutput{0}.xml", DateTime.Now.ToString("MM-d-yyyy")));
SqlConnection connection = new SqlConnection("Data Source=YOURCOMPUTERNAME\\SQLEXPRESS;Initial Catalog=YOURDATABASE;Integrated Security=True;Connect Timeout=0");
connection.Open();
SqlBulkCopy sbc = new SqlBulkCopy(connection);
sbc.DestinationTableName = "Test";
sbc.WriteToServer(reportData.Tables["SsmXmlTable"]);
connection.Close();
s.Stop();
var duration = s.Elapsed;
MessageBox.Show(duration.ToString() + " bulk insert way");
MessageBox.Show(ds.Tables["SsmXmlTable"].Rows.Count.ToString());//439 rows
}
catch (Exception ex)
{
KillExcel();
GCollector();
MessageBox.Show(ex.ToString() + i.ToString());
}
}
Without the reading from Excel part, the insertion of data using bulk copy only takes a couple of seconds (0.5secs for 449 rows).
For others who are encountering the same issue, what I did was:
save the xml as an xlsx file
use oledb to read the xlsx file
store in dataset using OleDbAdapter (Fill() method)
bulk insert
Here is the code that I used to do this (change the connection string):
Stopwatch s = new Stopwatch();
s.Start();
string sSheetName = null;
string sConnection = null;
System.Data.DataTable sheetData = new System.Data.DataTable();
System.Data.DataTable dtTablesList = default(System.Data.DataTable);
OleDbConnection oleExcelConnection = default(OleDbConnection);
sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + #"C:\Users\YOURUSERNAME\Documents\Visual Studio 2012\Projects\TestXmlParser\TestXmlParser\bin\Debug\ConsolidatedSSMFiles.xlsx" + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
oleExcelConnection = new OleDbConnection(sConnection);
oleExcelConnection.Open();
dtTablesList = oleExcelConnection.GetSchema("Tables");
if (dtTablesList.Rows.Count > 0)
{
sSheetName = dtTablesList.Rows[0]["TABLE_NAME"].ToString();
}
dtTablesList.Clear();
dtTablesList.Dispose();
if (!string.IsNullOrEmpty(sSheetName))
{
OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [TEST$]", oleExcelConnection);
sheetAdapter.Fill(sheetData);
} s.Stop();
var duration = s.Elapsed;
oleExcelConnection.Close();
dataGridView1.DataSource = sheetData;
MessageBox.Show(sheetData.Rows.Count.ToString()+"rows - "+ duration.ToString());
This reads 25000+ rows of excel data to a datable in approx. 1.9 to 2.0 seconds.

How can i read from excel file

Please can you help me with my actually script. I have done select from excel file but now i dont know how can i added value to my lists. I get data from more files together not only from one.
Here is my actually scripts:
Base button initiate event for read.
private void next_Click(object sender, EventArgs e)
{
for (int i = 0; i < UniqueValue.traceToFile.Count; i++)
{
ReadFromExcel read = new ReadFromExcel();
read.ReadData(UniqueValue.traceToFile[i]);
}
}
After next step is this:
class ReadFromExcel : Config
{
public void ReadData(string fullpath)
{
DataSet da = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter();
string cell = "C7";
string name = "List1";
string FileName = fullpath;
string _ConnectionString = string.Empty;
string _Extension = Path.GetExtension(FileName);
// Checking for the extentions, if XLS connect using Jet OleDB
if (_Extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))
{
_ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0", FileName);
}
// Use ACE OleDb
else if (_Extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))
{
_ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", FileName);
}
OleDbConnection con = new OleDbConnection(_ConnectionString);
string strCmd = "SELECT * FROM [" + name + "$" + cell + "]";
OleDbCommand cmd = new OleDbCommand(strCmd, con);
try
{
con.Open();
da.Clear();
adapter.SelectCommand = cmd;
adapter.Fill(da);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
con.Close();
}
}
}
Now I need that this result to be written to my list. i reading only one cells from every files.
List for result is
public static List<int> money = new List<int>();
I really thanks for all answers which can help me.
I think below code will help you.
In this example dt, dt2 and dt3 will consider return from your function ReadData function which have datatable from excel.
public static List<int> money = new List<int>();
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name");
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["ID"] = 1;
dt.Rows[dt.Rows.Count - 1]["Name"] = "Test1";
DataTable dt2 = new DataTable();
dt2.Columns.Add("ID", typeof(Int32));
dt2.Columns.Add("Name");
dt2.Rows.Add();
dt2.Rows[dt2.Rows.Count - 1]["ID"] = 2;
dt2.Rows[dt2.Rows.Count - 1]["Name"] = "Test2";
DataTable dt3 = new DataTable();
dt3.Columns.Add("ID", typeof(Int32));
dt3.Columns.Add("Name");
dt3.Rows.Add();
dt3.Rows[dt3.Rows.Count - 1]["ID"] = 3;
dt3.Rows[dt3.Rows.Count - 1]["Name"] = "Test2";
money = (from row in dt.AsEnumerable() select Convert.ToInt32(row["ID"])).ToList();
money.AddRange((from row in dt2.AsEnumerable() select Convert.ToInt32(row["ID"])).ToList());
money.AddRange((from row in dt3.AsEnumerable() select Convert.ToInt32(row["ID"])).ToList());
}
Excel.Application excel;
Excel.Workbook wb;
Excel.Worksheet sh;
DataTable dt=new DataTable();
excel = new Excel.Application();
excel.Visible = true;
wb = excel.Workbooks.Open("File Path");
sh = wb.Sheets.Add();
sh.Name = "Data";
count = 2;
sh.Cells[count, "A"].Value2 = "Column Name 1";
sh.Cells[count, "B"].Value2 = "Column Name 2";
sh.Cells[count, "C"].Value2 = "Column Name 3";
sh.Cells[count, "D"].Value2 = "Column Name 4";
sh.Cells[count, "E"].Value2 = "Column Name 5";
sh.Range["A" + count + "", "E" + count + ""].Font.Size = 12;
sh.Range["A" + count + "", "E" + count + ""].Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
sh.Range["I2"].WrapText = true;
try
{
con.Open();
da.Clear();
adapter.SelectCommand = cmd;
adapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
sh.Cells[count, "A"].Value2 = row["Column Name"].ToString();
sh.Cells[count, "B"].Value2 = row["Column Name"].ToString();
sh.Cells[count, "C"].Value2 = row["Column Name"].ToString();
sh.Cells[count, "D"].Value2 = row["Column Name"].ToString();
sh.Cells[count, "E"].Value2 = row["Column Name"].ToString();
}
wb.SaveAs("File Path" + "File Nmae" + ".xls");
excel.Workbooks.Close();
excel.Quit();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
con.Close();
}

Export data to excel file 2003, 2007 and Above

I'm using these connection strings, depending on the extension of the file:
For 2003 : Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;
For 2007 : Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;
Here is to get the connection.
string con_excel = "";
switch (Extension.ToLower())
{
case ".xls":
con_excel = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx":
con_excel = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
con_excel = con_excel.Replace("filename", filePath);
The following is the code to generate excel file.
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
oXL = new Excel.Application();
oXL.Visible = false;
oXL.SheetsInNewWorkbook = 1;
oWB = (Excel._Workbook)(oXL.Workbooks.Add());
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
try
{
string[] colNames = new string[dataTable.Columns.Count];
int col = 0;
foreach (DataColumn dc in dataTable.Columns)
colNames[col++] = dc.ColumnName;
char lastColumn = (char)(65 + dataTable.Columns.Count - 1);
oSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
oSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
oSheet.get_Range("A1", lastColumn + "1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
DataRow[] dr = dataTable.Select();
string[,] rowData = new string[dr.Count<DataRow>(), dataTable.Columns.Count + 1];
int rowCnt = 0;
foreach (DataRow row in dr)
{
for (col = 0; col < dataTable.Columns.Count; col++)
{
rowData[rowCnt, col] = row[col].ToString();
}
rowCnt++;
}
rowCnt++;
oSheet.get_Range("A2", lastColumn + rowCnt.ToString()).Value = rowData;
oXL.Visible = false;
oXL.UserControl = true;
String sNewFolderName = "Report_" + intReportId;
filename = Server.MapPath("Your Report\\" + sNewFolderName + DateTime.Now.ToString("dd-MM-yyyy_hh-mm-ss") + Extension);
oSheet.SaveAs(filename);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
oXL.Quit();
Marshal.ReleaseComObject(oSheet);
Marshal.ReleaseComObject(oWB);
Marshal.ReleaseComObject(oXL);
oSheet = null;
oWB = null;
oXL = null;
GC.GetTotalMemory(false);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.GetTotalMemory(true);
//The excel is created and opened for insert value. We most close this excel using this system
Process[] localByName = Process.GetProcessesByName("EXCEL");
foreach (Process process in localByName)
{
process.Kill();
}
2007 file format is ok.
I tried to upload the 2003(.xls) excel file and then also generate 2003(.xls) format. But when i open that file, i got the following error.
The file you are trying to open "FileName.xls" in a different format than specified file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
Is this because of connection string ?
Try this Code upload Excel 2003 and 2007 file
if (filenam.ToString() == ".xls")
{ constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathnam + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; }
else if (filenam.ToString() == ".xlsx")
{ constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathnam + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; }
else { Response.Write("<script>alert('Load Excel file Only')</script>"); }
string Qry = "SELECT [Customer], [Project], [Contact], [Designation], [Phone], [EmailID],[Region] FROM [Sheet1$]";
OleDbConnection conn = new OleDbConnection(constr);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
OleDbCommand cmd = new OleDbCommand(Qry, conn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
{
gv_upload.DataSource = dt;
gv_upload.DataBind();
}
da.Dispose(); conn.Close(); conn.Dispose();

Categories

Resources