求救!!! excel导出数据问题!我被折磨的快不行了,各位大侠救救我: ( 积分: 100 )

D

dwj_dd

Unregistered / Unconfirmed
GUEST, unregistred user!
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 Office;
using VBIDE;
using Excel;
using System.Reflection;
namespace ls
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void UpLoadExcel()
{
//获取记录集
//sqlOperate.OpenConnection();
//sqlOperate.OpenData(strSql, out dataSet);
string selTempRecord = "SELECT * FROM [dbo].[ryfl]";
DataSet dataSet = NsDb.Db.GetDataSet(selTempRecord);

//------------创建Excel对象并且写入,然后存到服务器上-----------
//定义一个Excel对象
Excel.ApplicationClass xExcel = new Excel.ApplicationClass();
xExcel.Visible = false;
//打开一个文件
//string openPath = "d://impexcel//MyTemplate.xls";
string openPath = "f://ls//ls.xls";
Excel.Workbook xBook = xExcel.Workbooks.Open(openPath,Missing.Value,Missing.Value,Missing.Value,Excel.XlTextQualifier.xlTextQualifierNone,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
//循环加载记录
for(int row = 0;
row < dataSet.Tables[0].Rows.Count;
row++)
{
for(int i =0;
i < dataSet.Tables[0].Columns.Count ;
i++)
{
xExcel.Cells[row+1,i+1] =dataSet.Tables[0].Rows[row].ToString();
}
}
dataSet.Dispose();
//设置保存信息
string savePath = "f://ls//ls1.xls";
xBook.SaveAs(savePath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);执行到这一步时好象死循环了,跳不出来
// xBook.Save();

//开始保存
xBook.Save();

//释放对象资源
xBook.Close(false, Missing.Value, Missing.Value);
xBook = null;
xExcel.Quit();
xExcel = null;
}
private void Button1_Click(object sender, System.EventArgs e)
{
UpLoadExcel();
GC.Collect();
}
}
}
以上为全部源码,在XP+OFFICE200上调试没问题,在2000 adv servver+office上xBook.SaveAs出现死循环,执行不出来,请大家一定救救我!
 
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 Office;
using VBIDE;
using Excel;
using System.Reflection;
namespace ls
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void UpLoadExcel()
{
//获取记录集
//sqlOperate.OpenConnection();
//sqlOperate.OpenData(strSql, out dataSet);
string selTempRecord = "SELECT * FROM [dbo].[ryfl]";
DataSet dataSet = NsDb.Db.GetDataSet(selTempRecord);

//------------创建Excel对象并且写入,然后存到服务器上-----------
//定义一个Excel对象
Excel.ApplicationClass xExcel = new Excel.ApplicationClass();
xExcel.Visible = false;
//打开一个文件
//string openPath = "d://impexcel//MyTemplate.xls";
string openPath = "f://ls//ls.xls";
Excel.Workbook xBook = xExcel.Workbooks.Open(openPath,Missing.Value,Missing.Value,Missing.Value,Excel.XlTextQualifier.xlTextQualifierNone,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
//循环加载记录
for(int row = 0;
row < dataSet.Tables[0].Rows.Count;
row++)
{
for(int i =0;
i < dataSet.Tables[0].Columns.Count ;
i++)
{
xExcel.Cells[row+1,i+1] =dataSet.Tables[0].Rows[row].ToString();
}
}
dataSet.Dispose();
//设置保存信息
string savePath = "f://ls//ls1.xls";
xBook.SaveAs(savePath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);执行到这一步时好象死循环了,跳不出来
// xBook.Save();

//开始保存
xBook.Save();

//释放对象资源
xBook.Close(false, Missing.Value, Missing.Value);
xBook = null;
xExcel.Quit();
xExcel = null;
}
private void Button1_Click(object sender, System.EventArgs e)
{
UpLoadExcel();
GC.Collect();
}
}
}
以上为全部源码,在XP+OFFICE200上调试没问题,在2000 adv servver+office上xBook.SaveAs出现死循环,执行不出来,请大家一定救救我!
 
xExcel.asSave(savePath);//不要那个存,用这个存,行不行????
 
顶部