一直报说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: 异常来自 HRESULT:0x800A03EC
代码如下:protected void btnCreate_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application excel = null;
            Microsoft.Office.Interop.Word.Application word = null;
            Microsoft.Office.Interop.Word.Document doc=null;            object SaveChanges = true; //保存更改
            object OriginalFormat = System.Type.Missing;
            object RouteDocument = System.Type.Missing;
            try
            {
                string path = Server.MapPath("Doc/");//目录
                string infoListXLS = path + "myexcel.xls";//EXCEL模版
                string wordTemplate = path + "template.doc";//WORD模版
                excel = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook workBook = excel.Workbooks.Open(infoListXLS, System.Type.Missing, false, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
                //Microsoft.Office.Interop.Excel.Worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;
                
                excel.Visible = false;                int intRow = 2;
                //string repStr = "&Chr(13)&Chr(10)";
                //string desStr = "@";                //开始循环,直到第十一列“题干”为空时退出循环
               
                while (excel.Cells[intRow, 11].ToString() != "")
                {                    //取得第十一列内容即 题干
                    string ti = excel.Cells[intRow, 11].ToString();
                    //取得第十二列内容即 内容,实现把@替换成Word中的换行符 
                    string daan = excel.Cells[intRow, 12].ToString();
                    string docFile = path + ti + ".doc";
                    if (!File.Exists(docFile))
                    {
                        File.Copy(wordTemplate,docFile);
                        word = new Microsoft.Office.Interop.Word.Application();
                        word.Visible = false;
                        object fileName = docFile; //文件名称                        object ConfirmConversions = false; //允许转换                        object ReadOnly = false; //只读方式打开                        object AddToRecentFiles = false; //添加到最近打开的文档                        object PasswordDocument = System.Type.Missing;                        object PasswordTemplate = System.Type.Missing;                        object Revert = System.Type.Missing;                        object WritePasswordDocument = System.Type.Missing;                        object WritePasswordTemplate = System.Type.Missing;                        object Format = System.Type.Missing; //格式                        object Encoding = System.Type.Missing; //编码                        object Visible = System.Type.Missing;                        object OpenAndRepair = System.Type.Missing;                        object DocumentDirection = System.Type.Missing;                        object NoEncodingDialog = System.Type.Missing;                        object XMLTransform = System.Type.Missing;                        doc = word.Documents.Open(ref fileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform);
                        Microsoft.Office.Interop.Word.Table docTable = doc.Tables[0];
                        docTable.Cell(1, 1).Range.Text = ti;
                        docTable.Cell(2, 1).Range.Text = daan;
                        doc.Save();
                        
                        doc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
                        word.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
                        intRow++;
                    }
                    excel.Quit();                }
            }
            catch (Exception ex)
            {
                throw ex;
                
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
                }
                if (word != null)
                {
                    word.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
                }
                excel.Quit();                
                    
                
            }        }

解决方案 »

  1.   

     while (excel.Cells[intRow, 11].ToString() != "")
    这一行就要出现这个异常
      

  2.   

    试下这个:这个错误解决方法是:要设置账户asp.net访问应用程序Excel的权限,并设置该应用程序目录的权限为“写入”,否则应用程序会才出现楼主所示错误,当然Excel文件的成功倒出来了看下这个:http://wenda.tianya.cn/wenda/thread?tid=2764e58c48033157
    http://topic.csdn.net/u/20090617/11/4a48b4ca-b568-457c-8b1c-58da6270cb2a.html
      

  3.   

    关于asp.net导出Excel出现“异常来自 HRESULT:0x800A03EC”错误的另一种可能 :
    Posted on 2009-12-21 12:05 阿松 阅读(2292) 评论(2) 编辑 收藏 所属分类: 其它, 原贴  好不容易花了半天终于解决了导出数据到Excel出现“异常来自 HRESULT:0x800A03EC”的错误。网上的解决方案基本上都是说开始行的下标值写成了0导致的“excel.Cells[0, 1] = "第1行第1列";”,改为大于0就能解决。但我的程序都是从第4行开始写数据的,想可能不是这个问题导致的。插入断点,调试程序,又出现“不能单步执行”的错误。 后来又仔细看了这个异常,发现异常是从“excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);”这个语句跳出来的,如果我把写入数据行的小标从0开始,异常会从那个写入行时跳出来。发现问题可能是在这个语句的参数上,于是随便把语句改为了“excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel7, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);”居然发现解决了问题,这个问题可能是我的Excel的版本不对吧(我的excel版本为2007)。信息来源:http://www.cnblogs.com/Gaton/articles/1628761.html
      

  4.   

    我觉得excel.Cells[intRow, 11]有问题的可能是你的introw,因为excel不知道你要定位在那个位置,比如是“E11”还是什么,所以你这个要组合好。我觉得cells不应该两个都是数字
      

  5.   

    恩,当行号为零时,会出现这个错误
    再就是了再给CEll复制的时候是先行号,在列好。
    而且你看一下你的赋值是不是格式正确了。必须是 值类型
      

  6.   

    有时候获取excel单元格时,超出excel的范围也会报这个异常
      

  7.   

    我是这样用的:            for ( j = 0; j < 26; j++)    //xlsSheet.Cells.Rows.Count            
                {
                    for ( i = 0; i < 26; i++) //xlsSheet.Cells.Columns.Count
                    {
                        if (i == 8)
                        {
                            xlsSheet.get_Range(xlsSheet.Cells[j + 1, i + 1], xlsSheet.Cells[j + 1, i + 1]).NumberFormatLocal = "0";//"G/通用格式" @ General
                            xlsSheet.get_Range(xlsSheet.Cells[j + 1, i + 1], xlsSheet.Cells[j + 1, i + 1]).Interior.ColorIndex = 18;
                        
                        }                    str = str + "," + xlsSheet.Cells[i+1, j+1].ToString().Trim();
                    }
                }