文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>利用MyXls导出Excel并下载

利用MyXls导出Excel并下载

时间:2010-09-30  来源:杨硕

导出Excel并且下载

 

protected void butExport_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = GetList(StrSql).Tables[0];
            ExportExcel excel = new ExportExcel();
            string fileName = excel.Export(dt, DropDownList1.SelectedItem.Text, DataName);
            string path = Request.PhysicalApplicationPath + "Excel\\" + fileName + ".xls";
            DownFile.ResponseFile(Page.Request, Page.Response, fileName + ".xls", path, 1024000);

 

            FileInfo file = new FileInfo(path);
            file.Delete();
        }
        catch (Exception ex)
        {
            throw new AppException(ex);
        }

    }

 

 

利用MyXls导出Excel

  /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public string Export(DataTable dt, string xlsName, List<string> dataName)
        {
            XlsDocument xls = new XlsDocument();
            string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
            xls.FileName = fileName;

 

            Worksheet sheet = xls.Workbook.Worksheets.Add(xlsName);//Sheet名称

            Cells cells = sheet.Cells;

            Cell cell = cells.Add(1, 1, dataName[0].ToString());
            cell.Font.Bold = true;

            for (int i = 1; i < dataName.Count; i++)
            {
                cell = cells.Add(1, i + 1, dataName[i].ToString());
                cell.Font.Bold = true;

            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    cells.Add(i + 2, j + 1, dt.Rows[i][j].ToString());
                }
            }

            string file = System.Web.HttpContext.Current.Server.MapPath("/Excel/");
            xls.Save(file);
            return fileName;
        }

 

文件下载,目前用着还不错

  public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
        {
            try
            {
                FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader br = new BinaryReader(myFile);
                try
                {
                    _Response.AddHeader("Accept-Ranges", "bytes");
                    _Response.Buffer = false;
                    long fileLength = myFile.Length;
                    long startBytes = 0;

 

                    double pack = 10240; //10K bytes
                    //int sleep = 200;   //每秒5次   即5*10K bytes每秒
                    int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
                    if (_Request.Headers["Range"] != null)
                    {
                        _Response.StatusCode = 206;
                        string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
                        startBytes = Convert.ToInt64(range[1]);
                    }
                    _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
                    if (startBytes != 0)
                    {
                        //Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
                    }
                    _Response.AddHeader("Connection", "Keep-Alive");
                    _Response.ContentType = "application/octet-stream";
                    _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));

                    br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                    int maxCount = (int)Math.Floor((fileLength - startBytes) / pack) + 1;

                    for (int i = 0; i < maxCount; i++)
                    {
                        if (_Response.IsClientConnected)
                        {
                            _Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
                            Thread.Sleep(sleep);
                        }
                        else
                        {
                            i = maxCount;
                        }
                    }
                }
                catch
                {
                    return false;
                }
                finally
                {
                    br.Close();

                    myFile.Close();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载