我正在努力将数据从Excel工作表导入数据库。 Excel工作表包含几个空行,我想删除那些空行,然后将清除的数据插入数据库 我通过引用其他代码编写了代码,这是插入值的代码:
OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtExcelFile.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'");
//DataTable dt = new DataTable();
try
{
cnn.Open();
OleDbDataAdapter data = new OleDbDataAdapter("select * from [Customers$]", cnn);
data.Fill(dsExcel);
dgvCustomers.ColumnHeadersVisible = false;
SqlConnection connection = new SqlConnection("Data Source=COMPUTER-8EB749;Initial Catalog=KITS;Integrated Security=true");
connection.Open();
for (int i = 0; i < dsExcel.Tables[0].Rows.Count; i++)
{
string ID = ds.Tables[0].Rows[i][0].ToString();
Int16 CustID = Convert.ToInt16(ID);
string CustName = dsExcel.Tables[0].Rows[i][1].ToString();
string CardScheme = dsExcel.Tables[0].Rows[i][2].ToString();
string Outlet = dsExcel.Tables[0].Rows[i][3].ToString();
string TerminalNum = dsExcel.Tables[0].Rows[i][4].ToString();
Int32 Terminal = Convert.ToInt32(TerminalNum);
string Date1 = dsExcel.Tables[0].Rows[i][5].ToString();
DateTime Date = Convert.ToDateTime(Date1);
string Time = dsExcel.Tables[0].Rows[i][6].ToString();
DateTime DateTime = Convert.ToDateTime(Time);
string Amount1 = ds.Tables[0].Rows[i][7].ToString();
double Amount = Convert.ToDouble(Amount1);
SqlCommand com = new SqlCommand("insert into Customer(CustID,CustName,CardScheme,Outlet,TerminalNum,TranDate,TranDateTime,Amount) values ('" + CustID + "','" + CustName + "','" + CardScheme + "','" + Outlet + "','" + Terminal + "','" + Date + "','" + DateTime + "','" + Amount + "')", connection);
com.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("Data Inserted Successfully.");
}
任何人都可以说我如何删除空行以便我只能插入数据?!
答案 0 :(得分:61)
这将删除每个列中没有任何内容或空格的所有行:
dataTable = dataTable.Rows
.Cast<DataRow>()
.Where(row => !row.ItemArray.All(field => field is DBNull ||
string.IsNullOrWhiteSpace(field as string)))
.CopyToDataTable();
答案 1 :(得分:6)
试试这个。
public bool InsertRowsToDataBase()
{
try
{
DataTable excelTable = new DataTable();
string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtExcelFile.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
using (OleDbConnection cnn = new OleDbConnection(connString))
{
string query = "select * from [Customers$]";
using (OleDbDataAdapter data = new OleDbDataAdapter(query, cnn))
{
data.Fill(excelTable);
}
}
dgvCustomers.ColumnHeadersVisible = false;
connString = "Data Source=COMPUTER-8EB749;Initial Catalog=KITS;Integrated Security=true";
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
for (int i = 0; i < excelTable.Rows.Length; i++)
{
//takes from the 3rd row
if (i > 1)
{
DataRow row = excelTable.Rows[i];
object ID = row[0];
if (ID != null && !String.IsNullOrEmpty(ID.ToString().Trim()))
{
Int16 CustID = Convert.ToInt16(ID);
string CustName = row[1].ToString();
string CardScheme = row[2].ToString();
string Outlet = row[3].ToString();
string TerminalNum = row[4].ToString();
Int32 Terminal = Convert.ToInt32(TerminalNum);
string Date1 = row[5].ToString();
DateTime Date = Convert.ToDateTime(Date1);
string Time = row[6].ToString();
DateTime DateTime = Convert.ToDateTime(Time);
string Amount1 = row[7].ToString();
double Amount = Convert.ToDouble(Amount1);
string columnNames = "CustID,CustName,CardScheme,Outlet,TerminalNum,TranDate,TranDateTime,Amount";
string query = String.Format("insert into Customer(0}) values ('{1}', '{2}','{3}','{4}','{5}','{6}','{7}','{8}')",
columnNames, CustID, CustName, CardScheme, Outlet, Terminal, Date, DateTime, Amount);
using (SqlCommand com = new SqlCommand(query, connection))
{
com.ExecuteNonQuery();
}
}
}
//this is your last row. do whatever you want with this
DataRow lastRow = excelTable.Rows[excelTable.Rows.Count - 1];
}
}
return true;
}
catch (Exception exception)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
return false;
}
}
请注意,我只是检查ID是否为空,并且不插入任何此类行作为ID将是您表中的PK。
答案 2 :(得分:2)
try
{
OpenOleDBConnection();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter("select * from [" + SelectedSheet + "]", Connection);
dataAdapter.Fill(DataTable);
if ((DataTable != null) && (DataTable.Rows != null) && (DataTable.Rows.Count > 0))
{
List<System.Data.DataRow> removeRowIndex = new List<System.Data.DataRow>();
int RowCounter = 0;
foreach (System.Data.DataRow dRow in DataTable.Rows)
{
for(int index = 0; index < DataTable.Columns.Count; index++)
{
if (dRow[index] == DBNull.Value)
{
removeRowIndex.Add(dRow);
break;
}
else if (string.IsNullOrEmpty(dRow[index].ToString().Trim()))
{
removeRowIndex.Add(dRow);
break;
}
}
RowCounter++;
}
// Remove all blank of in-valid rows
foreach (System.Data.DataRow rowIndex in removeRowIndex)
{
DataTable.Rows.Remove(rowIndex);
}
}
}
catch(Exception e)
{
WPFMessageBox.Show(e.Message, Globalization.GetValue("Import_ImportOption_FormHeader"), WPFMessageBoxButtons.OK, WPFMessageBoxImage.Error);
}
finally
{
CloseOleDBConnection();
}
如果在任何一行中有空白条目,我也会跳过这些行。
答案 3 :(得分:2)
我已经制作了这个私有方法。 它将DataTable作为参数,并返回相同的DataTable而没有空行。
private DataTable StripEmptyRows(DataTable dt)
{
List<int> rowIndexesToBeDeleted = new List<int>();
int indexCount = 0;
foreach(var row in dt.Rows)
{
var r = (DataRow)row;
int emptyCount = 0;
int itemArrayCount = r.ItemArray.Length;
foreach(var i in r.ItemArray) if(string.IsNullOrWhiteSpace (i.ToString())) emptyCount++;
if(emptyCount == itemArrayCount) rowIndexesToBeDeleted.Add(indexCount);
indexCount++;
}
int count = 0;
foreach(var i in rowIndexesToBeDeleted)
{
dt.Rows.RemoveAt(i-count);
count++;
}
return dt;
}
答案 4 :(得分:2)
这将从datable中删除所有空行:
DataTable dt = dt.Rows
.Cast<DataRow>()
.Where(row => !row.ItemArray.All(f => f is DBNull))
.CopyToDataTable();
OR
DataTable dt = dt.Rows
.Cast<DataRow>()
.Where(row => !row.ItemArray.All(f => f is DBNull ||
string.IsNullOrEmpty(f as string ?? f.ToString())))
.CopyToDataTable();
答案 5 :(得分:1)
为什么不在插入空行之前直接忽略空行?
if(string.IsNullOrEmpty(ID + CustName + CardScheme /*.. and so on */))
{
continue;
}
像这样:
for (int i = 0; i < dsExcel.Tables[0].Rows.Count; i++)
{
string ID = ds.Tables[0].Rows[i][0].ToString();
Int16 CustID = Convert.ToInt16(ID);
string CustName = dsExcel.Tables[0].Rows[i][1].ToString();
string CardScheme = dsExcel.Tables[0].Rows[i][2].ToString();
string Outlet = dsExcel.Tables[0].Rows[i][3].ToString();
string TerminalNum = dsExcel.Tables[0].Rows[i][4].ToString();
Int32 Terminal = Convert.ToInt32(TerminalNum);
string Date1 = dsExcel.Tables[0].Rows[i][5].ToString();
DateTime Date = Convert.ToDateTime(Date1);
string Time = dsExcel.Tables[0].Rows[i][6].ToString();
DateTime DateTime = Convert.ToDateTime(Time);
string Amount1 = ds.Tables[0].Rows[i][7].ToString();
double Amount = Convert.ToDouble(Amount1);
/*** Add this if-statement to you code! ***/
if(string.IsNullOrEmpty(ID + CustName + CardScheme + Outlet + TerminalNum + Date1 + Time + Amount1))
{
continue;
}
SqlCommand com = new SqlCommand("insert into Customer(CustID,CustName,CardScheme,Outlet,TerminalNum,TranDate,TranDateTime,Amount) values ('" + CustID + "','" + CustName + "','" + CardScheme + "','" + Outlet + "','" + Terminal + "','" + Date + "','" + DateTime + "','" + Amount + "')", connection);
com.ExecuteNonQuery();
}
答案 6 :(得分:1)
我修改了Cfrim的答案。您需要检查空字符串和空白字符串。空白区域来自已删除的单元格,空白区域来自已删除的数据。
private DataTable StripEmptyRows(DataTable dt)
{
List<int> rowIndexesToBeDeleted = new List<int>();
int indexCount = 0;
foreach(var row in dt.Rows)
{
var r = (DataRow)row;
int emptyCount = 0;
int itemArrayCount = r.ItemArray.Length;
foreach (var i in dr.ItemArray)
{
if (string.IsNullOrEmpty(i.ToString()) || string.IsNullOrWhiteSpace(i.ToString()))
emptyCount++;
}
if(emptyCount == itemArrayCount) rowIndexesToBeDeleted.Add(indexCount);
indexCount++;
}
int count = 0;
foreach(var i in rowIndexesToBeDeleted)
{
dt.Rows.RemoveAt(i-count);
count++;
}
return dt;
}
答案 7 :(得分:0)
你的数据库本身有空行吗?这很奇怪。可以通过说主键列不为NULL来进行选择查询时对其进行过滤
答案 8 :(得分:0)
public static DataTable RemoveEmptyRows(DataTable dt)
{
List removeRowIndex = new List();
foreach (DataRow dRow in dt.Rows)
{
for (int index = 0; index < dt.Columns.Count; index++)
{
if (string.IsNullOrEmpty(dRow[index].ToString().Trim()))
{
removeRowIndex.Add(dRow);
break;
}
else if (dRow[index] == DBNull.Value)
{
removeRowIndex.Add(dRow);
break;
}
}
}
foreach (DataRow rowIndex in removeRowIndex)
{
dt.Rows.Remove(rowIndex);
}
return dt;
}
答案 9 :(得分:0)
检查空行
Foreach(DataRow as row in datable.Rows) {
var isEmpty = row.ItemArray.All(c => c is DBNull);
if(!isEmpty) {
//Your Logic
}
}
答案 10 :(得分:0)
这对我来说很完美:
dt.Load(cmd.ExecuteReader());
var x = dt.Rows.Cast<DataRow>()
.Where(row => !Array.TrueForAll(row.ItemArray, value =>
{ return value.ToString().Length == 0; }
));
dt = x.CopyToDataTable();
答案 11 :(得分:0)
我在@Levitikon帖子https://stackoverflow.com/a/9233696/5848472中做了一些更改 带有@ shA.t comment,此代码删除数据表中的所有空行和列:
dt = ds.Tables[tablename].Rows
.Cast<DataRow>()
.Where(row => !row.ItemArray.All(field => field is DBNull ||
string.IsNullOrWhiteSpace(field as string ?? field.ToString())))
.CopyToDataTable();
foreach (var column in dt.Columns.Cast<DataColumn>().ToArray())
{
if (dt.AsEnumerable().All(dr => dr.IsNull(column)))
dt.Columns.Remove(column);
}
答案 12 :(得分:0)
这对我有用。如果我们不检查行而直接执行CopyToDataTable()
,那么当数据表有空行时,您可能会得到异常。
var rows = tbl.Rows.Cast<DataRow>()
.Where(row => !row.ItemArray.All(field => field is DBNull || String.IsNullOrWhiteSpace(field as string ?? field.ToString())));
if (rows.Any())
tbl = rows.CopyToDataTable();