如何读入excel模板,向这个excel模板中写入DataSet中的数据.写完以后,另存为一个文件.
明天结贴.

解决方案 »

  1.   

    protected void Button1_Click(object sender, System.EventArgs e)
    {
    SqlConnection Conn=new SqlConnection(information.ConnectionString);
    Excel.Application  oExcel;  
    Excel.Workbook  oBook;  
    Object  oMissing  =  System.Reflection.Missing.Value;  
    oExcel  =  new  Excel.Application();  
    oBook  =  oExcel.Workbooks.Add(oMissing);  
    try
    {
    Conn.Open();
    SqlCommand Cmd=Conn.CreateCommand();
    Cmd.CommandType=CommandType.StoredProcedure;
    Cmd.CommandText="njpm";
    DataSet ds=new DataSet();
    SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand=Cmd;
    da.Fill(ds,"score");
    int rowIndex=1;
    int colIndex=0; DataTable table=ds.Tables["score"]; //将所得到的表的列名,赋值给单元格
    foreach(DataColumn col in table.Columns)
    {
    colIndex++; 
    oExcel.Cells[1,colIndex]=col.ColumnName;    
    } //同样方法处理数据
    foreach(DataRow row in table.Rows)
    {
    rowIndex++;
    colIndex=0;
    foreach(DataColumn col in table.Columns)
    {
    colIndex++;
    oExcel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
    }
    }
    oBook.Saved  =  true;  
    oExcel.UserControl  =  false;  
    string  mm=Server.MapPath(".")+"\\aa.xls";//服务器保存地址  
    oExcel.ActiveWorkbook.SaveCopyAs  (mm);  
    Response.Redirect  ("aa.xls");//注意上保存和调用时的路径。
    }
    catch (Exception exc) 
    {
    string msg="数据导出Excel时出现错误!";
    Response.Write("<SCRIPT   language='javascript'>");
    Response.Write("alert('"   +   msg   +   "')");
    Response.Write("</SCRIPT>");
    Response.Write(exc.Message);
    }
    finally
    {
    Conn.Close();
    oBook=null;
    oExcel.Quit();
    oExcel=null;
    GC.Collect(0);
    KillExcelProcess();
    }
    }
    private void KillExcelProcess()
    {
    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessesByName("EXCEL")[0];
    p.Kill();
    }
      

  2.   

    首先谢楼上关注。不过我需要的功能是这样的。有一个excel模板,里面好多sheet页,我要把这个模板读出来,将DATASET中的数据逐行写进这个模板的第一个sheet页中,然后另存为另一个excel文件。哪位高人指点一下啊。要不我就再开一个百分贴。搞不定啊。