• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
笨笨笨笨
博客园    首页    新随笔    联系   管理    订阅  订阅

完成第六步,DataSet与XML互转

F:"项目"Web"DataManagement"IndicatorConfiguration.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml;
using System.Data.SqlClient;
using System.IO;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Text;
using DataAccess.PO;
using BusinessFacade;

public partial class DataManagement_IndicatorConfiguration : System.Web.UI.Page
{

    
//private void GetIndicatorData()
    
//{
    
//    //获取用户输入
    
//    string startDateString = DateRangeControl1.StartDateString;
    
//    string endDateString = DateRangeControl1.EndDateString;

    
//    //获取数据
    
//    TimeSequenceDataManager.GetIndicatorMonthBasedTimeSequenceValue(IndicatorMultiSelectControl1.SelectedIndicatorList, startDateString, endDateString);

    
//}

    
private void Bind(int pageIndex)
    
{
        
//if (startyear.Text == "" || startmonth.Text == "")
        
//    startLabel.Text = "请输入起始年月!";
        
//if (endyear.Text == "" || endmonth.Text == "")
        
//    endLabel.Text = "请输入终止年月!";

        
//StringBuilder indiCodes = new StringBuilder();
        
//if (IndicatorMultiSelectControl1.SelectedIndicatorList != null)
        
//{
        
//    foreach (Indicator indi in IndicatorMultiSelectControl1.SelectedIndicatorList)
        
//    {
        
//        indiCodes.Append("'" + indi.IndicatorCode + "',");//加逗号是因为参数里有逗号分隔符
        
//    }
        
//}

        
//if (indiCodes == null || indiCodes.Length == 0)
        
//    return;
        
//indiCodes.Remove(indiCodes.Length - 1, 1);//去掉加上的逗号

        
string indiID = Session["IndicatorID"] as string;
        
string typeid = Session["TypeID"] as string;
        
string startyear = DateRangeControl1.StartDateString.Substring(DateRangeControl1.StartDateString.Length - 7, 4);
        
string endyear = DateRangeControl1.EndDateString.Substring(DateRangeControl1.EndDateString.Length - 7, 4);
        
string startmonth = DateRangeControl1.StartDateString.Substring(DateRangeControl1.StartDateString.Length - 2, 2);
        
string endmonth = DateRangeControl1.EndDateString.Substring(DateRangeControl1.EndDateString.Length - 2, 2);

        
string filter = "IndicatorID  in ('" + indiID.ToString() + "') and TypeID in ( " + typeid.ToString() + " ) and YearNo > "+startyear+" and YearNo < "+endyear+" and MonthNo > "+startmonth+" and MonthNo < "+endmonth;

        
/**////从PBC库中取出数据  
        PBCIntegrationManager manager = new PBCIntegrationManager();
        List
<Import_MonthBase> monthbase = new List<Import_MonthBase>();
        monthbase 
= manager.GetEntityList(filter);

        Session[
"PBCmonthbase"] = manager.GetList(filter);

        DataTable dt 
= PBCAnalyzer.GetPBCMonthBase(monthbase);
        ImportGridView.DataSource 
= dt;
        ImportGridView.PageIndex 
= pageIndex;
        ImportGridView.DataBind();
    }

   
    
protected void ShowButton_Click(object sender, EventArgs e)
    
{
        
/**////从EWS库取指标
        ImportGridView.Visible = true;
        Bind(
0);
    }


    
protected void ImportGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        
//for (int i = 0; i < 20; i++)
        
//{
        
//    ImportGridView.DataKeys[ImportGridView.Items[i].ItemIndex].ToString();
        
//}
        Bind(e.NewPageIndex);
    }


    
protected void SelectBoxCheckedChanged(object sender, EventArgs e)
    
{
        
//CheckBox box = (CheckBox)sender;
        
//int rowIndex = ((GridViewRow)box.NamingContainer).RowIndex;
        
//string uid = ImportGridView.DataKeys[rowIndex].Value.ToString();

        
//Response.Write("<script>alert('" + uid + "')</script>");
        
        
/**/////取出uid对应数据
        //string checkResult = "UID  in ( " + uid.ToString() + " )";
        
//PBCIntegrationManager manager = new PBCIntegrationManager();
        
//List<Import_MonthBase> monthbase = new List<Import_MonthBase>();
        
//monthbase = manager.GetEntityList(checkResult);
        
//Session["PBCmonthbase"] = monthbase;

       
    }


    
protected void Import_Click(object sender, EventArgs e)
    
{
        
/**////从EWS库取指标
        //List<Import_MonthBase> mbs = Session["PBCmonthbase"] as List<Import_MonthBase>;
        
//string file = "D:\\PBCmonth.xml";
        
//TextWriter writer = new StreamWriter(file);
        
//XmlSerializer sr = new XmlSerializer(typeof(List<Import_MonthBase>));
        
//sr.Serialize(writer, mbs);
        
//writer.Close();

        
if (Session["PBCmonthbase"] == null) return;
        EWSOutputHelper.ExportPBCMonth((DataSet)Session[
"PBCmonthbase"]);

    }


    
protected void PBCButton_Click(object sender, EventArgs e)
    
{
        
        
/**////文本框 关键字匹配
        string keyWord = KeyWordTextBox.Text;
        
string filter = "IndicatorName LIKE '%" + keyWord + "%' or IndicatorID LIKE '%" + keyWord + "%'";

        PBCIntegrationManager manager 
= new PBCIntegrationManager();
        List
<PBCIndicatorCode> cnname = new List<PBCIndicatorCode>();
        cnname 
= manager.GetCnnameList(filter);


        DataTable dt 
= PBCAnalyzer.GetCnnameList(cnname);

        
/**////显示数据
        PBCGridView.DataSource = dt;
        PBCGridView.DataBind();
        PBCGridView.Visible 
= true;
        TypeidGridView.Visible 
= false;
        ImportGridView.Visible 
= false;
       
    }



    
//protected void PBCGridView_RowCommand(object sender, GridViewCommandEventArgs e  EventArgs e)
    
//{
        
    
//}

         
protected void GetIDButton_Command(object sender, CommandEventArgs e)  
         
{  
            
int index = Convert.ToInt32(e.CommandArgument);  
            GridViewRow row 
= PBCGridView.Rows[index];  

            
/**////取出Indicatorid值
            string indiID = row.Cells[1].Text;

            
/**////通过Indicatorid取出TypeID
            string filter = "IndicatorID  in ( '" + indiID.ToString() + "' )";

            PBCIntegrationManager manager 
= new PBCIntegrationManager();
            DataTable typeid 
= new DataTable();
            typeid 
= manager.GetOnlyEntityList(filter);
            DataTable dt 
= typeid;

            TypeidGridView.DataSource 
= dt;
            TypeidGridView.DataBind();

            TypeidGridView.Visible 
= true;
            PBCGridView.Visible 
= false;
           
        }


       

        
protected void GetNameButton_Command(object sender, CommandEventArgs e)
        
{

            
int index = Convert.ToInt32(e.CommandArgument); 
            GridViewRow row 
= TypeidGridView.Rows[index];

            
//取出IndicatorID和TypeName
            string indiID = row.Cells[1].Text.Trim();
            
string tpName = row.Cells[2].Text.Trim();

            
//通过TypeName得到TypeID
            string filter = "TypeName  in ( '" + tpName.ToString() + "' )";
            PBCIntegrationManager manager 
= new PBCIntegrationManager();
            DataSet ds 
= new DataSet();
            ds 
= manager.GetTypeID(filter);
            
string typeid = ds.Tables[0].Rows[0][0].ToString();

            Session[
"IndicatorID"] = indiID;
            Session[
"TypeID"] = typeid;

        }


        
//private void GetIndicatorData()
        
//{
        
//    //获取用户输入
        
//    string startDateString = DateRangeControl1.StartDateString;
        
//    string endDateString = DateRangeControl1.EndDateString;

        
//    _indicatorList = IndicatorMultiSelectControl1.SelectedIndicatorList;

        
//    if (_indicatorList == null) return;

        
//    //获取数据
        
//    TimeSequenceDataManager.GetIndicatorTimeSequenceValue(_indicatorList, startDateString, endDateString);

        
//}

    
protected void Export_Click(object sender, EventArgs e)
    
{
        
string path = Request.MapPath("~/UploadFiles") + "\\PBCmonth.xml";
        FileUpload.SaveAs(path);

        
string filename = path;


        
//UpImportFile(File1.Value);            

        
//DataImport di = new DataImport();
        DataTable dt;
        
try
        
{
            dt 
= DataImport.ConvertXMLFileToDataSet(path).Tables[0];
        }

        
catch
        
{
            Response.Write(
"<script language=javascript>");
            Response.Write(
"alert('数据模版出错,无法生成表格!');");
            Response.Write(
"</script>");
            
return;
        }

        
//DataTable Monthdt = null;

        
if (dt.Rows.Count == 0)
        
{
            Response.Write(
"<script language=javascript>");
            Response.Write(
"alert('目前模板中没有数据!');");
            Response.Write(
"</script>");
            
return;
        }



        
//if (di.MonthData_BuildTableForImport(dt, ref Monthdt) == true)
        
//{

            
//databind 
            ExportGridView.DataSource = dt.DefaultView;
            ExportGridView.DataBind();
            
//Session["PBCmonth"] = dt;
        
//}
        
//else
        
//{
        
//    Response.Write("<script language=javascript>");
        
//    Response.Write("alert('月度数据表中数据出错,无法导入!');");
        
//    Response.Write("</script>");
        
//    return;
        
//}
    }

}

 

F:"项目"Web"App_Code"DataImport.cs

 

 /**//// <summary>
    
/// 将XML转DataSet
    
/// </summary>
    
/// <param name="xmlData"></param>
    
/// <returns></returns>

    public static DataSet ConvertXMLFileToDataSet(string xmlFile)
    
{
        StringReader stream 
= null;
        XmlTextReader reader 
= null;
        
try
        
{
            XmlDocument xmld 
= new XmlDocument();
            xmld.Load(xmlFile);

            DataSet xmlDS 
= new DataSet();
            stream 
= new StringReader(xmld.InnerXml);
            
//从stream装载到XmlTextReader

            reader 
= new XmlTextReader(stream);
            xmlDS.ReadXml(reader);
            
//xmlDS.ReadXml(xmlFile);

            
return xmlDS;
        }

        
catch (System.Exception ex)
        
{
            
throw ex;
        }

        
finally
        
{
            
if (reader != null)
                reader.Close();
        }

    }

 

 F:"项目"Web"App_Code"EWSOutputHelper.cs

 

 public static void ExportPBCMonth(DataSet ds)
    
{


        
//System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        /**/////System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        
//oStringWriter.WriteLine(filename);

        
/**/////show header
        //StringBuilder sb = new StringBuilder();
        /**/////  dg.ShowHeader = true;
        //foreach (DataColumn col in dt.Columns)
        
//{

        
//    sb.Append(col.ToString());//BUG:无法获取
        
//    if (dt.Columns.IndexOf(col) != dt.Columns.Count - 1)
        
//        sb.Append(COMMA_SPLITTER);
        
//}
        
//oStringWriter.WriteLine(sb.ToString());

        
/**/////show contents
        //foreach (DataRow row in dt.Rows)
        
//{
        
//    StringBuilder contentSb = new StringBuilder();
        
//    if (row != null && row.ItemArray != null)
        
//        for (int i = 0; i < row.ItemArray.Length; i++)
        
//        {
        
//            contentSb.Append(row.ItemArray[i].ToString());
        
//            if (i != row.ItemArray.Length - 1)
        
//                contentSb.Append(COMMA_SPLITTER);
        
//        }
        
//    oStringWriter.WriteLine(contentSb.ToString());
        
//}
       
     
        OutputPBCMonth(ds);
    }


    
private static void OutputPBCMonth(DataSet ds)
    
{
        
//HttpContext.Current.Response.Buffer = true;
        
//HttpContext.Current.Response.Charset = ENCODING_STR;
        
//HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=PBCmonth.xml");
        
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(ENCODING_STR);
        
//HttpContext.Current.Response.ContentType = "text/xml";
        
//HttpContext.Current.Response.Write(ds.GetXml());

        
/**/////HttpContext.Current.Response.Write(oStringWriter.g);
        //HttpContext.Current.Response.End();

        StringBuilder sb 
= new StringBuilder();
        XmlWriterSettings settings 
= new XmlWriterSettings();
        settings.Indent 
= true;

        XmlWriter writer 
= XmlWriter.Create(sb, settings);
        writer.WriteProcessingInstruction(
"xml", "version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"");
        ds.WriteXml(writer, XmlWriteMode.WriteSchema);  

         HttpContext.Current.Response.Charset 
= "UTF-8";   
         HttpContext.Current.Response.ContentEncoding 
= Encoding.UTF8;    
         HttpContext.Current.Response.ContentType 
= "application/xml";
         HttpContext.Current.Response.AppendHeader(
"Content-Disposition", "attachment;filename=PBCmonth.xml");    
         HttpContext.Current.Response.Write(sb.ToString());  
         HttpContext.Current.Response.End(); 
    }

 

 

 

 

 

 

 

posted @ 2008-11-28 23:28  笨笨笨笨  阅读(198)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3