示例#1
0
        /// <summary>
        /// 根据传入参数生成SQL
        /// </summary>
        private void AnalysisData()
        {
            try
            {
                string errorStr = "";

                DataTable dt = Session["DataTable"] as DataTable;

                Common.ConfigModel modelConfig = Common.ConfigOperater.GetConfigModel();

                DAL.SqlHelper.connectionString = modelConfig.SqlConnectionString;

                InitConfig(modelConfig);

                string result = SetExcelToSQL.RenderDataTableToSQLResult(dt, modelConfig, ref errorStr);

                if (errorStr != "")
                {
                    SQLResult.InnerText = errorStr;
                    sqlConnection.Value = modelConfig.SqlConnectionString;
                }
                else
                {
                    SQLResult.InnerText = result;
                    sqlConnection.Value = modelConfig.SqlConnectionString;
                }
            }
            catch (Exception ex)
            {
                SQLResult.InnerText = ex.ToString();
            }
        }
示例#2
0
        protected void UploadExcel(object sender, EventArgs e)
        {
            if (ExcelFile.HasFile)
            {
                string   fileName = ExcelFile.FileName;
                FileInfo fileInfo = new FileInfo(fileName);

                if (fileInfo.Extension == ".xls" || fileInfo.Extension == ".xlsx")
                {
                    fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + fileName;

                    ConfigModel modelConfig = ConfigOperater.GetConfigModel();

                    string filePath = Server.MapPath("/" + modelConfig.UploadPath + "/" + fileName);

                    ExcelFile.SaveAs(filePath);

                    try
                    {
                        DataTable dt = ExcelHelper.GetExcelToDataTable(url: filePath, startRow: (modelConfig.StartRow > 0?modelConfig.StartRow - 1:0));

                        string errorStr = "";

                        string result = SetExcelToSQL.RenderDataTableToSQLResult(dt, modelConfig, ref errorStr);

                        if (errorStr != "")
                        {
                            RegisterScript("alert('导入错误!" + errorStr.Replace("\n", "").Replace("\r", "").Replace("'", "\"") + "')");
                        }
                        else
                        {
                            Context.Items.Add("ResultSQL", result);
                            Context.Items.Add("SQLConnection", modelConfig.SqlConnectionString);

                            Server.Transfer("/Excel/Result.aspx");
                        }
                    }
                    catch (Exception ex)
                    {
                        RegisterScript("alert('导入错误!" + ex.ToString().Replace("\n", "").Replace("\r", "").Replace("'", "\"") + "')");
                    }
                    finally
                    {
                        File.Delete(filePath);
                    }
                }
                else
                {
                    RegisterScript("alert('错误的文件!')");
                }
            }
            else
            {
                RegisterScript("alert('请选择文件!')");
            }
        }