how to use NetOffice to manipulate xlsx file

Apr 5, 2013 at 1:01 PM
Edited Apr 5, 2013 at 1:14 PM
Hi there,

i have written this C# code fragment, but it still fails with a COMException.
It seems that i have overseen sth. while opening the file as it assumes that the opened xlsx file is write-protected. Can anyone help me?
        /// <summary>
        /// Converts html file to excel file (xlsx)
        /// </summary>
        /// <param name="from">this should be the full filename</param>
        public static void ConvertToExcel(String from)
        {
            Excel.Application cscExcelApp = null;
            Excel.Workbook cscWorkbook;
            Excel.Worksheet cscWorksheet;
            try
            {

                // First Contact: Excel Prozess initialisieren
                cscExcelApp = initExcel();
                cscExcelApp.DisplayAlerts = false;
                // Excel Datei anlegen: Workbook
                var myCount = cscExcelApp.Workbooks.Count;

                cscWorkbook = (Excel.Workbook)(cscExcelApp.Workbooks.Open(from,
                        Type.Missing, false, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing, Excel.Enums.XlPlatform.xlWindows, Type.Missing,
                        Type.Missing, Type.Missing, Type.Missing, false,
                        Type.Missing, Type.Missing));
                cscWorksheet = (Excel.Worksheet)cscWorkbook.ActiveSheet;
                cscExcelApp.ActiveWindow.DisplayGridlines = true;

                transformData(cscExcelApp, cscWorksheet);

                cscWorksheet.Name = "report";

                SetHeaderAndFooter(cscExcelApp, cscWorksheet);
// everything work fine until here
                cscWorkbook.SaveAs("D:\\temp\\report.xlsx",
                    Excel.Enums.XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing,
                    false, Excel.Enums.XlSaveAsAccessMode.xlShared, Excel.Enums.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing,
                    Type.Missing, Type.Missing, Type.Missing);
                cscExcelApp.DisplayAlerts = false;
                cscExcelApp.Dispose();
                cscWorkbook.Close(true, "D:\\temp\\report.xlsx", System.Reflection.Missing.Value);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                // Excel beenden
                if (cscExcelApp != null)
                {

                    cscExcelApp.Quit();
                }

            }
        }
Here is the caught exception:
"System.Runtime.InteropServices.COMException" wurde aufgefangen.
  HResult=-2147467259
  Message=See inner exception(s) for details.
  Source=NetOffice
  ErrorCode=-2147467259
  StackTrace:
       bei NetOffice.Invoker.Method(COMObject comObject, String name, Object[] paramsArray)
       bei NetOffice.ExcelApi._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)
       bei audi.ExcelGenerator.ConvertToExcel(String from)
  InnerException: System.Reflection.TargetInvocationException
       HResult=-2146232828
       Message=Ein Aufrufziel hat einen Ausnahmefehler verursacht.
       Source=mscorlib
       StackTrace:
            bei System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
            bei System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
            bei System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
            bei NetOffice.Invoker.Method(COMObject comObject, String name, Object[] paramsArray)
       InnerException: System.Runtime.InteropServices.COMException
            HelpLink=C:\Program Files\Microsoft Office\Office12\1031\XLMAIN11.CHM
            HResult=-2146827284
            Message=Auf die Datei konnte nicht zugegriffen werden. Versuchen Sie die folgenden Lösungsvorschläge:

• Überprüfen Sie, ob der angegebene Ordner vorhanden ist.
• Stellen Sie sicher, dass  der Ordner, in dem sich die Datei befindet, nicht schreibgeschützt ist.
• Vergewissern Sie sich, dass der Dateiname keines der folgenden Zeichen enthält:  <  >  ?  [  ]  : | oder  *
• Der Dateiname darf nicht länger als 218 Zeichen sein.
            Source=Microsoft Office Excel
            ErrorCode=-2146827284
            InnerException: 
Apr 5, 2013 at 1:33 PM
Edited Apr 5, 2013 at 1:35 PM
Solved. After replacing argument Excel.Enums.XlSaveConflictResolution.xlLocalSessionChanges with Type.Missing it has worked.
I am also replaced Excel.Enums.XlSaveAsAccessMode.xlShared with Excel.Enums.XlSaveAsAccessMode.xlExclusive as i am not interested in that this document should be shared with another one ;)
It was a strange behaviour. Maybe anyone can explain it.
Apr 5, 2013 at 1:37 PM
Shouldn't be an API easy to follow? Why are there so misstyled methods like open and saveas constructed?