我用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:


...全文
71 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenee543216 2003-08-23
  • 打赏
  • 举报
回复
private void DataGridToFile_ServerSide(string strFileName)//this.DataGridToFile_ServerSide("D:\\temp\\test.xls");
{
//remove the model columns
this.DataGrid1.Columns[0].Visible=false;
//
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
this.DataGrid1.RenderControl(hw);//get the contxt of datagrid
//
StreamWriter strwriterobj;
strwriterobj = File.CreateText ( strFileName );
strwriterobj.WriteLine(tw);//write the contxt to strFileName
strwriterobj.Close();
Response.Write ( "成功的进行了文件写操作!" );
}
以上实现dataGrid to xls
孟子E章 2003-08-23
  • 打赏
  • 举报
回复
xlsheet.ActiveSheet.Export("c:\\my.xls",OWC.SheetExportActionEnum.ssExportActionNone);

http://xml.sz.luohuedu.net/xml/content.asp
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=EC5E84EC-68F9-4CD7-9E11-6F5C92027F0B

http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=ADB6D011-A4AD-43A0-A8BF-3CED872A7A95
孟子E章 2003-08-23
  • 打赏
  • 举报
回复
权限问题.c:没有设匿名可以写
guoyan19811021 2003-08-23
  • 打赏
  • 举报
回复
up
最近使用easyui 来最为前段框架,以前使用的是extjs,不使用它的主要原因是extjs过于庞大,不适合小型项目使用。 datagrid是比较常用的一个控件,并且有的时候我们需要并不知道一个table的列数是多少,甚至不知道每列的数据有哪些,这个时候就要动态生成列了, 使用easyui中的datagrid动态生成列还是比较容易的,下面开始代码: 前段代码动态生成DataGrid,包括分页、checkbox、动态绑定数据列、动态生成按钮并绑定事件、允许泛型数据类 最新效果图: 效果如下: 使用只需要配置即可: <datagridwithpages:DataGridWithPage Grid.Row="0" Name="table"></datagridwithpages:DataGridWithPage> //设置表格内容 table.Loaded += Table_Loaded; //设置数据源、分页事件 //DemoVie

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧