需要引用com:MS Excel 10.0 Object LIbray (.net带的),例子如下:
在客户IE生成EXCEL
string SQL_excel="select * from files ;
Response.ContentType = "application/octet-stream";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.Default;
Response.AddHeader("Content-Disposition", "attachment;filename=result.xls");
SqlConnection conn=new SqlConnection(Application["conn"].ToString());
SqlDataAdapter excel_da=new SqlDataAdapter(SQL_excel,conn);
DataSet excel_ds=new DataSet("files");
excel_da.Fill(excel_ds);
DataTable excel_dt=excel_ds.Tables[0];
DataGrid1.DataSource=excel_ds;
DataGrid1.DataBind();
conn.Close();
this.EnableViewState = false;
StringWriter tw=new StringWriter();
HtmlTextWriter hw=new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
在服务器生成excel
int rowIndex=1;
int colIndex=0;
Excel.Application excel= new Excel.Application();
try
{
excel.Application.Workbooks.Add(true);
excel.Visible=true;
}
catch
{
Response.Write("您可能没有安装OFFICE!");
return ;
}
foreach(DataRow row in excel_dt.Rows)
{
Response.Write("do");
rowIndex++;
colIndex=0;
foreach(DataColumn col in excel_dt.Columns)
{
Response.Write("go");
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
excel.Visible=true;
在客户IE生成EXCEL
string SQL_excel="select * from files ;
Response.ContentType = "application/octet-stream";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.Default;
Response.AddHeader("Content-Disposition", "attachment;filename=result.xls");
SqlConnection conn=new SqlConnection(Application["conn"].ToString());
SqlDataAdapter excel_da=new SqlDataAdapter(SQL_excel,conn);
DataSet excel_ds=new DataSet("files");
excel_da.Fill(excel_ds);
DataTable excel_dt=excel_ds.Tables[0];
DataGrid1.DataSource=excel_ds;
DataGrid1.DataBind();
conn.Close();
this.EnableViewState = false;
StringWriter tw=new StringWriter();
HtmlTextWriter hw=new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
在服务器生成excel
int rowIndex=1;
int colIndex=0;
Excel.Application excel= new Excel.Application();
try
{
excel.Application.Workbooks.Add(true);
excel.Visible=true;
}
catch
{
Response.Write("您可能没有安装OFFICE!");
return ;
}
foreach(DataRow row in excel_dt.Rows)
{
Response.Write("do");
rowIndex++;
colIndex=0;
foreach(DataColumn col in excel_dt.Columns)
{
Response.Write("go");
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
excel.Visible=true;
解决方案 »
- asp.net验证控件的问题
- 这个错误信息什么意思怎么解决?
- 页面跳转
- URLRewriter实现二级域名重写的问题,急(帮顶有分)
- 怎样将图片名放到TextBox里?整了很久都整不出来
- 如何取客户端机器名
- [馨郁星愿]关于datalist绑定图片的问题,希望哪个有空进来看看
- 在asp.net中怎样包含一个html头文件
- 我增加 WebForm1.aspx 时,VS.net 无法创建 WebForm1.aspx.cs 文件,这是为什么啊?
- 如何在Button_Click事件中实现本页面的自动关闭
- 用RegisterClientScriptBlock("clientScript", strScript)取得的值总是前一次的值?如何修改让它可以取得是当前的值?程序如下.....
- 请问在asp.net(c#)如何引用CDO,使用cdo的邮件系统平台必须是win2000吗
是添加COM,还是添加using
但有MS Office 10.0 Object LIbray !
是这个吗?
不是Office的,是Excel,你仔细找找,有的,你装的是OFFICEXP吗?
是在添加引用里选择添加引用中的com中找吗?
Office2000是Excel 9.0
OfficeXP是Excel 10.0
Excel.Application App = new Excel.Application();
原来必须要安装OFFICE啊!
谢了!