SQL数据 导出Excel

突然有一个需求,客户需要把某一些数据导出成Excel,之前没有做过这方面的需求,Goolg了一下,可以用BCP来完成,但是最后发现导出的Excel有换行了,所以最终把数据的内容的换行符,回车符,双引号替换掉,下面是是代码(只适用于偶尔的数据导出

select * into test2 from CUS_QDiathesis



update test2 set QD_TARGET=REPLACE(convert(nvarchar(4000),QD_TARGET),CHAR(13) , ''),

QD_SELFCOMMENT=REPLACE(convert(nvarchar(4000),QD_SELFCOMMENT),CHAR(13) , ''),

QD_SUPERVISORCOMMENT=REPLACE(convert(nvarchar(4000),QD_SUPERVISORCOMMENT),CHAR(13) , ''),

QD_IMPROVETARGET=REPLACE(convert(nvarchar(4000),QD_IMPROVETARGET),CHAR(13) , ''),

QD_IMPROVEPLAN=REPLACE(convert(nvarchar(4000),QD_IMPROVEPLAN),CHAR(13) , ''),

QD_ESTIMATEDATE=REPLACE(convert(nvarchar(4000),QD_ESTIMATEDATE),CHAR(13) , ''),

QD_FINISHINFO=REPLACE(convert(nvarchar(4000),QD_FINISHINFO),CHAR(13) , ''),

QD_PERFORMANCE_REQUIREMENT=REPLACE(convert(nvarchar(4000),QD_PERFORMANCE_REQUIREMENT),CHAR(13) , ''),

QD_DETAIL_ACTIONPLAN=REPLACE(convert(nvarchar(4000),QD_DETAIL_ACTIONPLAN),CHAR(13) , '')

go



update test2 set QD_TARGET=REPLACE(convert(nvarchar(4000),QD_TARGET),CHAR(10) , ''),

QD_SELFCOMMENT=REPLACE(convert(nvarchar(4000),QD_SELFCOMMENT),CHAR(10) , ''),

QD_SUPERVISORCOMMENT=REPLACE(convert(nvarchar(4000),QD_SUPERVISORCOMMENT),CHAR(10) , ''),

QD_IMPROVETARGET=REPLACE(convert(nvarchar(4000),QD_IMPROVETARGET),CHAR(10) , ''),

QD_IMPROVEPLAN=REPLACE(convert(nvarchar(4000),QD_IMPROVEPLAN),CHAR(10) , ''),

QD_ESTIMATEDATE=REPLACE(convert(nvarchar(4000),QD_ESTIMATEDATE),CHAR(10) , ''),

QD_FINISHINFO=REPLACE(convert(nvarchar(4000),QD_FINISHINFO),CHAR(10) , ''),

QD_PERFORMANCE_REQUIREMENT=REPLACE(convert(nvarchar(4000),QD_PERFORMANCE_REQUIREMENT),CHAR(10) , ''),

QD_DETAIL_ACTIONPLAN=REPLACE(convert(nvarchar(4000),QD_DETAIL_ACTIONPLAN),CHAR(10) , '')

go





update test2 set QD_TARGET=REPLACE(convert(nvarchar(4000),QD_TARGET),'"' , ''),

QD_SELFCOMMENT=REPLACE(convert(nvarchar(4000),QD_SELFCOMMENT),'"' , ''),

QD_SUPERVISORCOMMENT=REPLACE(convert(nvarchar(4000),QD_SUPERVISORCOMMENT),'"' , ''),

QD_IMPROVETARGET=REPLACE(convert(nvarchar(4000),QD_IMPROVETARGET),'"' , ''),

QD_IMPROVEPLAN=REPLACE(convert(nvarchar(4000),QD_IMPROVEPLAN),'"' , ''),

QD_ESTIMATEDATE=REPLACE(convert(nvarchar(4000),QD_ESTIMATEDATE),'"' , ''),

QD_FINISHINFO=REPLACE(convert(nvarchar(4000),QD_FINISHINFO),'"' , ''),

QD_PERFORMANCE_REQUIREMENT=REPLACE(convert(nvarchar(4000),QD_PERFORMANCE_REQUIREMENT),'"' , ''),

QD_DETAIL_ACTIONPLAN=REPLACE(convert(nvarchar(4000),QD_DETAIL_ACTIONPLAN),'"' , '')

go

下面是创建一个视图用BCP导出的代码

CREATE view [dbo].[ReportView]

as

select c.*, a.* from test2 a,   EVA_QuarterEvaluate b,  SEC_User  c    where a.QUARTEREVALUATE_ID=b.QUARTEREVALUATE_ID and b.USER_ID=c.USER_ID



GO



EXEC master..xp_cmdshell 'BCP "SELECT * FROM MLK_PDP.dbo.ReportView" queryout c:\currency5.xlt -c -U"sa" -P"sa"'

go

注意:可能上面执行需要启用xp_cmdshell,下面是启用命令

EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;

关闭一样.只是将上面的后面的那个"1"改成"0"就可以了. 

EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;


最后发现,在正式环境,我的帐户没有执行exec 的权限(那时郁闷呀),所以最终还是用Asp.Net代码来处理,下面是用asp.net 导出成Excel的代码

 

        protected void btnExport_Click(object sender, EventArgs e)

        {

            

            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MLK_PDPConnectionString"].ConnectionString);

            string strsql = "select * from ReportView";

            SqlCommand cmd = new SqlCommand(strsql, conn);

            conn.Open();



            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();

            da.Fill(dt);

            CreateExcel(dt, "dia.xlt");

            

        }



        private void CreateExcel(DataTable dt, string fileName)

        {

            HttpResponse resp;

            resp = Page.Response;

            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

            resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);

            string colHeaders = "", ls_item = "";



            ////定义表对象与行对象,同时用DataSet对其值进行初始化

            //DataTable dt = ds.Tables[0];

            DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的

            int i = 0;

            int cl = dt.Columns.Count;



            //取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符

            for (i = 0; i < cl; i++)

            {

                if (i == (cl - 1))//最后一列,加n

                {

                    colHeaders += dt.Columns[i].Caption.ToString() + "\n";

                }

                else

                {

                    colHeaders += dt.Columns[i].Caption.ToString() + "\t";

                }



            }

            resp.Write(colHeaders);

            //向HTTP输出流中写入取得的数据信息



            //逐行处理数据 

            foreach (DataRow row in myRow)

            {

                //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据   

                for (i = 0; i < cl; i++)

                {

                    if (i == (cl - 1))//最后一列,加n

                    {

                        ls_item += row[i].ToString() + "\n";

                    }

                    else

                    {

                        ls_item += row[i].ToString() + "\t";

                    }



                }

                resp.Write(ls_item);

                ls_item = "";



            }

            resp.End();

        }

      

 

你可能感兴趣的:(导出Excel)