我用datagrid生成xls文件的一段代码莫名其妙的错误
hthy 2003-08-23 11:24:42 代码如下:
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 System.Data.SqlClient;
using OWC;
using System.Configuration;
namespace mytest
{
/// <summary>
/// exex 的摘要说明。
/// </summary>
public class exex : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox xlfile;
protected System.Web.UI.WebControls.Button ExportDataGrid2Excel;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Label1.Text = "请输入要保存得文件名字:";
ExportDataGrid2Excel.Text = "由DataGrid生成Excel文件";
DataGrid1.Columns[0].HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
DataGrid1.Columns[0].HeaderText = "文章名称";
DataGrid1.Columns[1].HeaderText = "作者";
// DataGrid1.Columns[0].HeaderStyle.Font.Bold = True;
DataGrid1.Style.Add("font-size", "9pt");
this.BindDataGrid();
}
private void BindDataGrid()
{
SqlConnection myconn=new SqlConnection();
myconn.ConnectionString=ConfigurationSettings.AppSettings["test"];
string query="select * from exex";
SqlDataAdapter mya=new SqlDataAdapter(query,myconn);
DataSet mydata=new DataSet();
mya.Fill(mydata,"exex");
this.DataGrid1.DataSource=mydata.Tables["exex"].DefaultView;
DataGrid1.DataBind();
}
private void export()
{
SpreadsheetClass xlsheet=new SpreadsheetClass();
int i = 0;
int j = 0;
foreach (DataGridColumn oItem in DataGrid1.Columns)
{
xlsheet.ActiveSheet.Cells[1, i + 1] = oItem.HeaderText;
// xlsheet.ActiveSheet.Cells(1, i + 1).Font.Bold = True;
// xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Bold = True;
//
// xlsheet.Range(xlsheet.Cells(1, 1), xlsheet.Cells(1, i + 1)).Font.Color = "red";
i = i + 1;
}
int numbercols = DataGrid1.Items[0].Cells.Count;
for (j=0;j<DataGrid1.Items.Count - 1;j++)
{
for (i=0;i< numbercols - 1;i++)
{
// xlsheet.(xlsheet.Cells(2, 2), xlsheet.Cells(j + 2, i + 1)).Font.Color = "blue";
// xlsheet.Range(xlsheet.Cells(2, 1), xlsheet.Cells(j + 2, i + 1)).AutoFitColumns();
xlsheet.ActiveSheet.Cells[j + 2, i + 1] = DataGrid1.Items[j].Cells[i].Text.Replace(" ", " ");
}
}
xlsheet.ActiveSheet.Export("c:/my.xls",OWC.SheetExportActionEnum.ssExportActionNone);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ExportDataGrid2Excel.Click += new System.EventHandler(this.ExportDataGrid2Excel_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ExportDataGrid2Excel_Click(object sender, System.EventArgs e)
{
this.export();
}
}
}
错误提示如下:
“/mytest”应用程序中的服务器错误。
--------------------------------------------------------------------------------
HRESULT 中的异常:0xE004002A。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Runtime.InteropServices.COMException: HRESULT 中的异常:0xE004002A。
源错误:
行 92:
行 93:
行 94: xlsheet.ActiveSheet.Export("c:/my.xls",OWC.SheetExportActionEnum.ssExportActionNone);
行 95:
行 96: