当前位置:编程学习 > C#/ASP.NET >>

保存EXCEL時,出現 Exception from HRESULT: 0x800A03EC 錯誤


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


using Microsoft.Office.Interop.Excel;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

namespace testExptXls.Controllers
{
    public class XlsChartController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        //testExptXls/XlsChart/addChartXls
        public void addChartXls() 
        {
            string Text = Server.MapPath("~/File");
            string P_str_path = Text;//记录路径
           Application excelApp = new Application();

            object missing = System.Reflection.Missing.Value;

            Workbook newWorkBook =excelApp.Workbooks.Add(missing);

            Worksheet workSheet = (Worksheet)excelApp.Worksheets[1];

            int rowCount = 20;
            int colCount = 5;
            object[,] dataArray = new object[rowCount, colCount];
            Random rand = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < colCount; j++)
                {
                    dataArray[i, j] = i + j;
                }
            }
            workSheet.Range[workSheet.Cells[1, 1], workSheet.Cells[rowCount, colCount]].Value2 = dataArray;



            //增加圖表
            //设置图表大小。
            Excel.ChartObjects charts = (ChartObjects)workSheet.ChartObjects(Type.Missing);
            ChartObject chartObj = charts.Add(0, 0, 400, 300);
            Chart chart = chartObj.Chart;
            //设置图表数据区域。
            Range range = workSheet.get_Range("A1", "E10");
            //chart.ChartWizard(range, XlChartType.xl3DColumn, missing, XlRowCol.xlColumns, 1, 1, true, "标题", "X轴标题", "Y轴标题", missing);
            chart.ChartWizard(range, XlChartType.xlLine, missing, XlRowCol.xlColumns, 2, 0, true, "标题", "X轴标题", "Y轴标题", missing);
            //将图表移到数据区域之下。
            chartObj.Left = Convert.ToDouble(range.Left);
            chartObj.Top = Convert.ToDouble(range.Top) + Convert.ToDouble(range.Height);


            if (P_str_path.EndsWith("\\"))//判斷路徑是否以‘\’結尾,C#中‘\’表示要在其前頁增‘\’,如‘\\’
            {
               //SaveCopyAs把工作薄的副本保存在指定的文件夾中,但不修改內存中打開的工作薄,語法 SaveCopyAs(fileName),fileName 必須為絕對路徑+文件名。
                //newWorkBook.SaveCopyAs(P_str_path + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");//保存Excel文件此方式OK,下面的保存方法不行
               newWorkBook.SaveAs(P_str_path + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls", missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
   
            }
            else
            {
               // newWorkBook.SaveCopyAs(P_str_path + "\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");//保存Excel文件OK,下面的保存方法不行
               newWorkBook.SaveAs(P_str_path + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls", missing, missing, missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);

            }

            Response.Write("<script>alert('Excel 創建成功!');</script>");//弹出提示信息
            
            //創建進程對象
            System.Diagnostics.Process[] excelProcess = System.Diagnostics.Process.GetProcessesByName("EXCEL");
            foreach (System.Diagnostics.Process p in excelProcess)
            {
                p.Kill();
            }

        }

    }
}


1》在使用SaveAS方法時就出現 Exception from HRESULT: 0x800A03EC  異常,若改為 SaveCopyAs 就ok,請問如何處理? 

Server Error in '/' Application.
--------------------------------------------------------------------------------


 Exception from HRESULT: 0x800A03EC 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800A03EC

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[COMException (0x800a03ec): Exception from HRESULT: 0x800A03EC]
   Microsoft.Office.Interop.Excel._Workbook.SaveAs(Object Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local) +0
   testExptXls.Controllers.XlsChartController.addChartXls() +5993
   lambda_method(Closure , ControllerBase , Object[] ) +62
   System.Web.Mvc.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +214
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +253
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +21
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +324
   System.Web.Mvc.Controller.ExecuteCore() +106
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +91
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +34
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +19
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +48
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
 


2》使用SaveCopyAs 方法時,在關閉進程(kill process)時,若服務器有EXCEL文件打開時,保存也出現‘存儲被’異常在,如何處理?

C#創建EXCEL文檔后保存異常 --------------------编程问答-------------------- 我試了一下,在OFFICE 2010中產生是沒有問題的。但在office 2003版本就有異常。可能是版本問題。有沒有人知道 2003版本的此異常如何處理? --------------------编程问答-------------------- 沒人回答嗎?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,