根据下拉框动态生成Excel 并带下载 和上传(一)

限于喜欢 提交于 2020-01-25 19:52:05

//ExportData.aspx  html中的JS代码

/function GetExcelByTypeId()
{
    var obj = $("#<%=ddltype.ClientID %>");
    var typeid = $(obj).find("option:selected").val();
    if(typeid=="0")
        return;
    var url="ExportData.aspx?ajaxtype=getexcel&typeid="+typeid;
    var rethtml = createTextHttpRequest(url);
    if(rethtml.length>0)
        $("#spdownaddress").append(rethtml);
}

//ExportData.aspx  CS主要代码

//全局变量

        public string dirpath = @"D:\PublicSite\downfile\";
        public string filepath = "http://sa.sa.com/downfile/";
        public string ajaxtype
        { get { return null == Request.QueryString["ajaxtype"] ? string.Empty : Request.QueryString["ajaxtype"].Trim(); } }

 

//根据下拉框动态生成Excel  //包括2003/2007版本
//AJAX 
            if (!string.IsNullOrEmpty(ajaxtype))
            {
                switch (ajaxtype)
                {
                    case "getexcel"://根据类型获取数据并生成Excel 同时返回下载地址
                        string typeid = Request.QueryString["typeid"];
                        int recordcount = 0;
                        string title2003 = "member-2003版本";
                        string title2007 = "member-2007版本";
                        string xmlFilePath2003 = dirpath + title2003 + ".xls";
                        string xmlFilePath2007 = dirpath + title2007 + ".xlsx";
                        if (System.IO.File.Exists(xmlFilePath2003) && System.IO.File.Exists(xmlFilePath2007))//判断此目录下是否存在此文件
                        {
                            //string rethtml = "<a href='javascript:DownFile(\"" + filepath + title2003 + "\")'>" + title2003 + "</a>&nbsp;&nbsp;<a href='javascript:DownFile(\"" + filepath + title2007 + "\")'>" + title2007 + "</a>";
                            string rethtml = "<a href='down.aspx?filename=" +Server.UrlEncode( filepath + title2003 )+ "'>" + title2003 + "</a>&nbsp;&nbsp;<a href='javascript:DownFile(\"" + filepath + title2007 + "\")'>" + title2007 + "</a>";

                            Response.Write(rethtml);
                            Response.End();
                            return;
                        }
                        Mltm.Model.Users.T_UserBasic objinfo = new Mltm.Model.Users.T_UserBasic();
                        objinfo.C_UserID.SelectColumn = objinfo.C_LoginName.SelectColumn = true;
                        System.Data.DataTable dt = Mltm.BLL.Users.B_UserBasic.QueryDataToTable(objinfo, "C_UserType='" + typeid + "'", "C_UserID desc");
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            object miss = Missing.Value;

                            Application excelApp = new Application();
                            Workbook workBook = excelApp.Workbooks.Add(true);
                            Worksheet workSheet = (Worksheet)workBook.ActiveSheet;
                            recordcount = dt.Rows.Count;
                            int colCount = dt.Columns.Count;
                            object[,] dataArray = new object[recordcount, colCount];
                            Random rand = new Random(DateTime.Now.Millisecond);
                            for (int i = 0; i < recordcount; i++)
                            {
                                for (int j = 0; j < colCount; j++)
                                {
                                    if (j == 0)     //在C_UserID字段前面加上单引号"'",避免数字字符串以科学技术法的方式显示
                                    {
                                        dataArray[i, j] = "'" + dt.Rows[i][j].ToString();
                                    }
                                    else
                                    {
                                        dataArray[i, j] = dt.Rows[i][j];
                                    }
                                }
                            }
                            workSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[recordcount, colCount]).Value2 = dataArray;
                            workSheet = null;
                            workBook.RefreshAll();

                            workBook.SaveAs(xmlFilePath2003, miss, miss, miss, miss, miss, XlSaveAsAccessMode.xlShared, miss, miss, miss, miss, miss);
                            workBook.SaveAs(xmlFilePath2007, miss, miss, miss, miss, miss, XlSaveAsAccessMode.xlShared, miss, miss, miss, miss, miss);
                            workBook.Close(false, miss, miss);
                            workBook = null;
                            excelApp.Quit();
                            excelApp = null;
                            GC.Collect();
                            //string rethtml = "<a href='javascript:DownFile(\"" + filepath + title2003 + "\")'>" + title2003 + "</a>&nbsp;&nbsp;<a href='javascript:DownFile(\"" + filepath + title2007 + "\")'>" + title2007 + "</a>";
                            string rethtml = "<a href='down.aspx?filename=" + filepath + title2003 + "'>" + title2003 + "</a>&nbsp;&nbsp;<a href='javascript:DownFile(\"" + filepath + title2007 + "\")'>" + title2007 + "</a>";

                            Response.Write(rethtml);
                            Response.End();
                        }
                        break;                   
                }

//down.aspx下载页面
 protected void Page_Load(object sender, EventArgs e)
        {
            string retfile = Request["filename"];
            DownFile(retfile);
        }

        private void DownFile(string filename)
        {
            string fileName = filename.Substring(filename.LastIndexOf("/") + 1); //被下载的文件名
            string filePath = (dirpath + "/" + fileName).Replace('/', '\\');
            try
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
                Response.Clear();
                Response.Charset = "gb2312";
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Server.UrlEncode(fileName) + "\"");
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.ContentType = "image/jpg";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.WriteFile(fileInfo.FullName);
                Response.Flush();
                Response.End();
            }
            catch (Exception exp)
            {
            }
        }

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!