这是代码:
lblmsg.Text = "";
try
{
//System.Threading.Thread.Sleep(5000);
int stateid = 0, cityid = 0;
DataTable dtbank = new DataTable();
DataSet ds = new DataSet();
if (fildetails.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(fildetails.FileName);
if (fileExtension == ".xls" || fileExtension == ".xlsx")
{
string fileLocation = Server.MapPath("/NewFolder1/") + fildetails.FileName;
if (System.IO.File.Exists(fileLocation))
{
// System.IO.File.Delete(fileLocation);
}
fildetails.SaveAs(fileLocation);
string excelConnectionString = string.Empty;
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
//connection String for xls file format.
if (fileExtension == ".xls")
{
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
//connection String for xlsx file format.
else if (fileExtension == ".xlsx")
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
//Create Connection to Excel work book and add oledb namespace
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
DataTable dt = new DataTable();
dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return;
}
String[] excelSheets = new String[dt.Rows.Count];
int t = 0;
//excel data saves in temp file here.
foreach (DataRow row in dt.Rows)
{
string x = row["TABLE_NAME"].ToString();
if (x != "Sheet1$_" && x != "Sheet2$_" && x != "Sheet3$_" && x != "Sheet4$_" && x != "Sheet5$_")
{
excelSheets[t] = row["TABLE_NAME"].ToString();
t++;
}
}
OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
int totalsheet = excelSheets.Length;
for (int i = 0; i < totalsheet; i++)
{
string query = string.Format("Select * from [{0}]", excelSheets[i]);
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
{
dataAdapter.Fill(ds);
}
}
}
if (fileExtension.ToString().ToLower().Equals(".xml"))
{
string fileLocation = Server.MapPath("~/Content/") + Request.Files["FileUpload"].FileName;
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
Request.Files["FileUpload"].SaveAs(fileLocation);
XmlTextReader xmlreader = new XmlTextReader(fileLocation);
// DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
}
这里我在ds中获得excel值。我该怎么办? 怎么检查? 我试过这件事:
BL objbankbl=new BL();
for (int j = 0; j < ds.Tables.Count; j++)
{
for (int i = 0; i < ds.Tables[j].Rows.Count; i++)
{
////city_name///
if (!DBNull.Value.Equals(ds.Tables[j].Rows[i][0]))
{
// dtbank = objbankbl.GetReportDate("","","", ds.Tables[j].Rows[i][0].ToString(), "", "", "","","");
dtbank = objbankbl.GetReportDate(ds.Tables[j].Rows[i][0].ToString());
if (dtbank.Rows.Count > 0 && ( ds.Tables[j].Rows[i][0].ToString() == dtbank.Rows[j]["Name"]) )
{
stateid = Convert.ToInt32(dtbank.Rows[0]["ID"]);
}
else
{
string bankname = ds.Tables[j].Rows[i][0].ToString();
if (bankname != " " || bankname != null)
{
//stateid = objbankbl.Insert(1, ds.Tables[j].Rows[i][0].ToString(), "", "", 0, "", 0);
}
}
}
答案 0 :(得分:0)
DataTable dt = new DataTable();
dt.Columns.Add();
dt.Columns.Add();
dt.Columns.Add();
dt.Rows.Add(1, "Test1", "Sample1");
dt.Rows.Add(2, "Test2", "Sample2");
dt.Rows.Add(3, "Test3", "Sample3");
dt.Rows.Add(4, "Test4", "Sample4");
dt.Rows.Add(5, "Test5", "Sample5");
var duplicates = dt.AsEnumerable().GroupBy(r => r[0]).Where(gr => gr.Count() > 1).ToList();
Console.WriteLine("Duplicate found: {0}", duplicates.Any());
dt.Rows.Add(1, "Test6", "Sample6"); // Duplicate on 1
dt.Rows.Add(1, "Test6", "Sample6"); // Duplicate on 1
dt.Rows.Add(3, "Test6", "Sample6"); // Duplicate on 3
dt.Rows.Add(5, "Test6", "Sample6"); // Duplicate on 5
duplicates = dt.AsEnumerable().GroupBy(r => r[0]).Where(gr => gr.Count() > 1).ToList();
if (duplicates.Any())
Console.WriteLine("Duplicate found for Classes: {0}", String.Join(", ", duplicates.Select(dupl => dupl.Key)));
Console.ReadLine();
我希望这个例子可以帮助你。
答案 1 :(得分:0)
使用DataView.ToTable方法可以轻松处理。语法如下。
DataView.ToTable(bool distinct, string[] columnNames)
distinct:如果是,则返回的DataTable包含对第二个参数中指定的所有列具有不同值的行。默认值为false。
columnNames:一个字符串数组,其中包含要包含在返回表中的列名列表。返回表中列的顺序与数组中显示的列相同。
<强>练习1 强>
DataTable temp = dt.DefaultView.ToTable(true, "Region");
<强>练习2 强>
DataTable temp = dt.DefaultView.ToTable(true, "Region", "City");
答案 2 :(得分:0)
有几种方法可以使它工作,我想到的前两种方法是使用HashTables或LinQ表达式。
看看这个:从数据表中删除重复条目的最佳方法,但不是删除副本(查看第二个foreach),而是打印消息。
public void CheckDuplicateRows(DataTable dTable, string colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();
//Add list of all the unique item value to hashtable, which stores combination of key, value pair.
//And add duplicate item value in arraylist.
foreach (DataRow drow in dTable.Rows)
{
if (hTable.Contains(drow[colName]))
duplicateList.Add(drow);
else
hTable.Add(drow[colName], string.Empty);
}
//Checks the list dimension to verify if there is any duplicate
if(duplicateList.Count() > 0)
{
//you can print your message here or eventually get info about the duplicate row
}
}