protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/Report/");
        string filename = path + "配电缺陷统计记录.xls";
        object oReadOnly = true;
        
        Excel._Workbook Myworkbook;
        Excel._Application oExcel = new Excel.Application();
        object missing = System.Reflection.Missing.Value;
        
        Myworkbook = oExcel.Workbooks.Open(@filename, missing, oReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
        Excel.Worksheet Myworksheet = (Excel.Worksheet)Myworkbook.Worksheets["Sheet1"];
        oExcel.Visible = true;
        Myworksheet.Rows.AutoFit();
        List<PmsModel.V_DePower> list = ViewState["list"] as List<PmsModel.V_DePower>;        int Rindex1 = 5;        int id = 1;
        foreach (PmsModel.V_DePower model in list)
        {
            
            Myworksheet.Cells[Rindex1, 1] = id;
            Myworksheet.Cells[Rindex1, 2] = (model.LineName + model.EquipmentName + model.DefectContent);
            Myworksheet.Cells[Rindex1, 3] = model.DefectCategory;
            Myworksheet.Cells[Rindex1, 4] = model.DiscoveryTime;
            Myworksheet.Cells[Rindex1, 5] = model.DiscoveryMan;
            Myworksheet.Cells[Rindex1, 6] = model.ApprovalSituation;
            Myworksheet.Cells[Rindex1, 7] = model.OverhaulsTime;
            Myworksheet.Cells[Rindex1, 8] = model.Overhauls;
            Myworksheet.Cells[Rindex1, 9] = model.ApprovalTime;
            Myworksheet.Cells[Rindex1, 10] = model.Approval;
            Rindex1++;
            id++;
        }
        string FullFileName = path + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls;
        Myworksheet.SaveAs(FullFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing);
        Myworkbook.Saved = true;
        oExcel.Workbooks.Close();        oExcel.Quit();
        FileInfo DownloadFile = new FileInfo(FullFileName);
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
        Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
        Response.Write(DownloadFile.FullName);
        //Response.TransmitFile(DownloadFile.FullName);        Response.Flush();
        Response.End();
}点击导出的时候Excel只是闪一下,然后就存到我指定的文件夹Report中了,没有实现下载功能另存为, 这是为什么呀?

解决方案 »

  1.   

    你这是用excel组建导出的,你要用留的方式导出 也就是用response.write的方法 具体代码网上很多
      

  2.   

    FileInfo DownloadFile = new FileInfo(FullFileName);
            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = false;
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
            Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
            
    Response.TransmitFile(DownloadFile.FullName); 
      

  3.   

        private void ShowExcel(DataTable table)
        {
            DateTime start = DateTime.Now;
            Excel.Application app = new Excel.ApplicationClass();
            app.Visible = false;
            app.DisplayInfoWindow = false;
            Excel.Workbooks books = app.Workbooks;
            Excel.Workbook book = books.Open(Server.MapPath("Template/Allowance.xls"), Missing.Value, true, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            Excel.Worksheet sheet = (Excel.Worksheet)book.Sheets[1];        DateTime end = DateTime.Now;        try
            {
                int currentRow = 1;
                string[] data = new string[table.Columns.Count];
                foreach (DataRow row in table.Rows)
                {
                    for (int i = 0; i < row.ItemArray.Length; i++)
                    {
                        data[i] = row.ItemArray[i].ToString();
                    }
                    currentRow++;                sheet.Cells[currentRow, 1] = String.Join("|", data);
                    Excel.Range range = (Excel.Range)sheet.Cells[currentRow, 1];
                    range.TextToColumns(Missing.Value, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierNone,
                        false, false, false, false, false, true, "|", Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                    ((Excel.Range)sheet.Cells[currentRow, 1]).AddComment(row.RowError);            }            sheet.UsedRange.Columns.AutoFit();
                Excel.Borders borders = ((Excel.Borders)sheet.UsedRange.Borders);
                borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                borders.Weight = Excel.XlBorderWeight.xlThin;
                ////凍結儲存格
                //((Excel.Range)sheet.Cells[3, 4]).Select();
                ((Excel.Range)sheet.Cells[2, 4]).Select();
                app.ActiveWindow.FreezePanes = true;            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_Allowance.xls";
                book.SaveAs(Server.MapPath("Download/" + fileName), Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Excel.XlSaveConflictResolution.xlLocalSessionChanges,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                Response.Redirect("Download/" + fileName, false);
            }
            finally
            {
                book.Close(true, Missing.Value, Missing.Value);
                books.Close();
                app.Quit();
                foreach (System.Diagnostics.Process excelProcess in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
                {
                    if (excelProcess.StartTime > start && excelProcess.StartTime < end)
                    {
                        excelProcess.Kill();
                    }
                }
            }
        }
      

  4.   

    把dataset数据保存到excel
    public void CreateExcel(DataSet ds, 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();
        }
      

  5.   

    http://support.microsoft.com/kb/306654
      

  6.   

    现在都啥年代了,还用dataset,汗水!
      

  7.   

    再给你个链接:http://support.microsoft.com/kb/932921
      

  8.   

    原因我找到了,谢谢各位,是因为ajax无刷新的问题