📄 productimport.aspx.cs
字号:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Data.OleDb;
using Shop.Web.UI;
namespace YXShop.Web.Admin.Product
{
public partial class ProductImport : System.Web.UI.Page
{
public Random rd;
BasePage bp = new BasePage();
protected void Page_Load(object sender, EventArgs e)
{
if (!PowerTree.PowerPass.isPass("001005000", PowerTree.PowerPanel.PowerType.look))
{
bp.PageError("对不起,你没有查看此页面的权限!", "../index.aspx");
}
if (!PowerTree.PowerPass.isPass("001005002", PowerTree.PowerPanel.PowerType.add))
{
//bp = new BasePage();
//bp.PageError("对不起,你没有商品批量导入的权限!", "../index.aspx");
Button1.Enabled = false;
Button1.ToolTip = "对不起,你没有商品批量导入的权限!";
}
if (!Page.IsPostBack)
{
}
}
protected void Button1_Click(object sender, System.EventArgs e)
{
if (File1.PostedFile != null)
{
rd = new Random(1);
string filename = DateTime.Now.Date.ToString("yyyymmdd") + DateTime.Now.ToLongTimeString().Replace(":", "") + rd.Next(9999).ToString() + ".xls";
int j = 0;
File1.Accept = "application/octet-stream,application/vnd.ms-excel";
try
{
if (File1.Accept.Contains(File1.PostedFile.ContentType))//检测当前的文件类型是不是在被允许的列表中
{
File1.PostedFile.SaveAs(@Server.MapPath("file/") + filename);
string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + Server.MapPath("file") + "/" + filename + ";Extended Properties=Excel 8.0;";
OleDbConnection thisconnection = new OleDbConnection(conn);
thisconnection.Open();
string Sql = "select * from [Sheet1$]";
OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
DataSet ds = new DataSet();
mycommand.Fill(ds, "[Sheet1$]");
thisconnection.Close();
int count = ds.Tables["[Sheet1$]"].Rows.Count;
for (int i = 0; i < count; i++)
{
string ClassID, Pro_Name, Pro_Numert, Pro_Unit, Pro_Price, Pro_SPrice, Pro_weigth, Pro_Conunt, Pro_Content, Pro_Repertory, Pro_Discription, Pro_Produce, Pro_SCount;
int UnitID = 0;
ClassID = "0";
Pro_Name = ds.Tables["[Sheet1$]"].Rows[i]["商品名称"].ToString();
Pro_Numert = ds.Tables["[Sheet1$]"].Rows[i]["商品编码"].ToString();
Pro_Unit = ds.Tables["[Sheet1$]"].Rows[i]["单位"].ToString();
Pro_SPrice = ds.Tables["[Sheet1$]"].Rows[i]["商城价"].ToString();
Pro_Price = ds.Tables["[Sheet1$]"].Rows[i]["市场参考价"].ToString();
Pro_weigth = ds.Tables["[Sheet1$]"].Rows[i]["重量(Kg)"].ToString();
Pro_Conunt = ds.Tables["[Sheet1$]"].Rows[i]["库存数量"].ToString();
Pro_Repertory = ds.Tables["[Sheet1$]"].Rows[i]["库存报警下限"].ToString();
Pro_Produce = ds.Tables["[Sheet1$]"].Rows[i]["生产商"].ToString();
Pro_Content = ds.Tables["[Sheet1$]"].Rows[i]["商品简介"].ToString();
Pro_Discription = ds.Tables["[Sheet1$]"].Rows[i]["商品介绍"].ToString();
Pro_SCount = ds.Tables["[Sheet1$]"].Rows[i]["上架数量"].ToString();
YXShop.BLL.Product BLL = new YXShop.BLL.Product();
YXShop.Model.Product model = new YXShop.Model.Product();
YXShop.Model.Pro_Unit datau = new YXShop.Model.Pro_Unit();
YXShop.BLL.Pro_Unit databp = new YXShop.BLL.Pro_Unit();
if (Pro_Unit != string.Empty)
{
List<YXShop.Model.Pro_Unit> datam = databp.GetListByColumn("Unit_Name", Pro_Unit);
if (datam.Count > 0)
{
UnitID = Convert.ToInt32(datam[0].ProU_ID);
}
else
{
datau.ProU_Name = Pro_Unit;
datau.ProU_Descriptoin = string.Empty;
datau.ProU_Order = 0;
databp.Create(datau);
datam = databp.GetListByColumn("Unit_Name", Pro_Unit);
if (datam.Count > 0)
{
UnitID = Convert.ToInt32(datam[0].ProU_ID);
}
}
}
model.ProP_ID = string.Empty;
model.Pro_PValues = string.Empty;
model.Pro_CID = string.Empty;
model.Brand_ID = 0;
model.Pro_Name = Pro_Name;
model.Pro_MOdel = string.Empty;
model.Pro_NO = Pro_Numert;
model.Pro_KeyWord = string.Empty;
model.Pro_Description = Pro_Content;
model.Pro_ShopPrice = Pro_SPrice != string.Empty ? Convert.ToDecimal(Pro_SPrice) : 0;
model.Pro_MarketPrice = Pro_Price != string.Empty ? Convert.ToDecimal(Pro_Price) : 0;
YXShop.BLL.MemberRand databmr = new YXShop.BLL.MemberRand();
List<YXShop.Model.MemberRand> datamm = databmr.GetAll();
string strCount = "-1";
if (datamm.Count > 0)
{
for (int a = 0; a < datamm.Count; a++)
{
strCount += ",-1";
}
}
model.Pro_RatingDiscount = strCount;
model.Pro_VirtualCurrencies = 0;
model.Pro_Integral = 0;
model.Pro_SmallImages = string.Empty;// Pro_SmallImages;商品缩略图片
model.Pro_MarkPic = string.Empty;// Pro_MarkPic;商品标志图片
model.Pro_Content = Pro_Discription;
model.Pro_MetaKeywords = string.Empty;
model.Pro_MetaDescription = string.Empty;
model.Pro_MetaTitle = string.Empty;
model.Pro_Weight = Pro_weigth != string.Empty ? Convert.ToDecimal(Pro_weigth) : 0;
model.Pro_WeightUnit = 1;
model.Pro_Stock = Pro_Conunt != string.Empty ? Convert.ToInt32(Pro_Conunt) : 0;
model.ProU_ID = UnitID;
model.Pro_Alarm = Pro_Repertory != string.Empty ? Convert.ToInt32(Pro_Repertory) : 0;
model.Pro_AuditCount = Pro_SCount != string.Empty ? Convert.ToInt32(Pro_SCount) : 0;
model.Pro_Audit = 2;
model.Pro_State = "0|0|0|0|0|0|";
model.Pro_Designation = "1|0|0|0|0|";
model.Pro_RelatingProID = string.Empty;
model.Pro_RelatingArtID = string.Empty;
model.Pro_CreateTime = System.DateTime.Now;
model.Pro_UpdateTime = System.DateTime.Now;
model.Pro_onClicks = 0;
model.Pro_IsAllowComment = 0;
model.Pro_SaleNum = 0;
model.Pro_CostPrice = 0;
model.Pro_AutoUp = System.DateTime.Now;
model.Pro_AutoDown = System.DateTime.Now;
model.Pro_IsAuto = 0;
model.Pro_SaleType = 1;
model.Pro_IntegralPrice = 0;
BLL.Create(model);
j++;
}
}
else
{
bp.PageError("你选择的文件不是Excel!或者版本过低!","Product_List.aspx");
}
}
catch (Exception ex)
{
bp.PageError("导入失败!", "Product_List.aspx");
}
System.IO.File.Delete(@Server.MapPath("file/") + filename);
bp.PageRight("导入成功,共导入" + j.ToString() + "条商品信息。","Product/Product_List.aspx");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -