写入数据库是msdn的例子,应该不会错误读出重新生成文件为什么错误呢?帮忙谢谢

解决方案 »

  1.   

    搜索到这个:http://expert.csdn.net/Expert/topic/2731/2731327.xml?temp=.3578302难道只能用这个来解决?
      

  2.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using JBInfoBase;
    using Excel;
    namespace JBInfoManage._2052.ASPX.ClassManage
    {
    /// <summary>
    /// ClassExportExcel 的摘要说明。
    /// </summary>
    public class ClassExportExcel : System.Web.UI.Page
    {
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(this.Request.QueryString["ClassGuid"]!=null)
    {
    JBInfoBase.SingMems pSM=new JBInfoBase.SingMems();
    string pFileName=this.Request.QueryString["ClassName"].ToString();
    System.Data.DataTable pDateTable=pSM.SearchExcelDate(this.Request.QueryString["ClassGuid"].ToString().Trim());
    HttpResponse rsp;
    rsp=Page.Response;

    rsp.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//乱码问题
    rsp.AppendHeader("Content-Disposition", "attachment;filename="+pFileName ); //'必要,做成下载文件 Excel.Application oExcel=null;  


    Excel.Workbook oBook;  
    Object oMissing=System.Reflection.Missing.Value ;
    oExcel=new Excel.Application();  
    oBook=oExcel.Workbooks.Add(oMissing);  
    int rows;
    int clumns;
    // System.Globalization.CultureInfo pa=new System.Globalization.CultureInfo("zh-CHS");
    //
    //  System.Globalization.NumberFormatInfo pN=new System.Globalization.NumberFormatInfo();

    rows=pDateTable.Rows.Count-1;
    clumns=pDateTable.Columns.Count-1;
    for(int i=0;i<=rows;i++)  
    {  
    // oExcel.get_Range(oExcel.Cells[i,6],oExcel.Cells[i+1,6]).ClearFormats();
    for(int j=0;j<=clumns;j++)  
    {
    if(i==0)
    {

    oExcel.Cells[1,j+1]=pDateTable.Columns[j].ColumnName.ToString();  
    }

    oExcel.Cells[i+2,j+1]=pDateTable.Rows[i][j].ToString();  
    }
    }

    oBook.Saved=true;  
    oExcel.UserControl=false;


    string  file=Server.MapPath(".")+"\\" + pFileName;//Server.MapPath(".")服务器保存地址  
    oExcel.ActiveWorkbook.SaveCopyAs(file);  
    //oExcel.Quit();

    rsp.WriteFile(file);
    oExcel.Quit();
    oExcel=null;

    // rsp.Flush();
    // rsp.End();
    //JBInfoBase.MakeExcel pME=new JBInfoBase.MakeExcel();
    //pME.GenerateFile(this.Page,this.Request.QueryString["ClassName"].ToString(),pDT);
    }
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    }
    }
      

  3.   

    能解释一下,为什么不能打开这个excel文件呢?已经生成好了在指定文件夹,但是打开就提示:不能访问xx.xls,该文件是只读的
      

  4.   

    你还是不要用例子了把字段设成ntext类型,加数据库中时:Convert.ToBase64String(...)读出数据量应该不大,你就一下读出来:记得转换一下 Convert.FromBase64String(...)
    fs.Write(fsdata,0,len);
    就可以了