コード例 #1
0
ファイル: Excel_Func.cs プロジェクト: shew990/TaiBang
        public bool ImportExcel(int StockType, UserInfo user, DataSet HeadDS, DataSet DetailDS, ref string strError)
        {
            string strKey     = string.Empty;
            string HeadJson   = string.Empty;
            string DetailJson = string.Empty;

            StringBuilder jsonString = new StringBuilder();

            jsonString.Append("{\"Result\":\"TRUE\",\"ErrMsg\":null,\"RecordList\": [");

            for (int i = 0; i < HeadDS.Tables["Sheet1"].Rows.Count; i++)
            {
                DataSet   ImportDS  = new DataSet();
                DataTable TableHead = new DataTable();

                TableHead = HeadDS.Tables["Sheet1"].Clone();


                //添加行到新的dataset
                TableHead.Rows.Add(HeadDS.Tables["Sheet1"].Rows[i].ItemArray);
                ImportDS.Tables.Add(TableHead);

                if (i > 0)
                {
                    jsonString.Append(",");
                }
                jsonString.Append("{");
                jsonString.Append("\"WmsVoucherType\":");
                jsonString.Append("\"" + HeadDS.Tables["Sheet1"].Rows[i]["WmsVoucherType"].ToString() + "\"");
                jsonString.Append(",");
                HeadJson = JSONHelper.DateSetObjectToJson(ImportDS).Replace("Sheet1", "Head");
                jsonString.Append(HeadJson.Replace("Head", "\"Head\""));
                jsonString.Append(",");

                strKey = HeadDS.Tables["Sheet1"].Rows[i][0].ToString();

                DataTable TableDetail = new DataTable();
                TableDetail = DetailDS.Tables["Sheet2"].Clone();

                for (int j = 0; j < DetailDS.Tables["Sheet2"].Rows.Count; j++)
                {
                    if (strKey.CompareTo(DetailDS.Tables["Sheet2"].Rows[j][0].ToString()) == 0)
                    {
                        TableDetail.Rows.Add(DetailDS.Tables["Sheet2"].Rows[j].ItemArray);
                    }
                }
                DataSet ImportDetailDS = new DataSet();
                ImportDetailDS.Tables.Add(TableDetail);
                DetailJson = JSONHelper.DateSetListToJson(ImportDetailDS).Replace("Sheet2", "Detail");

                jsonString.Append(DetailJson.Replace("Detail", "\"Detail\""));

                jsonString.Append("}");
            }

            jsonString.Append("]}");

            ParamaterField_Func pfunc = new ParamaterField_Func();

            return(pfunc.GetExcelmport(StockType, jsonString.ToString(), "ERP", ref strError));
        }