代码如下:
Excel.Application ExApp = new Excel.Application();
Excel.Workbooks MyWorkbooks = ExApp.Workbooks;
Excel.Workbook MyWorkbook = MyWorkbooks.Add(Excel .XlWBATemplate .xlWBATWorksheet );
Excel.Worksheet MyWorksheet = (Excel.Worksheet)MyWorkbook.Worksheets[1];
红色代码为出错代码,编译环境给出的提示如下:
无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel._Application”。此操作失败的原因是对 IID 为“{000208D5-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 不支持此接口 (异常来自 HRESULT:0x80004002 (E_NOINTERFACE))。
编译环境为:visual studio 2010;.com为excel.11.0 library
没怎么用过你那种方法
我的方法
#region 导出excel,word
/// <summary>
/// 将gridview的导出
/// </summary>
/// <param name="FileType">导出类型(application/ms-excel,application/ms-word)</param>
/// <param name="FileName">导出的文件名(test.xls,test.doc)</param>
/// <param name="GridView1">要导出的数据</param>
private void Export(string FileType, string FileName, DataList dl)
{
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
dl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
protected void btn_export_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生报名表.xls", dl_memberlist);
}
public override void VerifyRenderingInServerForm(Control control)
{
}
#endregion
推荐到OSCHINA上下载一个开源的组件。这样不用依靠OFFICE 就可以导出到Excel