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 DSWeb.EntityDA;
using System.IO;

namespace DSWeb.Invoice
{
    public partial class InvoiceMakeOut : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ibExcel_Click(object sender, EventArgs e)
        {
            //setExcelReport();
            setExcelReport2();
        }

        protected void setExcelReport2()
        {
            if (this.hdSQL.Value.Trim() != "")
            {
                T_ALL_DA T_ALL_DA = new T_ALL_DA();
                DataSet DS = T_ALL_DA.GetAllSQL(this.hdSQL.Value.Trim());
                if (DS != null)
                {
                    ExcelDA ExcelDA = new EntityDA.ExcelDA();
                    MemoryStream ms = ExcelDA.RenderToExcel(DS.Tables[0], this.hdNAMES.Value.Trim());
                    ExcelDA.RenderToBrowser(ms, Context, "report.xls");
                }
            }
        }

        protected void setExcelReport()
        {
            StringWriter sw = new StringWriter();
            string sSQL = "";
            //sw.WriteLine("结算单位\t委托编号\t主提单号\t分提单号\t船名\t航次\tETD\t目的港\t揽货人\t应收RMB\t实收RMB\t应收USD\t实收USD\t余额");
            sw.WriteLine(this.hdNAMES.Value.Trim());
            T_ALL_DA T_ALL_DA = new T_ALL_DA();
            DataSet DS = T_ALL_DA.GetAllSQL(this.hdSQL.Value.Trim());
            if (DS != null)
            {
                foreach (DataTable myTable in DS.Tables)
                {
                    foreach (DataRow myRow in myTable.Rows)//遍历表
                    {
                        sSQL = "";
                        for (int i = 0; i < myTable.Columns.Count; i++)
                        {
                            if (sSQL.Trim() != "")
                            {
                                sSQL += "\t";
                            }
                            if (myRow[i].ToString() == null || myRow[i].ToString() == "")
                            {
                                sSQL += "";
                                if (sSQL.Trim() == "")
                                {
                                    sSQL += "\t";
                                }
                            }
                            else
                            {
                                sSQL += myRow[i].ToString().Trim();
                            }
                        }
                        sw.WriteLine(sSQL);
                    }
                }
                //
                sw.Close();
                switch ("MUEXCEL")
                {
                    case "MUEXCEL":
                        Response.AddHeader("Content-Disposition", "attachment; filename=report.xls");
                        Response.ContentType = "application/ms-excel";
                        break;
                    case "MUTEXT":
                        Response.AddHeader("Content-Disposition", "attachment; filename=report.txt");
                        Response.ContentType = "application/txt";
                        break;
                    case "MUWORD":
                        Response.AddHeader("Content-Disposition", "attachment; filename=report.doc");
                        Response.ContentType = "application/ms-word";
                        break;
                    case "MUPDF":
                        Response.AddHeader("Content-Disposition", "attachment; filename=report.pdf");
                        Response.ContentType = "application/pdf";
                        break;
                    case "MUXML":
                        Response.AddHeader("Content-Disposition", "attachment; filename=report.xml");
                        Response.ContentType = "application/xml";
                        break;
                }
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.Write(sw);
                Response.End();
            }
        }
    }
}