Asp.net 导入的Excel工作表未正常排序
下面的代码将数据从excel工作表导入ASP.Net网页,并将其保存在我的数据库中的SQL server中 在ASP网页或数据库中,当我从所需的工作表中导入数据时,它不是按照默认的排序导入的,这会严重影响最终的结果并丢失数据。 有关更多说明,如果excel工作表中第一个ID为“1”,活动为“新安装”,等等……在ASP网页和SQL数据库中,可能出现在第九行,并且没有完整数据Asp.net 导入的Excel工作表未正常排序,asp.net,sql-server,excel,Asp.net,Sql Server,Excel,下面的代码将数据从excel工作表导入ASP.Net网页,并将其保存在我的数据库中的SQL server中 在ASP网页或数据库中,当我从所需的工作表中导入数据时,它不是按照默认的排序导入的,这会严重影响最终的结果并丢失数据。 有关更多说明,如果excel工作表中第一个ID为“1”,活动为“新安装”,等等……在ASP网页和SQL数据库中,可能出现在第九行,并且没有完整数据 using System; using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.OleDb;
namespace ImpExpExc
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateData();
lblMessage.Text = "Current Database Data!";
}
}
private void PopulateData()
{
using (TEDataEntities dc = new TEDataEntities())
{
gvData.DataSource = dc.ImportExportExcels.ToList();
gvData.DataBind();
}
}
protected void btnImport_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.ContentType == "application/vnd.ms-excel" ||
FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
try
{
string fileName = Path.Combine(Server.MapPath("~/ImportDocument"), Guid.NewGuid().ToString() + Path.GetExtension(FileUpload1.PostedFile.FileName));
FileUpload1.PostedFile.SaveAs(fileName);
string conString = "";
string ext = Path.GetExtension(FileUpload1.PostedFile.FileName);
if (ext.ToLower() == ".xls")
{
conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (ext.ToLower() == ".xlsx")
{
conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "Select [ID], [Activities], [Governorate], [Exchange], [Client], [Technician], [CircuitNo], [Comment], [Date] from [Sheet1$]";
OleDbConnection con = new OleDbConnection(conString);
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
OleDbCommand cmd = new OleDbCommand(query, con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
da.Dispose();
con.Close();
con.Dispose();
//Import to Database
using (TEDataEntities dc = new TEDataEntities())
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
string empID = dr["ID"].ToString();
var v = dc.ImportExportExcels.Where(a => a.ID.Equals(empID)).FirstOrDefault();
if (v != null)
{
//Update here
v.Activities = dr["Activities"].ToString();
v.Governorate = dr["Governorate"].ToString();
v.Exchange = dr["Exchange"].ToString();
v.Client = dr["Client"].ToString();
v.Technician = dr["Technician"].ToString();
v.CircuitNo = dr["CircuitNo"].ToString();
v.Comment = dr["Comment"].ToString();
v.Date = dr["Date"].ToString();
}
else
{
//Insert here
dc.ImportExportExcels.AddObject(new ImportExportExcel
{
ID = dr["ID"].ToString(),
Activities = dr["Activities"].ToString(),
Exchange = dr["Exchange"].ToString(),
Client = dr["Client"].ToString(),
Technician = dr["Technician"].ToString(),
CircuitNo = dr["CircuitNo"].ToString(),
Comment = dr["Comment"].ToString(),
Date = dr["Date"].ToString()
});
}
}
dc.SaveChanges();
}
PopulateData();
lblMessage.Text = "Successfully data import done!";
}
catch (Exception)
{
throw;
}
}
}
}
}
最后,通过在数据库中添加具有增量ID的附加列解决了排序问题,但仍然有一些与日期相关的单元格是空的我认为出现此问题是因为代码中的ID是字符串,而数据库中的nvarchar(50)是整数,但我不知道如何在代码中使其成为整数