zoukankan      html  css  js  c++  java
  • asp.net 导出数据 下载文件名 正常显示

    很多人在处理Response下载文件名是使用这个方法
    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileInfo.Name, System.Text.Encoding.UTF8));
    但这个只是针对没有空格和IE的情况下使用。

    如果想在FireFox下输出没有编码的文件,并且IE下输出的文件名中空格不为+号,就要多一次判断了。

    详细出处参考:http://www.jb51.net/article/34048.htm

    View Code
    if (Request.UserAgent.ToLower().IndexOf("msie") > -1) 
    { 
    downloadfilename = HttpUtility.UrlPathEncode(downloadfilename); 
    } 
    if (Request.UserAgent.ToLower().IndexOf("firefox") > -1) 
    { 
    Response.AddHeader("Content-Disposition", "attachment;filename=\"" + downloadfilename + "\""); 
    } 
    else 
    { 
    Response.AddHeader("Content-Disposition", "attachment;filename=" + downloadfilename); 
    } 

     源代码:project.aspx.cs

    View Code
     #region 导出数据事件
            protected void DataGrid_ItemCommand(object sender, DataGridCommandEventArgs e)
            {
                try
                {
                    if (e.CommandName == "export")
                    {
                        //取得单条数据 项目ID
                        string[] obj = e.CommandArgument.ToString().Split(',');
                        int proj_id = Convert.ToInt32(obj[0]);
                        string proj_name = obj[1].ToString();//用于最后保存文件命名使用
                        //daochu
                        string downloadfilename = "项目成本统计_";
                        ProjectBusiness.costExport(proj_id, proj_name, Response.OutputStream);
                        Response.ContentType = "application/x-msdownload";
                        Response.Charset = "";
                        //Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(downloadfilename + proj_name + ".xls", System.Text.Encoding.UTF8));
                        if (Request.UserAgent.ToLower().IndexOf("msie") > -1)
                        {
                            downloadfilename = HttpUtility.UrlPathEncode(downloadfilename);
                            proj_name = HttpUtility.UrlPathEncode(proj_name);
                        }
                        if (Request.UserAgent.ToLower().IndexOf("firefox") > -1)
                        {
                            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + downloadfilename +proj_name+ ".xls" + "\"");
                        }
                        else
                        {
                            Response.AddHeader("Content-Disposition", "attachment;filename=" + downloadfilename +proj_name+ ".xls");
                        }
                        Response.End();
                    }
                }
                catch { Message.Show(this, "E025"); return; }
            }
            #endregion

    ProjectBusiness.cs

    View Code
     #region 项目成本统计
            public static void costExport(int id, string projectName, Stream outputStream)
            {
                try
                {
                    using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(EXPENSE_OUTPUT_TEMPLATE_FILE), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        POIFSFileSystem poifs = new POIFSFileSystem(fs);
                        //创建excel
                        HSSFWorkbook workbook = new HSSFWorkbook(poifs);
                        //创建sheet1
                        ISheet sheet1 = workbook.GetSheetAt(0);
                        //创建sheet2
                        ISheet sheet2 = workbook.GetSheetAt(1);
                        //创建sheet3
                        ISheet sheet3 = workbook.GetSheetAt(2);
                        costProject(id, projectName, workbook, sheet1);
                        costWorkRecord(id, projectName, workbook, sheet2);
                        costExpense(id, projectName, workbook, sheet3);
                        workbook.Write(outputStream);
                    }
                }
                catch { }
            }
            #endregion
    
            #region 导出项目成本(日报明细表)
            private static void costWorkRecord(int id, string projectName, HSSFWorkbook workbook, ISheet sheet)
            {
                try
                {
                    PoiUtils poi = new PoiUtils(workbook, sheet);
                    //填充单元格
                    DataTable recordTable = getRecordSql(id);
                    //设置单元格样式 普通样式
                    ICellStyle cellstyle;
                    cellstyle = poi.GetCellStyle(3, 1);
                    //字体变红
                    IFont font = workbook.CreateFont();
                    ICellStyle cellred;
                    cellred = poi.CloneCellStyle(cellstyle);
                    font.Color = HSSFColor.RED.index;
                    cellred.SetFont(font);
                    //换行样式
                    ICellStyle newlinecell;
                    newlinecell = poi.GetCellStyle(3, 13);
                    //设置第一行标题
                    poi.SetCellText(1, 1, projectName + "_日报明细统计");
                    //填充单元格 第一列ID不要
                    for (int i = 0; i < recordTable.Rows.Count; i++)
                    {
                        for (int j = 1; j < recordTable.Columns.Count; j++)
                        {
                            poi.SetCellText(i + 3, j, recordTable.Rows[i][j]);
                        }
                        poi.SetCellStyle_Row(i + 3, 12, 10, cellstyle, cellred);//设置1-12列样式,第10,11列条件判断后设置样式
                        poi.SetCellStyle_Rows(i + 3, 13, 14, newlinecell);//设置13,14列换行样式;
                    }
                }
                catch { }
            }
            private static DataTable getRecordSql(int proj_id)
            {
                try
                {
                    using (DbHelper db = new DbHelper())
                    {
                        string sql = @"select TB_Work_Record.User_id,TB_User.Username,
                                    TB_User.Realname,
                                    CONVERT(varchar(100), Record_date,111)Record_date,
                                    '' as date_type,
                                    CONVERT(varchar(100), First_time,111) + '~' +
                                    CONVERT(varchar(100), Last_time,111)Record,
                                    '' as normal,
                                    '' as work_normal,
                                    '' as work_weekend,
                                    '' as work_holiday,
                                    '' as unconfirm,
                                    '' as rebut,
                                    '' as total,
                                    '' as content, '' as Remark
                                from TB_Attendance right join  TB_Work_Record on TB_Attendance.User_id=TB_Work_Record.User_id and 
                                    CONVERT(varchar(100), Record_date,23)=CONVERT(varchar(100), First_time,23) left join
                                    TB_User ON TB_Work_Record.User_id = TB_User.ID
                                where Project_id=@proj_id and Record_status != @status
                                group by TB_Work_Record.User_id,TB_User.Username,TB_User.Realname,Record_date,First_time,Last_time";
                        DbParameter[] parameters = new DbParameter[] { db.CreateParameter("@proj_id", proj_id), db.CreateParameter("@status", (int)enmStatus.删除) };
                        DataTable tbrecord = db.GetDataSet(sql, parameters).Tables[0];
                        for (int i = 0; i < tbrecord.Rows.Count; i++)
                        {
                            //累加求和备注和工作内容;
                            int userid = Convert.ToInt32(tbrecord.Rows[i]["User_id"]);
                            DateTime recordtime = Convert.ToDateTime(tbrecord.Rows[i]["Record_date"]);
                            htcond.Clear();
                            htcond.Add("userid", userid);
                            htcond.Add("record_date_total", recordtime);
                            htcond.Add("project_id", proj_id);
                            ArrayList list = workRecordCommon.sumRecord(htcond);
                            tbrecord.Rows[i]["normal"] = list[0];
                            tbrecord.Rows[i]["work_normal"] = list[1];
                            tbrecord.Rows[i]["work_weekend"] = list[2];
                            tbrecord.Rows[i]["work_holiday"] = list[3];
                            tbrecord.Rows[i]["unconfirm"] = list[8];
                            tbrecord.Rows[i]["rebut"] = list[10];
                            tbrecord.Rows[i]["total"] = list[9];
                            tbrecord.Rows[i]["Remark"] = list[6];
                            tbrecord.Rows[i]["content"] = list[7];
                            //判断日期种类
                            tbrecord.Rows[i]["date_type"] = workRecordCommon.dateType_Judgement(recordtime);
                        }
                        return tbrecord;
                    }
                }
                catch { return null; }
            }
            #endregion
    
            #region 导出项目成本(工作记录表)
            protected static void costProject(int id, string projectName, HSSFWorkbook workbook, ISheet sheet)
            {
                try
                {
                    PoiUtils poi = new PoiUtils(workbook, sheet);
                    htcond.Clear();
                    htcond.Add("project_id", id);
                    TB_Work_record[] work_record = Work_recordBusiness.FindAllById(htcond);
                    var record = from item in work_record orderby item.User_id, item.Record_date select item;
                    int i = 3;//用户或者日期不同时循环
                    int userid = -1;//初始用户ID
                    string recorddata = "";//初始记录日期;
                    string username = "";//初始用户名
                    string realname = "";
                    //累计工作时间
                    decimal normal = 0;
                    decimal work_normal = 0;
                    decimal work_weekend = 0;
                    decimal work_holiday = 0;
                    decimal unconfirmed_time = 0;
                    decimal rebut = 0;
                    //累计每个用户所有月份工作时间
                    decimal sumnormal = 0;
                    decimal sumwork_normal = 0;
                    decimal sumwork_weekend = 0;
                    decimal sumwork_holiday = 0;
                    decimal sumunconfirmed_time = 0;
                    decimal sumrebut = 0;
                    //最后一次统计使所有小计累计之和
                    decimal tempnormal = 0;
                    decimal tempwork_normal = 0;
                    decimal tempwork_weekend = 0;
                    decimal tempwork_holiday = 0;
                    decimal tempunconfirmed_time = 0;
                    decimal temprebut = 0;
                    //设置单元格样式
                    ICellStyle cellstyle;
                    cellstyle = poi.GetCellStyle(3, 1);
                    IFont font = workbook.CreateFont();
                    ICellStyle cellred;
                    cellred = poi.CloneCellStyle(cellstyle);
                    font.Color = HSSFColor.RED.index;
                    cellred.SetFont(font);
                    //设置第一行标题
                    poi.SetCellText(1, 1, projectName + "_项目成本统计");
    
                    #region 填充
                    foreach (var item in record)
                    {
                        //用户不同或用户相同月份不同就添加一行
                        if (recorddata != "" && (userid != item.User_id || recorddata != item.Record_date.ToString("yyyy-MM")))
                        {
                            poi.SetCellText(i, 1, username);
                            poi.SetCellText(i, 2, realname);
                            poi.SetCellText(i, 3, recorddata);
                            poi.SetCellText(i, 4, normal);
                            poi.SetCellText(i, 5, work_normal);
                            poi.SetCellText(i, 6, work_weekend);
                            poi.SetCellText(i, 7, work_holiday);
                            poi.SetCellText(i, 8, unconfirmed_time);
                            poi.SetCellText(i, 9, rebut);
                            poi.SetCellText(i, 10, normal + work_normal + work_weekend + work_holiday);
                            poi.SetCellStyle_Row(i, 10, 8, cellstyle, cellred);//设置1到10列的样式
                            //小计一个用户所有月份
                            sumnormal += normal;
                            sumwork_normal += work_normal;
                            sumwork_weekend += work_weekend;
                            sumwork_holiday += work_holiday;
                            sumunconfirmed_time += unconfirmed_time;
                            sumrebut += rebut;
                            //清空用于下一行累加
                            normal = 0;
                            work_normal = 0;
                            work_weekend = 0;
                            work_holiday = 0;
                            unconfirmed_time = 0;
                            rebut = 0;
                            i++;
                            //用户不同时 添加小计
                            if (userid != item.User_id)
                            {
                                //把小计赋值临时变量 最后总计
                                tempnormal += sumnormal;
                                tempwork_normal += sumwork_weekend;
                                tempwork_weekend += sumwork_normal;
                                tempwork_holiday += sumwork_holiday;
                                tempunconfirmed_time += sumunconfirmed_time;
                                temprebut += sumrebut;
                                //添加
                                poi.MergedRegion(i, 1, i, 3);
                                poi.SetCellText(i, 1, "小计:");
                                poi.SetCellText(i, 4, sumnormal);
                                poi.SetCellText(i, 5, sumwork_normal);
                                poi.SetCellText(i, 6, sumwork_weekend);
                                poi.SetCellText(i, 7, sumwork_holiday);
                                poi.SetCellText(i, 8, sumunconfirmed_time);
                                poi.SetCellText(i, 9, sumrebut);
                                poi.SetCellText(i, 10, sumnormal + sumwork_normal + sumwork_weekend + sumwork_holiday);
                                poi.SetCellStyle_Row(i, 10, 8, cellstyle, cellred);//设置1到10列的样式
                                //清空用于下一次重新小计
                                sumnormal = 0;
                                sumwork_weekend = 0;
                                sumwork_normal = 0;
                                sumwork_holiday = 0;
                                sumunconfirmed_time = 0;
                                sumrebut = 0;
                                i++;
                            }
                        }
                        #region 累加时间
                        if (item.Record_status != (int)enmStatus.删除 && item.Record_type == (int)enmRecord_type.正常工作)
                        {
                            normal += item.Work_time;
                        }
                        if (item.Record_status != (int)enmStatus.删除 && item.Record_type == (int)enmRecord_type.平时加班)
                        {
                            work_normal += item.Work_time;
                        }
                        if (item.Record_status != (int)enmStatus.删除 && item.Record_type == (int)enmRecord_type.双休日加班)
                        {
                            work_weekend += item.Work_time;
                        }
                        if (item.Record_status != (int)enmStatus.删除 && item.Record_type == (int)enmRecord_type.节假日加班)
                        {
                            work_holiday += item.Work_time;
                        }
                        if (item.Record_status == (int)enmStatus.未确认)
                        {
                            unconfirmed_time += item.Work_time;
                        }
                        if (item.Record_status == (int)enmStatus.驳回)
                        {
                            rebut += item.Work_time;
                        }
                        #endregion
                        //记录本次数据,用于和下条记录做比较
                        username = item.user.Username;
                        userid = item.User_id;
                        realname = item.user.Realname;
                        recorddata = item.Record_date.ToString("yyyy-MM");
                    }
                    #endregion
    
                    #region 最后一次
                    //最后一次小计 因为最后一次直接跳出循环了 要新添记录
                    poi.SetCellText(i, 1, username);
                    poi.SetCellText(i, 2, realname);
                    poi.SetCellText(i, 3, recorddata);
                    poi.SetCellText(i, 4, normal);
                    poi.SetCellText(i, 5, work_normal);
                    poi.SetCellText(i, 6, work_weekend);
                    poi.SetCellText(i, 7, work_holiday);
                    poi.SetCellText(i, 8, unconfirmed_time);
                    poi.SetCellText(i, 9, rebut);
                    poi.SetCellText(i, 10, normal + work_normal + work_weekend + work_holiday);
                    poi.SetCellStyle_Row(i, 10, 8, cellstyle, cellred);//设置1到10列的样式
                    i++;
                    //添加小计
                    poi.MergedRegion(i, 1, i, 3);
                    poi.SetCellText(i, 1, "小计:");
                    poi.SetCellText(i, 4, sumnormal + normal);
                    poi.SetCellText(i, 5, sumwork_normal + work_normal);
                    poi.SetCellText(i, 6, sumwork_weekend + work_weekend);
                    poi.SetCellText(i, 7, sumwork_holiday + work_holiday);
                    poi.SetCellText(i, 8, sumunconfirmed_time + unconfirmed_time);
                    poi.SetCellText(i, 9, sumrebut + rebut);
                    poi.SetCellText(i, 10, sumnormal + sumwork_normal + sumwork_weekend + sumwork_holiday + normal + work_normal + work_weekend + work_holiday);
                    poi.SetCellStyle_Row(i, 10, 8, cellstyle, cellred);//设置1到10列的样式
                    i++;
                    //最后一次合计
                    poi.MergedRegion(i, 1, i, 3);
                    poi.SetCellText(i, 1, "总计:");
                    poi.SetCellText(i, 4, tempnormal + sumnormal + normal);
                    poi.SetCellText(i, 5, tempwork_normal + sumwork_normal + work_normal);
                    poi.SetCellText(i, 6, tempwork_weekend + sumwork_weekend + work_weekend);
                    poi.SetCellText(i, 7, tempwork_holiday + sumwork_holiday + work_holiday);
                    poi.SetCellText(i, 8, tempunconfirmed_time + sumunconfirmed_time + unconfirmed_time);
                    poi.SetCellText(i, 9, temprebut + sumrebut + rebut);
                    poi.SetCellText(i, 10, tempnormal + tempwork_normal + tempwork_weekend + tempwork_holiday + sumnormal + sumwork_normal + sumwork_weekend + sumwork_holiday + normal + work_normal + work_weekend + work_holiday);
                    poi.SetCellStyle_Row(i, 10, 8, cellstyle, cellred);//设置1到10列的样式
    
                    #endregion
                }
                catch { }
            }
            #endregion
    
            #region 导出项目成本(报销明细表)
            private static void costExpense(int id, string projectName, HSSFWorkbook workbook, ISheet sheet)
            {
                try
                {
                    PoiUtils poi = new PoiUtils(workbook, sheet);
                    //填充单元格
                    htcond.Clear();
                    htcond.Add("project_id", id);
                    htcond.Add("status_export", "");
                    TB_expense_detail[] expenseTable = Expense_detailBusiness.FindAll(htcond);
                    var expense = from item in expenseTable orderby item.User_id select item;
                    //设置单元格样式 普通样式
                    ICellStyle cellstyle;
                    cellstyle = poi.GetCellStyle(3, 1);
                    //换行样式
                    ICellStyle newlinecell;
                    newlinecell = poi.GetCellStyle(3, 5);
                    //设置第一行标题
                    poi.SetCellText(1, 1, projectName + "_报销明细统计");
                    //填充单元格
                    int i = 3;
                    foreach (var item in expense)
                    {
                        poi.SetCellText(i, 1, item.user.Username);
                        poi.SetCellText(i, 2, item.user.Realname);
                        poi.SetCellText(i, 3, item.Detail_date.ToString("yyyy-MM-dd"));
                        poi.SetCellText(i, 4, item.Money);
                        poi.SetCellText(i, 5, item.Remark.Trim());
                        poi.SetCellStyle_Rows(i, 1, 4, cellstyle);
                        poi.SetCellStyle(i, 5, newlinecell);
                        i++;
                    }
                }
                catch { }
            }
            #endregion
  • 相关阅读:
    (转)SQL Server 2005两种安全验证模式
    C#练习题记录(交换两个数1)
    C# using 用法
    服务器的理解(菜鸟)
    zZ
    ZzZ
    [转]Arcgis制作泰森多边形具体步骤
    [转]免费网站推广
    [转]如何让Firefox优化得比Chrome更快
    [转]3天搞定网站重新被百度收录的方法
  • 原文地址:https://www.cnblogs.com/haoxr/p/3046604.html
Copyright © 2011-2022 走看看