using System; using System.Configuration; using System.Data; using System.Data.OleDb; using System.IO; using System.Text.RegularExpressions; using SuperMap.RealEstate.Web.UI; using SuperMap.RealEstate.CoreFrameWork; using HWRB = SuperMap.RealEstate.HighWay.Running.Business; namespace SuperMap.RealEstate.HighWay.CloudCompents.CommodityChange { public partial class UpdateExcel : Storage.UI.BasePage { protected string _BusinessType = ConfigurationManager.AppSettings["BusinessType"].ToString(); protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) return; btnDownExcel.Enabled = false; //设置确认导入按钮焦点时间 SetControlClientAction(btnDownExcel, false); } #region 上传Excel -> 显示在Table中 //上传按钮 protected void btnUpload_Click(object sender, EventArgs e) { if (fpUpload.HasFile) { string[] AExt = fpUpload.FileName.Split('.'); string strExt = ""; if (AExt.Length > 1) { strExt = AExt[AExt.Length - 1]; } //扩展名必须为xls if (strExt == "xls") { //创建上传服务器的临时Excel名称 string sUploadFile = sUploadFile = GetGuid() + '.' + strExt; string sPathFile = Server.MapPath("/ImportExcel"); if (Directory.Exists(sPathFile) == false) { Directory.CreateDirectory(sPathFile); } //指定文件夹的路径 string strSavePath = Path.Combine(sPathFile, sUploadFile); //上传文件 fpUpload.SaveAs(strSavePath); //临时保存数据源 lblUploadFile.Text = strSavePath; string sheetname = "Sheet1$"; string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + lblUploadFile.Text + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'"; //此连接可以操作.xls与.xlsx文件 (支持Excel2003 和 Excel2007 的连接字符串) System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn); conn.Open(); DataTable dtExcelSchema = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); if (dtExcelSchema.Rows.Count > 0) { sheetname = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); } System.Data.OleDb.OleDbDataAdapter oada = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetname + "]", strConn); //获取数据导出到DataSet DataSet ds = new DataSet(); oada.Fill(ds); conn.Close(); DataTable dt = ds.Tables[0]; string _Err = ""; int _Status = 1; table1.InnerHtml = GetCommodityTalbe(dt, out _Err, out _Status); if (_Err.Length > 0) { Alert(_Err); } if (_Status == 1) { btnDownExcel.Enabled = true; } } else { Alert("请选择上传XLS文件"); } } else { Alert("请选择上传XLS文件"); } } //获取excel中的表格 _Status 0 为错误excel private string GetCommodityTalbe(DataTable dt, out string _Err, out int _Status) { _Status = 1; string _Result = "
| 序列 | " + "业态 | " + "商品类型 | " + "商品编码 | " + "商品名称 | " + "商品条码 | " + "商品单位 | " + "商品规格 | " + "商品产地 | " + "质量等级 | " + "是否散装 | " + "称重方式 | " + "商品状态 | " + "进价税率 | " + "销售税率 | " + "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| " + (i + 1) + " | "; _Result += "" + dt.Rows[i][0].ToString() + " | "; _Result += "" + dt.Rows[i][1].ToString() + " | "; _Result += "" + dt.Rows[i][2].ToString() + " | "; _Result += "" + dt.Rows[i][3].ToString() + " | "; _Result += "" + dt.Rows[i][4].ToString().Replace("&", "&").Replace( "<", "<").Replace(">", ">").Replace("°", "°").Replace("·", "·") + " | "; _Result += "" + dt.Rows[i][5].ToString() + " | "; _Result += "" + dt.Rows[i][6].ToString() + " | "; _Result += "" + dt.Rows[i][7].ToString() + " | "; _Result += "" + dt.Rows[i][8].ToString() + " | "; _Result += "" + dt.Rows[i][9].ToString() + " | "; _Result += "" + dt.Rows[i][10].ToString() + " | "; _Result += "" + dt.Rows[i][11].ToString() + " | "; _Result += "" + dt.Rows[i][12].ToString() + " | "; _Result += "" + dt.Rows[i][13].ToString() + " | "; _Result += "
| 文件格式不正确 | "; _Result += "||||||||||||||