一、输出到excel

函数:

    protected void InputFileTheme(object[] Header,object [] DataFileds,string sql,string filename)
        {
            SqlDBHandler dbHandler=this.getDBHandler();
            string filterStr=",";
            string strTemp;

            Hashtable hashTable=new Hashtable();
            //hashTable.Add("laguage","513");//正文语种
            /*hashTable.Add("themetype3","504");//参考地图地域类别
            hashTable.Add("themetype4","505");//参考地图形式类别
            hashTable.Add("themetype5","508");//基础教材性质类别
            hashTable.Add("themetype6","506");//基础教材学科类别
            hashTable.Add("themetype8","519");//教辅性质
            //基础教材版别
            hashTable.Add("usesegment","509");//基础教材使用学段
            hashTable.Add("useterm","510");//基础教材使用学期
            hashTable.Add("managemode","511");//编务管理类别
            hashTable.Add("electype","608");//电子出版物类型 (dictid=608)

            hashTable.Add("bindmode","516");//装帧形式
            hashTable.Add("papersize","605");//纸张尺寸
            hashTable.Add("isfund","517");//出版基金类别
            */
            if(sql.Trim().Equals(""))
                return;
            if(DataFileds==null)
                return;
            if(filename.Equals(""))
                filename="default";
            int len;
            string strInput;

            StringWriter sw=new StringWriter();

            //输出表头
            if(Header!=null)
            {
                strInput="";
                ;len<Header.Length;len++)
                {
                    if(Header[len]!=null)
                    {
                        if(strInput.Equals(""))
                            strInput=Header[len].ToString();
                        else
                            strInput+=filterStr+Header[len].ToString();
                    }
                }//for(len=0;len<Header.Length;len++)
                strInput="序号"+filterStr+strInput;
                sw.WriteLine(strInput);
            }//if(Header!=null)

            DataTable dt=dbHandler.ExecuteDataTable(sql);
            dt=CncBw.Db.ThemeInfoSet.ThemeInfoDictTransfer(dt);
            ;
            foreach(DataRow dr in dt.Rows)
            {
                strInput="";
                row+=;
                ;len<DataFileds.Length;len++)
                {
                    )
                    {

                        //输入字段,输出数据
                        if(dr[DataFileds[len].ToString()]!=null)
                        {
                            strTemp=dr[DataFileds[len].ToString()].ToString().Trim();
                            strTemp=strTemp.Replace(",",",");
                            strInput=strTemp;
                        }
                        else
                        {
                            strInput="null";
                        }//if(dr[DataFileds[len]]!=null)
                    }
                    else
                    {
                        if(dr[DataFileds[len].ToString()]!=null)
                        {
                            strTemp=dr[DataFileds[len].ToString()].ToString().Trim();
                            strTemp=strTemp.Replace(",",",");
                            strTemp = strTemp.Replace("\r\n", " ");

                            strInput+=filterStr+strTemp;
                        }
                        else
                            strInput+=filterStr+"null";
                    }//if(len==0)

                }//for(len=0;len<DataFileds.Length;len++)
                strInput=row.ToString()+filterStr+strInput;
                sw.WriteLine(strInput);
            }//foreach(DataRow dr in dt.Rows)

            sw.Close();
            Response.AddHeader("Content-Disposition", "attachment;filename="+filename+".csv");
            Response.ContentType = "application/ms-excel";
            Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
            //Response.ContentEncoding=System.Text.Encoding.GetEncoding("unicode");
            Response.Write(sw);
            Response.End();
            dbHandler.Close();
        }

二、调用

this.InputFileTheme(header,dataFileds,strSql,"bookinfo");
05-04 06:15