将Excel文件上传到数据库时遇到问题我有一个字母,其中我有一个字母数字值或只有数字值,当我的列中有数字值时读取第一行然后它只存储数值在表单中,它不会选择字母数字值,但如果它在第一行中获得字母数字值,则它会将所有值存储在整个表格中
这是我的Excel表格:
如图所示,在TRAN DETAIL列中,它可以轻松地将值上传到数据库,但是当存在像图像中标记的字母数字值时,它会保存为空。
这是我的代码如何将excel文件上传到数据库,
[HttpPost]
public JsonResult UploadExcel(SBP_NEW users, HttpPostedFileBase FileUpload)
{
List<string> data = new List<string>();
if (FileUpload != null)
{
// tdata.ExecuteCommand("truncate table OtherCompanyAssets");
if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
string filename = FileUpload.FileName;
string targetpath = Server.MapPath("~/Doc/");
FileUpload.SaveAs(targetpath + filename);
string pathToExcelFile = targetpath + filename;
var connectionString = "";
if (filename.EndsWith(".xls"))
{
connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", pathToExcelFile);
}
else if (filename.EndsWith(".xlsx"))
{
connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", pathToExcelFile);
}
var adapter = new OleDbDataAdapter("SELECT * FROM [SBP OST Entries$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "ExcelTable");
DataTable dtable = ds.Tables["ExcelTable"];
string sheetName = "SBP OST Entries";
var excelFile = new ExcelQueryFactory(pathToExcelFile);
var artistAlbums = from a in excelFile.Worksheet<SBP_NEW>(sheetName) select a;
foreach (var a in artistAlbums)
{
try
{
if (a.SEQN_NUMB != "")
{
SBP_NEW TU = new SBP_NEW();
TU.SEQN_NUMB = a.SEQN_NUMB;
TU.VALU_DATE = a.VALU_DATE;
TU.DESCRIPTION = a.DESCRIPTION;
TU.TRAN_DETAIL = a.TRAN_DETAIL;
TU.TRAN_AMNT = a.TRAN_AMNT;
TU.OTST_AMNT = a.OTST_AMNT;
TU.BRAN_DEPT = a.BRAN_DEPT;
TU.OUR_THEIR = a.OUR_THEIR;
TU.REMARKS = a.REMARKS;
TU.INST_NUMB = a.INST_NUMB;
TU.TRAN_DATE = a.TRAN_DATE;
TU.RECON_MODE = a.RECON_MODE;
TU.USER_ID = a.USER_ID;
TU.SUPR_RQRD = a.SUPR_RQRD;
TU.SUPR_BY = a.SUPR_BY;
TU.SUPR_DATE = a.SUPR_DATE;
db.SBP_NEW.Add(TU);
db.SaveChanges();
}
else
{
//data.Add("<ul>");
//if (a.ID == 0 || a.ID == null) data.Add("<li> ID is required</li>");
//if (a.NAME == "" || a.NAME == null) data.Add("<li> Name is required</li>");
//data.Add("</ul>");
data.ToArray();
return Json(data, JsonRequestBehavior.AllowGet);
}
}
catch (DbEntityValidationException ex)
{
foreach (var entityValidationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in entityValidationErrors.ValidationErrors)
{
Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
}
}
}
}
//deleting excel file from folder
if ((System.IO.File.Exists(pathToExcelFile)))
{
System.IO.File.Delete(pathToExcelFile);
}
return Json("success", JsonRequestBehavior.AllowGet);
}
else
{
//alert message for invalid file format
data.Add("<ul>");
data.Add("<li>Only Excel file format is allowed</li>");
data.Add("</ul>");
data.ToArray();
return Json(data, JsonRequestBehavior.AllowGet);
}
}
else
{
data.Add("<ul>");
if (FileUpload == null) data.Add("<li>Please choose Excel file</li>");
data.Add("</ul>");
data.ToArray();
return Json(data, JsonRequestBehavior.AllowGet);
}
}
答案 0 :(得分:0)
当您输入文本的长号concatinate (')
,然后将其传输到Excel时
例如,键入'1234567890123456789,按ENTER后不会显示引号。