OleDbDataAdapter Fill method importing more rows - c#

I'm importing excel into DataTable. The excel file contains 50x7 cells with data.
The problem is that the Fill() method imports 368(?) rows regardless the fact that the data is in the first 50 of them. Any idea what might be the problem ?
I'm using OleDbDataAdapter for the import.
connectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0;";
string commandString = "select * from [" + worksheetName + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(commandString, connectionString);
DataTable fileTable = new DataTable();
adapter.Fill(fileTable);

Try this to remove empty cells from the DataTable:
adapter.Fill(fileTable);
fileTable = fileTable.AsEnumerable()
.Where(row => !row.ItemArray.All(f => f is System.DBNull || String.IsNullOrWhiteSpace(f.ToString())))
.CopyToDataTable();
Note that it also removes empty rows inside the sheet.

Related

Reading headers into excel sheet , from a list

I'm creating a program to export Excel sheets, and I want to use a list to read in the headers (List<String> lFields) but for some reason it does not work, the DateTime works though.
using (Workbook workbook = new Workbook())
{
Worksheet sht = workbook.Worksheets[0];
try
{
rowcount = 0;
//top headers
sht.Cells[rowcount, 1].Value = DBName;
int cols = lFields.Count() + 3;
sht.Cells[rowcount, cols].Value = Convert.ToString(DateTime.Now);
rowcount++;
//headers
for (int i = 0; i < lFields.Count(); i++)
{
sht.Cells[rowcount, i].Value = lfields[i];
}
//styling
sht.Rows[rowcount].Font.Name = "Calibri";
sht.Rows[rowcount].Font.Size = 10;
sht.Rows[rowcount].Font.Bold = true;
sht.Rows[rowcount].Font.Italic = false;
rowcount++;
sht.FreezeRows(0);
int RowCount = 0;
from Microsfot support :https://support.microsoft.com/en-us/help/306572/how-to-query-and-display-excel-data-by-using-asp-net--ado-net--and-vis
You can query the Excel file as a sql table, and than recover the headers:
// Create connection string variable. Modify the "Data Source"
// parameter as appropriate for your environment.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("../ExcelData.xls") + ";" +
"Extended Properties=Excel 8.0;";
// Create connection object by using the preceding connection string.
OleDbConnection objConn = new OleDbConnection(sConnectionString);
// Open connection with the database.
objConn.Open();
// The code to follow uses a SQL SELECT command to display the data from the worksheet.
// Create new OleDbCommand to return data from worksheet.
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM myRange1", objConn);
// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;
// Create new DataSet to hold information from the worksheet.
DataSet objDataset1 = new DataSet();
// Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "XLData");
// Bind data to DataGrid control.
DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
DataGrid1.DataBind();
// Clean up objects.
objConn.Close();

How to retrieve a range of excel records into datatable in c#

I'm importing an excel file into a datatable using OledbConnection and OledbDataAdapter classes. The first two rows of the excel sheet are merged and are empty and the column headers start from the third row. I have 492 rows filled starting from row 3 i.e., 492 - 2 rows = 489 + (1 row for the column header) = 490 filled rows. The issue is that when I debug the code, I get only 478 filled rows and all the excel contents aren't imported into the datatable.
My Code:
//Upload and save the file
string fileName = Path.GetFileName(excelPath.PostedFile.FileName);
string fileLocation = Server.MapPath("~/App_Data/" + fileName);
excelPath.SaveAs(fileLocation);
string conString = string.Empty;
string extension = Path.GetExtension(excelPath.PostedFile.FileName);
switch (extension)
{
case ".xls": //Excel 97-03
conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07 or higher
conString = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString;
break;
}
conString = string.Format(conString, fileLocation);
using (OleDbConnection excel_con = new OleDbConnection(conString))
{
excel_con.Open();
string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
DataTable dtExcelData = new DataTable();
using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT [ID],[Title],[Name],[Post],[Tests],[State] FROM [" + sheet1 + "]", excel_con))
{
oda.Fill(dtExcelData);
}
excel_con.Close();
}
What might have gone wrong?
I recently had the same issue when creating an application to move data from Excel into an SQL database.
I resolved mine by removing the merged rows/columns at the top of the sheet. I suspect that the reader looks for each row to have the format you're requesting in your query and that's causing the error.

Load Excel sheet issue with C#

I have a problem with reading an Excel document. I have tried for 4 hours now and can't find to fix it. I have the following code for now.
private void button2_Click(object sender, EventArgs e) {
if (!string.IsNullOrEmpty(textBox_sheet.Text)) {
string PathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + textBox_path.Text +
";Extended Properties=\"Excel 8.0; HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(PathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * From [" + textBox_sheet.Text + "$]",
conn);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
foreach (DataRow row in dt.Rows) {
Console.WriteLine(row[0].ToString());
while (!string.IsNullOrEmpty(row[0].ToString())) {
Console.WriteLine(row[1].ToString());
}
}
dataGridView1.DataSource = dt;
}
}
The problem is when i try to read in the excel sheet. It loads fine on the form but is taking the first value of the excel sheet as the column header name. I don't want this to happen. The excel sheet has the following format.
.
Here is an image of the forms application with the problem.
As you can see it is loading the first row of the excel sheet into the first row on the Datatable.
I don't want that to happen, but I can't figure out how to fix that.
Your connection string to the spreadsheet has been set to view the first row as a header, HDR=Yes;. Simply change it to HDR=No;.

Import excel header data in a drop down in .net

Hi I am importing a excel or a .csv file using OpenFileDialog in Visual Studio 2005.
I need to show all the headers in a list, which is supposed to be listed on a ComboBox.
e.g If I import a file which has 10 columns in it, my drop down should show me 10 values as
1, 2, 3..........10
Please let me know how to go about it.
CSV is completely different animal than Excel.
I would use the OpenXml library OR use the OleDb driver to read from the excel file.
Look here: Reading excel file using OLEDB Data Provider
You will need to have the ACE driver installed, you may already have it though.
// first read *.xls file into a DataTable; don't wory it is very quick.
public DataTable ReadExcelFile(string strFilePath)
{
string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + strFilePath + "; Extended Properties=\"Excel 8.0; HDR=No; IMEX=1;\"";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
DataTable sdt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
// Change this part to read 1 row
String str = "SELECT TOP(2) * FROM [" + sdt.Rows[0]["TABLE_NAME"].ToString() + "]";
//String str = "SELECT * FROM [" + sdt.Rows[0]["TABLE_NAME"].ToString() + "]";
OleDbCommand objCmdSelect = new OleDbCommand(str, objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataTable dt = new DataTable();
objAdapter1.Fill(dt);
objConn.Close();
dt.AcceptChanges();
return dt;
}
Now working with DataTable
DataTable dt = ReadExcelFile(#"c:\\x.xlsx");
if (dt != null)
{
System.Windows.Forms.ComboBox cmb = new System.Windows.Forms.ComboBox();
for (int i = 0; i < dt.Columns.Count; i++)
cmb.Items.Insert(i, dt.Columns[i].ColumnName);
}

Delete empty rows in a Dataset

How do I delete the empty rows in my dataset?
I am reading data from an excel spreadsheet that has a few empty rows at the bottom.
Here is my code so far:
ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", VariableFile);
OleDbConnection objConn = new OleDbConnection(ConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Requirements$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1);
objConn.Close();
Why not just modify your query to pull only the non-empty data.
Every solution I have found told me to modify the Excel query like you have it. So that isn't much help. You could just create a DataView from your Table that would look at the non-blank rows. Do you know what the columns are beforehand? Even if you don't you could still loop over the column names and build a filter string for the DataView.
string filter = "";
foreach (DataColumn dc in dt.Columns)
{
filter += dc.ColumnName + " <> '' ";
if (dt.Columns[dt.Columns.Count-1].ColumnName != dc.ColumnName)
{
filter += " AND ";
}
}
DataView view = new DataView(dt);
view.RowFilter = filter;
dt = view.ToTable();
Use Linq to include only rows that have a non-null/blank value in any of its columns.
var filteredData = sourceDataTable.AsEnumerable()
.Where(row => row.ItemArray.Any(col => !String.IsNullOrWhiteSpace(col.ToString())))
.ToArray();

Categories

Resources