62,067
社区成员
发帖
与我相关
我的任务
分享
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 COM.QSBK.BLL;
using COM.QSBK.Entity;
using System.Collections.Generic;
using System.Data.OleDb;
public partial class corp_response_leechdom_upload_do : System.Web.UI.Page
{
//protected string leeName
//{
// get
// {
// if (ViewState["leeName"]==null)
// {
// ViewState["leeName"]=Request[isleeName(dr[i]["药品名称"].ToString())];
// }
// return ViewState["leeName"].ToString();
// }
//}
//取文件的路径
private HttpPostedFile txtFile
{
get
{
return Request.Files["xlsfile"];
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UpLoad();
}
}
public List<LeechdomDetails> ldlist { get; set; }
private void UpLoad()
{
try
{
//判断上传文件是否为空
if (txtFile == null)//HasFile用来检查FileUpload是否有指定文件
{
Response.Write("<script>alert('请您选择Excel文件');window.location='../../../index.aspx';</script> ");
// Response.Write("<script>window.location.reload();</script> ");
return;//当无文件时,返回
}
//判断文件是否为excel
string IsXls = System.IO.Path.GetExtension(txtFile.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
if (IsXls != ".xls")
{
Response.Write("<script>alert('只可以选择Excel文件');window.location='../../../index.aspx';</script>");
return;//当选择的不是Excel文件时,返回
}
////读取名称
//读取上传文件的路径与名称
string savePath = txtFile.FileName;//Server.MapPath 获得虚拟服务器相对路径
//保存上传文件
DataSet ds = ExecleDs(savePath, txtFile.FileName); //调用自定义方法
//查看数量
int rowsnum = ds.Tables[0].Rows.Count;
DataRow[] dr = ds.Tables[0].Select();
//定义一个DataRow数组
int _i = dr.Length;
if (rowsnum == 0)
{
Response.Write("<script>alert('Excel表为空表,无数据!');window.location='../../../index.aspx';</script>"); //当Excel表为空时,对用户进行提示
}
else
{
for (int i = 0; i < rowsnum; i++)
{
LeechdomDetails lee = new LeechdomDetails()
{
leechdomName = isleeName(dr[i]["药品名称"].ToString(),i+1),
pinyin = "想要通过取出药品名称的值然后用JS把所有输入的中文改成拼音的第一个字母",
leegptname=dr[i]["通用名称"].ToString(),
lid = _Type(dr[i]["药品类型(中药/西药)"].ToString()),
warrant = dr[i]["批准文号(国药准字)"].ToString(),
spec = dr[i]["规格"].ToString(),
jixing = Jixing(dr[i]["剂型"].ToString()).ToString(),
unit = dr[i]["单位"].ToString(),
pack = packs(dr[i]["包装"].ToString()).ToString(),
leechdomProduce=dr[i]["生产企业名称"].ToString(),
leechdomNumber=dr[i]["生产批号"].ToString(),
productiondate=dr[i]["生产日期(年-月-日)"].ToString(),
validiy = dr[i]["有效期至(年-月-日)"].ToString(),
price = isPrice(Convert.ToDouble(dr[i]["供货价(元)"]), i + 1),
addTime = DateTime.Now.ToString(),
amount = 0,
leeimg = "",
mid = ClonePage._UserId,
updateTime ="",
metaTitle = dr[i]["药品名称"].ToString() + Pramp.CompanyName,
metaDescription = dr[i]["药品名称"].ToString() + Pramp.CompanyName,
metaKeywords = dr[i]["药品名称"].ToString() + Pramp.CompanyName,
};
if (this.ldlist==null)
{
this.ldlist = new List<LeechdomDetails>();
}
ldlist.Add(lee);
}
foreach (LeechdomDetails ldbl in this.ldlist)
{
LeechdomDetailsBLL.Insert(ldbl);
}
List<LeechdomDetails> _list = LeechdomDetailsBLL.Select("mid='" + ClonePage._UserId.ToString() + "'", "mid");
CreateLucence.CreateIndex(_list);
HxwcreateLucence.CreteIndex(_list);
ClonePage.SetUrlCookie("2");
Response.Write("<script>alert('数据导入成功!');window.location='../../../index.aspx';</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('请删除模板最后的空数据!');window.location='../../../index.aspx';</script>");
}
}
private Double isPrice(Double isprice, int i)
{
if (isprice==null)
{
Response.Write("<script>alert('价格第" + i + "行为空')</script>");
}
return 0;
}
/// <summary>
/// 药品名称是否为空
/// </summary>
/// <param name="lname">名称</param>
/// <param name="i">第i行</param>
/// <returns></returns>
protected string isleeName(string lname,int i)
{
if (lname == "")
{
Response.Write("<script>alert('药品名称第" + i + "行为空')</script>");
}
return lname;
}
/// <summary>
/// 查询EXCEL电子表格添加到DATASET
/// </summary>
/// <param name="filenameurl">服务器路径</param>
/// <param name="table">表名</param>
public DataSet ExecleDs(string filenameurl, string table)
{
//连接字符串
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
//初始化连接
OleDbConnection conn = new OleDbConnection(strConn);
//打开连接
conn.Open();
//创建结果集
DataSet ds = new DataSet();
//初始化适配器 读EXCEL表
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [temp$]", conn);
//添填
odda.Fill(ds, table);
//关闭连接
conn.Close();
//释放资源
conn.Dispose();
return ds;
}
/// <summary>
/// 取得药品类型
/// </summary>
/// <param name="_name"></param>
/// <returns></returns>
private Guid _Type(string _name)
{
List<LeechdomType> _list = LeechdomTypeBLL.Select("lType='" + _name + "'", "lType");
if (_list != null && _list.Count != 0)
{
return _list[0].lid;
}
return new Guid();
}
/// <summary>
/// 取得剂型类型
/// </summary>
/// <param name="_name"></param>
/// <returns></returns>
private int Jixing(string _name)
{
List<JiXing> _list = JiXingBLL.Select("_name='" + _name + "'", "_name");
if (_list != null && _list.Count != 0)
{
return Convert.ToInt32(_list[0].id);
}
return 1;
}
private string packs(string _pack)
{
List<Packs> _packs = PacksBLL.Select("packname='" + _pack + "'", "packname");
if (_packs!=null && _packs.Count!=0)
{
return _packs[0].id.ToString();
}
return "";
}
}